Java Reference
In-Depth Information
ue
, followed by an identifier for which a matching label must exist. Furthermore, the
label must immediately precede a loop statement.
Labeled continue is useful for breaking out of nested loops while still continuing to
executethelabeledloop.Thefollowingexamplerevealsthelabeledcontinuestatement
terminating the inner for loop's iterations:
outer:
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (i == 1 && j == 1)
continue outer;
else
System.out.println("i="+i+", j="+j);
System.out.println("Both loops terminated.");
When
i
'svalueis1and
j
'svalueis1,
continue outer;
isexecutedtoterminate
theinnerforloopandcontinuewiththeouterforloopatitsnextvalueof
i
.Bothloops
continue until they finish.
The following output is generated:
i=0, j=0
i=0, j=1
i=0, j=2
i=1, j=0
i=2, j=0
i=2, j=1
i=2, j=2
Both loops terminated.
EXERCISES
Thefollowingexercisesaredesignedtotestyourunderstandingofapplicationsand
language fundamentals:
1. Declarean
EchoArgs
classwhose
main()
methodoutputsitscommand-
line arguments, one argument per line. Store this class in a file named
EchoArgs.java
. Compile this source code (
javac
EchoArgs.java
) and run the application; for example,
java
EchoArgs A B C
. You should see each of
A
,
B
, and
C
appearing on a
separate line.