9. Boolean Expressions and Compound Conditions
Most control structures require the performance of a test to determine which path to take or whether to repeat certain steps. These tests take the form of Boolean expressions. In addition, sometimes it is necessary to determine the next step based upon two or more Boolean expressions. When Boolean expressions are combined, the result is a compound condition. In flowcharts, Boolean expressions and compound conditions are placed inside decision symbols.
Boolean Expressions
Boolean expressions compare two values with a result of either true or false. Their general form is as follows:
expression operator expression
An expression can be a constant, variable, or calculation. The operator will be a relational operator (<, >, <=, >=, ==, or !=). Boolean expressions therefore consist of two expressions separated by a relational operator. Here are some examples (assume number’s value is 5 and amount’s value is 3):
| Example | Test | Result |
|---|---|---|
4 < 9 | Is 4 less than 9? | true |
number != 5 | Is number not equal to 5? | false |
amount >= 3 | Is amount greater than or equal to 3? | true |
number * 2 == amount + 7 | Is number times 2 equal to amount plus 7? | true |
Note that some expressions are literals in the first three examples, two are variables in the second and third examples, and both are calculations in the last example.
Additional Points
- The equality operator, used to determine whether two values are equal, is two equal signs (
==) not one equal sign (which is actually the assignment operator). - The operators’ symbols cannot be reversed (i.e.,
=<,=>, and=!are invalid operators). - When expressions are calculations, the calculations are performed prior to the relational operator being used. It is not necessary to use parentheses to ensure this occurs as the arithmetic operators have higher precedence than the relational operators.
Parentheses can, however, be used to make the calculations stand out visually. For example, the fourth Boolean expression could have been written with parentheses:
(number * 2) == (amount + 7)
Compound Conditions
Logical operators are used to combine Boolean expressions to form compound conditions:
Boolean-expression logical-operator Boolean-expression
There are three logical operators: &&, ||, and !. Only && and || can be used to create compound conditions.
The && (And) Logical Operator
&& | true | false |
|---|---|---|
true | true | false |
false | false | false |
The And logical operator used in design is implemented as && in C++. The truth table for the && operator is shown at the rightbelow. Note that the overall result of a compound condition will only be true if both Boolean expressions evaluate to true.
&& | true | false |
|---|---|---|
true | true | false |
false | false | false |
Suppose the value of a variable named number is tested to determine whether it is between 1 and 10, inclusive. The compound condition, and its result given various values for number, is as follows:
number >= 1 && number <= 10
Value of number | number >= 1 | number <= 10 | Final Result |
|---|---|---|---|
7 | true | true | true |
10 | true | true | true |
0 | false | true | false |
15 | true | false | false |
The || (Or) Logical Operator
|| | true | false |
|---|---|---|
true | true | true |
false | true | false |
The Or logical operator used in design is implemented as || in C++. The truth table for the || operator is shown at the rightbelow. Note that the overall result of a compound condition is true if one or both Boolean expressions evaluate to true.
|| | true | false |
|---|---|---|
true | true | true |
false | true | false |
Suppose the value of a variable named amount is tested to determine whether it is too small (i.e., less than 1) or too large (i.e., greater than 10). The compound condition, and its result given various values for amount, is as follows:
amount < 1 || amount > 10
Value of amount | amount < 1 | amount > 10 | Final Result |
|---|---|---|---|
7 | false | false | false |
1 | false | false | false |
0 | true | false | true |
15 | false | true | true |
The ! (Not) Logical Operator
! | |
|---|---|
true | false |
false | true |
The Not logical operator, a single exclamation point (‘!’) in C++, reverses the truth value of a statement: false is changed to true and true to false. The truth table of the ! operator is shown at the rightbelow.
! | |
|---|---|
true | false |
false | true |
Suppose the value of a variable named answer is being tested to ensure it does not have a value of 'y' (i.e., the test should test true if answer’s value is not 'y'). The test, and the result for various values of answer, is as follows:
!(answer == 'y')
Value of answer | answer == 'y' | Final Result |
|---|---|---|
'y' | true | false |
'g' | false | true |
'Y' | false | true |
Note that != could have been used instead (i.e., answer != 'y').
Additional Points
- If more than two Boolean expressions are needed in a test, combine them using
&&or||, as applicable. - To ensure a test works correctly, validate it by stepping through its logic using various values for the variables. Be sure to select values so that both true and false answers should result.
Shortcuts cannot be taken. Every Boolean expression needs its own expressions on both sides of the relational operator. The two compound conditions that follow are invalid:
number >= 1 && <= 10 amount < 1 || > 10
C++ Operator Precedence
| Operator Preceduce Levels |
|---|
! |
* / % |
+ - |
> < <= >= == != |
&& |
|| |
= += -= *= /= %= |
The precedence levels of the relational and logical operators, along with the operators we use to perform calculations, are provided at the rightbelow. Operators are listed from highest to lowest precedence with operators in the same row having the same precedence level. The increment and decrement operators are not included as the order in which they are acted on depends upon how they are used (i.e., preincrement vs. postincrement and predecrement vs. postdecrement).
| Operator Preceduce Levels |
|---|
! |
* / % |
+ - |
> < <= >= == != |
&& |
|| |
= += -= *= /= %= |
Given the precedence levels of the operators, note that:
- Parentheses are always needed when
!is used if it is not to be acted on first. In the previous example, the test for equality must be performed before the!operator is used, so the parentheses are required. Since the relational operators have higher precedence than
&&and||, it is not necessary to use parentheses around Boolean expressions in compound conditions to ensure the relational operators are acted on first. Parentheses can be used, however, to add visual clarity:(number >= 1) && (number <= 10) (amount < 1) || (amount > 10)The
&&logical operator will be used before the||operator when more than two Boolean expressions are used in a compound condition. For example, suppose a test should test true if the value of a variable namednumberis less than 10 or greater than 15 but not 0. The compound condition below seems like it would work:number < 10 || number > 15 && number != 0However,
&&would be used before||, so the result would be true if number’s value was 0 (check it out). A corrected version of the compound condition, using parentheses to counter the precedence level of the logical operators, is given below:(number < 10 || number > 15) && number != 0
The associativity of logical and relational operators is left to right.