Java Reference
In-Depth Information
do
{
i
=
a.func ();
}
while (i
<
5)
Sometimes it is necessary to instantly break out of a
while
or
do-while
loop
without waiting on the test at either the beginning or end of the loop. The
break
statement provides that functionality. The processing will jump from a loop via
a
break
statement and continue to the statements following the loop, as in this
example:
while (i
<
5)
{
i++;
x = a.func ();
if (x < 0) break; // jump out of the loop
b.func ();
}
c.func ();
The
break
statement in the loop causes processing to jump immediately to the
following
c.func()
statement. The
b.func()
statement is ignored and no
further loop processing is done, regardless of the value of the text expression.
Sometimes it is necessary to begin a loop, perform only the first portion, skip
the rest, but continue in the loop for further processing. The
continue
statement
does just that, causing the processing in a
for
or
while
loop to skip the rest of
the loop and return back to the start of the loop. An example is
while (i < 5) {
if (a.func ())
continue;
b.func ();
}
Here the processing jumps back to the start of the
while
loop and checks the test
expression if
a.func()
is true. Otherwise, it executes the
b.func()
statement.
In a
do-while
loop a
continue
will cause the processing to skip down to the
while(test)
and execute the test expression.
2.9.3.4 The
switch
statement
The final flow control structure is the
switch
statement:
switch (int expression)
{
case 1: statement1;
case 2: statement2;
default: statement3;
}
Search WWH ::
Custom Search