Boolean Expression
-
A boolean expression is an expression that has relational and/or logical operators operating on boolean variables.
A boolean expression evaluates to either true or false.
-
Relational operators are:
== is identical to
!= is not identical to
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
Relational operators can operate on integers, floats, doubles and even strings (in a lexicographical fashion).
-
Logical operators are:
! logical negation
&& logical AND
|| logical OR
Logical operators operate on boolean variables or boolean expressions only.
-
Examples of Boolean expression:
long int winning_lottery_number = 947423945645, your_ticket_number;
bool is_a_winner;
cout << "Enter your lottery ticket number: ";
cin >> your_ticket_number;
is_a_winner = (your_ticket_number == winning_ticket_number); // can omit the parentheses
cout << "Please tell me if I won! ";
cout << "Output a 1 if I won, otherwise output a 0: ";
cout << is_a_winner << endl;
Another example:
bool isMarried, hasChildren, shouldShootMyself;
cout << "Are you married?\n";
cout << "Enter 1 if you are, otherwise enter 0: ";
cin >> isMarried;
cout << "Do you have any children?\n";
cout << "Enter 1 if you do, otherwise enter 0: ";
cin >> hasChildren;
shouldShootMyself = (isMarried && hasChildren); // can omit the parentheses
cout << "Output a 1 if I should shoot myself, otherwise output a 0: ";
cout << shouldShootMyself;
This example assumes that examScore and numberOfMissingHW are integers:
bool isFailing;
isFailing = (examScore < 55) || (numberOfMissingHW >= 3);
// both pairs of parentheses are inserted for readability reasons only
-
Most C++ compilers will treat any nonzero number as
true and will treat zero as false.
-
Careless use of Boolean expressions can lead to unexpected results.
-
For example, a student fails a course if his score is less than the class
average, otherwise the student passes the course. Someone may write the
following code:
if ( ! score > average )
cout < < "You failed the course.\n";
else
cout < < "You passed the course.\n";
The person read the code as saying: if it is not true that the score
is higher than the average, then you failed the course,
otherwise you passed the course.
The above code will not cause problem during compilation,
and it will also run without run-time error.
However the code actually has a mistake.
Suppose the score is 34 and the average is 63.
The compiler applies the precedence rule and interprets the
Boolean expression as the following
( ! score ) > average
This turns out to evaluates to false for the following reasons.
First because ! is a unary logical negation operator and the value of
score is nonzero and so score is converted to true.
Thus !score evaluates to false.
The next operator > compares 2 numbers and so !score is converted to 0,
and then this value is compared with 63. Since 0 is not greater than 63,
the entire expression evaluates to false, and the output will be:
You passed the course.
Of course the correct way is to use parentheses and write the above Boolean
expression as follows:
if ( ! (score > average) )
cout < < "You failed the course.\n";
else
cout < < "You passed the course.\n";
-
C++ uses short-circuit evaluation of compound Boolean expressions
for the sake of efficiency.
-
An expression such as (a && b) is false if either a or b is
false. The program first evaluates a to see if it is true
or false. If a is found to be false, the program will
set (a && b) as false without evaluate b at all.
-
Similarly an expression such as (a || b) is true if either a or b
is true. The program first evaluates a to see if it is
true or false. If a is found to be true,
the program will set (a || b) as true without evaluate b at all.
-
The following example shows how this C++ feature can be exploited to avoid
run-time error that will occur if count happens to be zero:
if (count != 0) && ((sumOfScores/static_cast(count)) < 35)
cout < < "What a low class average. Fire the professor!\n";
Make sure you revisit this example after we cover the if statement.
-
The NOT operator is a unary operator. It negates a boolean variable or expression,
turning them from true to false and vice versa.
Note that if a, b, c and d are integers, for example, then
Expression | Equivalent Expression |
!(a > b) | a <= b |
!(a == b) | a != b |
!(a == b || c == d) | a != b && c != d |
!(a == b && c > d) | a != b || c <= d |