Java Reference
In-Depth Information
do
statement
while
(logical expression);
In Java,
do
is a reserved word. As with the other repetition structures, the
do
...
while
statement
can be either a simple or compound statement. If it is a compound statement,
enclose it between braces. The
logical expression
is called the loop condition.
Figure 5-5 shows the flow of execution of a
do
...
while
loop.
5
statement
logical
expression
true
false
FIGURE 5-5
do
...
while
loop
The
statement
executes first, and then the
logical expression
is evaluated. If the
logical expression
evaluates to
true
, the
statement
executes again. As long as the
logical expression
in a
do
...
while
statement is
true
, the
statement
executes. To
avoid an infinite loop, you must, as before, make sure that the body of the loop contains a
statement that ultimately makes the
logical expression
evaluate to
false
and assures
that it exits properly.
EXAMPLE 5-16
i = 0;
do
{
System.out.print(i + " ");
i = i + 5;
}
while
(i <= 20);
Search WWH ::
Custom Search