go to previous page   go to home page   go to next page hear noise

Answer:

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. 

Arithmetic inside a Boolean Expression

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 >.


QUESTION 4:

Evaluate each of the following Boolean expressions (to true or false):

12 + 4 > 16 5.2 - 1.5 <= 1.9+22*3-4 == 1+1

Do the arithmetic first. Then compare numbers. Spaces in the expressions don't matter.