Java Reference
In-Depth Information
In the next example, the initialization portion is also moved out of the
for
:
In this version,
i
is initialized before the loop begins, rather than as part of the
for
.
Normally, you will want to initialize the loop control variable inside the
for
. Placing the
initialization outside of the loop is generally done only when the initial value is derived
through a complex process that does not lend itself to containment inside the
for
statement.
The Infinite Loop
You can create an
infinite loop
(a loop that never terminates) using the
for
by leaving the
conditional expression empty. For example, the following fragment shows the way most
Java programmers create an infinite loop: