Java Reference
In-Depth Information
In this case, two variables step through different ranges. As long as the
loop variable v is greater than zero, the exponent value is incremented
and v is divided by ten. When the loop completes, the value 10 exp is the
smallest power of ten that is greater than or equal to value . Both the
test value and the exponent are updated on each loop iteration. In such
cases, a comma-separated list of expressions is a good technique to en-
sure that the two values are always in lockstep.
The body of this loop is simply a continue statement, which starts the
next iteration of the loop. The body of the loop has nothing to doall the
work of the loop is in the test and iteration clauses of the for statement
itself. The continue style shown here is one way to show an empty loop
body; another way is to put a simple semicolon on a line by itself or to
use an empty block with braces. Simply putting a semicolon at the end
of the for line is dangerousif the semicolon is accidentally deleted or for-
gotten, the statement that follows the for can silently become the body
of the for .
All the expressions in the for construct are optional. If initialization-
expression or update-expression is left out, its part in the loop is simply
omitted. If loop-expression is left out, it is assumed to be TRue . Thus, one
idiomatic way to write an infinite loop is as a "for ever" loop:
for (;;)
statement
Presumably, the loop is terminated by some other means, such as a
break statement (described later) or by throwing an exception.
Conventionally, the for loop is used only when looping through a range
of related values. It is bad style to violate this convention by using ini-
tialization or increment expressions that are unrelated to the boolean
loop test.
 
Search WWH ::




Custom Search