How hungry are you? (1-10): 4 How nice do the cookies look? (1-10): 6 How nice do the cookies smell? (1-10): 9 Buy cookies! Continue down the Mall.
The true-branch is executed because the sum of the ratings, 19, is greater than the threshold of 15:
(hunger + look + smell) > 15
is true.
First the values for hunger
,
look
, and smell
are added up.
Then the sum is compared to the threshold.
The parentheses make this clear, but
precedence rules could have been used.
This topic is discussed in a later chapter.
For now, remember that:
Arithmetic operators have higher precedence than relational operators, and so are done first.
The above Boolean expression could be written as:
hunger + look + smell > 15
The +
operators are done first, since they have higher precedence than the >
.
Evaluate each of the following Boolean expressions (to true or false):
Do the arithmetic first. Then compare numbers. Spaces in the expressions don't matter.