Java Reference
In-Depth Information
• The general format of the for statement is
for ( initialization ; loopContinuationCondition ; increment )
statement
where the initialization expression names the loop's control variable and provides its initial value,
loopContinuationCondition determines whether the loop should continue executing and incre-
ment modifies the control variable's value, so that the loop-continuation condition eventually be-
comes false. The two semicolons in the for header are required.
•Most for statements can be represented with equivalent while statements as follows:
initialization ;
while ( loopContinuationCondition )
{
statement
increment ;
}
• Typically, for statements are used for counter-controlled repetition and while statements for
sentinel-controlled repetition.
•If the initialization expression in the for header declares the control variable, the control variable
can be used only in that for statement—it will not exist outside the for statement.
• The expressions in a for header are optional. If the loopContinuationCondition is omitted, Java
assumes that it's always true, thus creating an infinite loop. You might omit the initialization ex-
pression if the control variable is initialized before the loop. You might omit the increment expres-
sion if the increment is calculated with statements in the loop's body or if no increment is needed.
• The increment expression in a for acts as if it's a standalone statement at the end of the for 's body.
•A for statement can count downward by using a negative increment—i.e., a decrement (p. 158).
• If the loop-continuation condition is initially false , the for statement's body does not execute.
Section 5.4 Examples Using the for Statement
• Java treats floating-point constants like 1000.0 and 0.05 as type double . Similarly, Java treats
whole-number constants like 7 and -22 as type int .
• The format specifier %4s outputs a String in a field width (p. 161) of 4—that is, printf displays
the value with at least 4 character positions. If the value to be output is less than 4 character po-
sitions wide, the value is right justified (p. 161) in the field by default. If the value is greater than
4 character positions wide, the field width expands to accommodate the appropriate number of
characters. To left justify (p. 161) the value, use a negative integer to specify the field width.
Math.pow( x , y ) (p. 162) calculates the value of x raised to the y th power. The method receives
two double arguments and returns a double value.
•The comma ( , ) formatting flag (p. 162) in a format specifier indicates that a floating-point value
should be output with a grouping separator (p. 162). The actual separator used is specific to the
user's locale (i.e., country). In the United States, the number will have commas separating every
three digits and a decimal point separating the fractional part of the number, as in 1,234.45.
•The . in a format specifier indicates that the integer to its right is the number's precision.
Section 5.5 do while Repetition Statement
•The do while statement (p. 163) is similar to the while statement. In the while , the program tests
the loop-continuation condition at the beginning of the loop, before executing its body; if the con-
dition is false, the body never executes. The do while statement tests the loop-continuation con-
dition after executing the loop's body; therefore, the body always executes at least once.
Search WWH ::




Custom Search