Java Reference
In-Depth Information
Sample Run: (Figure 5-3 shows the sample run.)
FIGURE 5-3
Sample run
FibonacciNumber
The
while
loop discussed in the previous section is general enough to implement all
forms of repetitions. As noted earlier, Java provides three looping structures. The previous
section discussed
while
loops in detail. This section explains how to use Java's
for
loop.
The general form of the
for
statement is:
for
(initial expression; logical expression; update expression)
statement
In Java,
for
is a reserved word. The
logical expression
is called the loop condition.
The
initial expression
,
logical expression
, and
update expression
(called
for
loop control expressions) are enclosed within parentheses and control the body
(
statement
) of the
for
statement. Note that the
for
loop control expressions are
separated by semicolons, and that the body of a
for
loop can have either a simple or
compound statement. Figure 5-4 shows the flow of execution of a
for
loop.
Search WWH ::
Custom Search