A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.
Within most languages, expressions that yield Boolean data type values are divided into two groups. One group uses the relational operators within their expressions and the other group uses logical operators within their expressions.
The logical operators are often used to help create a test expression that controls program flow. This type of expression is also known as a Boolean expression because they create a Boolean answer or value when evaluated. There are three common logical operators that give a Boolean value by manipulating other Boolean operand(s). Operator symbols and/or names vary with different programming languages:
The vertical dashes or piping symbol is found on the same key as the backslash \. You use the SHIFT key to get it. It is just above the Enter key on most keyboards. It may be a solid vertical line on some keyboards and show as a solid vertical line on some print fonts.
In most languages there are strict rules for forming proper logical expressions. An example is:
6 > 4 && 2 <= 14
6 > 4 and 2 <= 14
This expression has two relational operators and one logical operator. Using the precedence of operator rules the two “relational comparison” operators will be done before the “logical and” operator. Thus:
true && true
True and True
The final evaluation of the expression is: true.
We can say this in English as: It is true that six is greater than four and that two is less than or equal to fourteen.
When forming logical expressions programmers often use parentheses (even when not technically needed) to make the logic of the expression very clear. Consider the above complex Boolean expression rewritten:
(6 > 4) && (2 <= 14)
(6 > 4) and (2 <= 14)
Most programming languages recognize any non-zero value as true. This makes the following a valid expression:
6 > 4 && 8
6 > 4 and 8
But remember the order of operations. In English, this is six is greater than four and eight is not zero. Thus,
true && true
True and True
To compare 6 to both 4 and 8 would instead be written as:
6 > 4 && 6 > 8
6 > 4 and 6 > 8
This would evaluate to false as:
true && false
True and False
A common way to show logical relationships is in truth tables.
|x||y||x and y|
|x||y||x or y|
I call this example of why I hate “and” and love “or”.
Every day as I came home from school on Monday through Thursday; I would ask my mother, “May I go outside and play?” She would answer, “If your room is clean and your homework is done then you may go outside and play.” I learned to hate the word “and”. I could manage to get one of the tasks done and have some time to play before dinner, but both of them… well, I hated “and”.
On Friday my mother took a more relaxed viewpoint and when asked if I could go outside and play she responded, “If your room is clean or your homework is done then you may go outside and play.” I learned to clean my room quickly on Friday afternoon. Well, needless to say, I loved “or”.
For the next example, just imagine a teenager talking to their mother. During the conversation, mom says, “After all, your Dad is reasonable!” The teenager says, “Reasonable. (short pause) Not.”
Maybe college professors will think that all their students studied for the exam. Ha ha! Not. Well, I hope you get the point.
- 25 < 7 || 15 > 36
- 15 > 36 || 3 < 7
- 14 > 7 && 5 <= 5
- 4 > 3 && 17 <= 7
- ! false
- ! (13 != 7)
- 9 != 7 && ! 0
- 5 > 1 && 7
- 25 < 7 or 15 > 36
- 15 > 36 or 3 < 7
- 14 > 7 and 5 <= 5
- 4 > 3 and 17 <= 7
- not False
- not (13 != 7)
- 9 != 7 and not 0
- 5 > 1 and 7
- logical operator
- An operator used to create complex Boolean expressions.
- truth tables
- A common way to show logical relationships.
- Wikipedia: Logical connective ↵