Java Reference
In-Depth Information
This loop will never execute because its control variable, count , is greater than 5 when the
loop is first entered. This makes the conditional expression, count < 5 , false from the out-
set; thus, not even one iteration of the loop will occur.
Some Variations on the for Loop
The for is one of the most versatile statements in the Java language because it allows a
wide range of variations. For example, multiple loop control variables can be used. Con-
sider the following program:
The output from the program is shown here:
Here, commas separate the two initialization statements and the two iteration expressions.
When the loop begins, both i and j are initialized. Each time the loop repeats, i is incre-
mented and j is decremented. Multiple loop control variables are often convenient and can
simplify certain algorithms. You can have any number of initialization and iteration state-
ments, but in practice, more than two or three make the for loop unwieldy.
The condition controlling the loop can be any valid Boolean expression. It does not need
to involve the loop control variable. In the next example, the loop continues to execute un-
til the user types the letter S at the keyboard:
Search WWH ::




Custom Search