Chapter 5 Flashcards
Typically, ______ statements are used for counter-controlled iteration and ______ statements for sentinel-controlled iteration.
for, while
The do…while statement tests the loop-continuation condition ______ executing the loop’s body; therefore, the body always executes at least once.
after
The _____ statement selects among multiple actions based on the possible values of an integer variable or expression, or a String.
switch
The _____ statement, when executed in an iteration statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.
continue
The _____ operator (with short-circuit evaluation) can be used to ensure that two conditions are both true before choosing a certain path of execution.
&& (conditional AND)
If the loop-continuation condition in a for header is initially _____, the program does not execute the for statement’s body.
false
Methods that perform common tasks and do not require objects are ______ methods.
static
(T or F) The default case is required in the switch selection statement.
False. The default case is optional. If no default action is needed, then there’s no need for a default case.
(T or F) The break statement is required in the last case of a switch selection statement.
False. The break statement is used to exit the switch statement. The break statement is not required for the last case in a switch statement.
(T or F) The expression ((x > y) && (a < b)) is true if either x > y is true or a < b is true.
False. Both of the relational expressions must be true for the entire expression to be true when using the && operator.
(T or F) An expression containing the || operator is true if either or both of its operands are true.
True
(T or F) The comma (,) formatting flag in a format specifier (e.g., %,20.2f) indicates that a value should be output with a grouping separator.
True
(T or F) To test for a range of values in a switch statement, use a hyphen (-) between the starts and end values of the range in a case label.
False. The switch statement does not provide a mechanism for testing ranges of values so every value that must be tested should be listed in a separate case label.
(T or F) Listing cases consecutively with no statements between them enables the cases to perform the same set of statements.
True
(Find the error) i = 1; while (i <= 10); \++i; }
Error: The semicolon after the while header causes an infinite loop, and there’s a missing left brace.
Correction: Replace the semicolon by a {, or remove both the ; and the }.