Overview of the Logical Operators
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.
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. This type of expression also creates a Boolean expression because they create a Boolean answer or value when evaluated. The answers to Boolean expressions within the C++ programming language are a value of either 1 for true or 0 for false. 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 C++ programming language operators with their meanings are:
|&&||Logical and||two ampersands|
|||||Logical or||two vertical dashes or piping symbols|
|!||Logical not||unary||the exclamation point|
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.
The && - meaning AND - simply says that both sides must be true. The || - meaning or - simply says that one or the other must be true. The ! - meaning not - reverses the meainng of the variable.
In most languages there are strict rules for forming proper logical expressions. An example is:
6 > 4 && 2 <= 14
This expression evaluates the 6 > 4 to determine a true or false, and then check the operator. Since we are working with && - if the first value is true then it has to evaluate the second condition to see if they are both true. If the first value is false, it never looks at the second one, since for && BOTH have to be true.
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:
1 && 1
true && true
The final evaluation of the expression is: 1 meaning 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)
A common way to show logical relationships is in truth tables.
|x||y||x && y|
|x||y||x || y|
I call this example of why I hate "and" and love "or".
Everyday 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 view point 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.
Questions 1. 25 < 7 || 15 > 36 2. 15 > 36 || 3 < 7 3. 14 > 7 && 5 <= 5 4. 4 > 3 && 17 <= 7 5. ! false - obviously NOT false is true 6. ! (13 != 7) - 13 is not equal to 7, so we have true, but then we have NOT true, which is false 7. 9 != 7 && !0 8. 5 > && 7 Answers 1.false 2. true 3. true 4. false 5. true 6. false 7. trru 8. Error, there needs to be an operand between the operators > and &&.
"Logical Operators" by Kenneth Busbee, Download for free at http://cnx.org/contents/303800f3-07f...email@example.com is licensed under CC BY 4.0