Self-Review Exercises
5.1 Fill in theblanks in each of the following statements:
a) Typically, for statementsare used for counter-controlled repetition and while statements forsentinel-controlled repetition.
b) The do…while statementtests the loop-continuation condition after executing the loop’s body;therefore, the body always executes at least once.
c) The switchstatement selects among multiple actions based on the possible values of aninteger variable or expression, or a String.
d) The continue statement,when executed in a repetition statement, skips the remaining statements in theloop body and proceeds with the next iteration of the loop.
e) The && operatorcan be used to ensure that two conditions are both true before choosinga certain path of execution.
f) If theloop-continuation condition in a for header isinitially false , the program does not execute the for statement’sbody.
g) Methods that performcommon tasks and do not require objects are called static methods.
5.2 State whethereach of the following is true or false. If false, explain why.
a) The default case isrequired in the switch selection statement. (false)
b) The break statement isrequired in the last case of a switch selection statement. (false)
c) The expression ((x > y) &&(a < b)) is true if either x > y is true or a < b is true. (false)
d) An expressioncontaining the || operator is true if either or both of its operands are true. (true)
e) The comma (,) formattingflag in a format specifier (e.g., %,20.2f) indicatesthat a value should be output with a thousands separator. (true)
f) To test for a range ofvalues in a switch statement, use a hyphen (–) between thestart and end values of the range in a case label. (false)
g) Listing cases consecutivelywith no statements between them enables the cases to perform the same set ofstatements. (true)
5.3 Write a Javastatement or a set of Java statements to accomplish each of the followingtasks:
a) Sum the odd integersbetween 1 and 99, using a for statement. Assume that the integer variables sum and count have beendeclared.
b) Calculate the value of2.5 raised to the power of 3, using the pow method.
c) Print the integersfrom 1 to 20, using a while loop and the counter variable i. Assume thatthe variable i has been declared, but not initialized. Print only five integersper line.
[Hint: Use thecalculation i % 5. When the value of this expression is 0, print a newline character;otherwise, print a tab character. Assume that this code is an application. Use theSystem.out.println() method to output thenewline character, and use the System.
out.print('\t') method tooutput the tab character.]
d) Repeat part (c), usinga for statement.
5.4 Find theerror in each of the following code segments, and explain how to correct it:
a) i = 1;
while (i <= 10);
++i;
}
b) for (k = 0.1; k != 1.0; k += 0.1)
System.out.println(k);
c) switch (n)
{
case 1:
System.out.println("Thenumber is 1");
case 2:
System.out.println("Thenumber is 2");
break;
default:
System.out.println("Thenumber is not 1 or 2");
break;
}
d) The following codeshould print the values 1 to 10:
n = 1;
while (n < 10)
System.out.println(n++);