<< Chapter < Page | Chapter >> Page > |
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. 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:
C++ Operator | Meaning | Comment | Typing |
&& | Logical and | two ampersands | |
|| | Logical or | two vertical dashes or piping symbols | |
! | Logical not | unary | the exclamation point |
In most languages there are strict rules for forming proper logical expressions. An example is:
6>4&&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:
1&&1
or
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 |
false | false | false |
false | true | false |
true | false | false |
true | true | true |
x | y | x ||y |
false | false | false |
false | true | true |
true | false | true |
true | true | true |
x | !x |
false | true |
true | false |
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.
Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
Download and store the following file(s) to your storage device in the appropriate folder(s). You may need to right click on the link and select "Save Target As" in order to download the file. Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials.
Download from Connexions: Demo_Logical_Operators.cpp
Notification Switch
Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?