Class Exercise: Control Structures - if...else if ...
Syntax
if (booleanExpression) {statements}
[else if (booleanExpression) {statements} ] ...
[else {statements}]
1. One-Way Selection:
Code an if statement to test a variable named richter representing the
strength of an earthquake, and if it is true that the value of richter is
less than 5.0, output the message "Expect little or no damage".
2. Complex conditions:
Construct an if statement that combines an expression for whether richter
equals 8.0 or whether richter is greater than 8.0, and when one or the other
is true will output the message "Most buildings will fall!".
3. Compound (Block of) Statements:
Enhance the "One-Way Selection" statement to execute two statements when the
value of richter is less than 5.0: one to assign to the variable named
message the string "Damage may be light, but I'm camping outside!", and
another to output the message. Hint: surround actions with {}.
4. Two-Way Selection:
Code an if .. else statement for the expression it is NOT true that richter
is less than 5.0 then output the message "Poorly constructed buildings will
be damaged", else output the message "Expect little or no damage". To
simplify, rephrase the boolean expresssion to eliminate the "NOT" operator.
5. Multiway Selection:
Construct an if .. else if .. else structure to implement the following
mutually exclusive decisions:
boolean expression action when true (output)
------------------ -------------------------------------------
richter > or = 8.0 "Most buildings will fall"
richter > or = 7.0 "Many buildings will fall"
richter > or = 6.0 "Some buildings will fall, others damaged"
richter > or = 5.0 "Poorly constructed buildings damaged"
richter < 5.0 "Expect little or no damage"