
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 fow. 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 |
|
II |
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 <= l4
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:
l && l or true && true
The fnal 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 <= l4)
- 1472 reads