Page 177 - Introduction to Programming with Java: A Problem Solving Approach
P. 177
the third component in the header updates that count variable after each execution and before the next
evaluation of the second component’s condition.
• You can perform multidimensional iteration by putting loops inside other loops.
• To avoid duplication and/or clutter, assign complicated logical expressions to boolean variables, and
use those variables in if statement or looping conditions.
• Use input validation to avoid bringing bad data into your programs.
• Optionally, use Boolean logic to simplify the expressions in if statement and looping conditions, and
use truth tables to verify the equivalence of alternative logical expressions.
Review Questions
§4.2 Conditions and boolean Values
1. What are Java’s two Boolean values?
2. Provide a list of Java’s comparison operators.
§4.3 if Statements
3. Provide an if statement that implements this logic:
When the water temperature is less than 120F, turn the heater on by assigning the value “on” to a heater string variable. When the water temperature is greater than 140 F, turn the heater off by assigning the value “off” to a heater string variable. Don’t do anything when the water temperature is between these two temperatures.
4. Whatisthemaximumnumberof“elseif”blocksallowedinanifstatementthatusesthe“if,elseif”
form?
Apago PDF Enhancer
§4.4 && Logical Operator
5. The relational and equality operators have higher precedence than the arithmetic operators. (T / F)
§4.5 || Logical Operator
6. Correct the following code fragment so that it executes and outputs OK if a, an int variable, is equal to
either 2 or 3:
if (a = 2 || 3)
{
}
print("OK\n");
§4.6 ! Logical Operator
7. What Java operator reverses the truth or falsity of a condition?
§4.7 switch Statement
8. What happens if you forget to include break; at the end of a block of statements after a particular case:
label?
9. If you are trying to substitute a switch statement for an “if, else” statement, you can use the if condition
as the controlling expression in the switch statement. (T / F)
10. Suppose the controlling expression in a switch statement is (stdIn.next().charAt(0)), and you
want to allow either 'Q' or 'q' to produce the same result, which is: System.out.println("quitting");
Write the code fragment for the case that produces this result.
§4.8 while Loop
11. What must a while loop condition evaluate to?
Review Questions 143