Arithmetic inside a Boolean Expression

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

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.2 <= 2.0+22*3-4 == 1+1

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