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):

ExampleTestResult
4 < 9Is 4 less than 9?true
number != 5Is number not equal to 5?false
amount >= 3Is amount greater than or equal to 3?true
number * 2 == amount + 7Is 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

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

&&truefalse
truetruefalse
falsefalsefalse

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.

&&truefalse
truetruefalse
falsefalsefalse

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 numbernumber >= 1number <= 10Final Result
7truetruetrue
10truetruetrue
0falsetruefalse
15truefalsefalse

The || (Or) Logical Operator

||truefalse
truetruetrue
falsetruefalse

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.

||truefalse
truetruetrue
falsetruefalse

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 amountamount < 1amount > 10Final Result
7falsefalsefalse
1falsefalsefalse
0truefalsetrue
15falsetruetrue

The ! (Not) Logical Operator

!
truefalse
falsetrue

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.

!
truefalse
falsetrue

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 answeranswer == 'y'Final Result
'y'truefalse
'g'falsetrue
'Y'falsetrue

Note that != could have been used instead (i.e., answer != 'y').

Additional Points

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:

The associativity of logical and relational operators is left to right.