Java Reference
In-Depth Information
: "no employee ID matches "+em-
ployeeSearchID);
TheexampleusesforandiftosearchanarrayofemployeeIDstodeterminewhether
aspecificemployeeIDexists.IfthisIDisfound,if'scompoundstatementassigns true
to found .Becausethereisnopointincontinuingthesearch,itthenusesbreaktoquit
the loop.
The labeled break statement transfers execution to the first statement following the
loopthat'sprefixedbya label (anidentifierfollowedbyacolon).Itconsistsofreserved
word break , followed by an identifier for which the matching label must exist. Fur-
thermore, the label must immediately precede a loop statement.
Labeledbreakisusefulforbreakingoutof nested loops (loopswithinloops).Thefol-
lowing example reveals the labeled break statement transferring execution to the first
statement that follows the outer for loop:
outer:
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (i == 1 && j == 1)
break outer;
else
System.out.println("i="+i+", j="+j);
System.out.println("Both loops terminated.");
When i 's value is 1 and j 's value is 1, break outer; is executed to terminate
both for loops. This statement transfers execution to the first statement after the outer
forloop,whichhappenstobe System.out.println("Both loops termin-
ated."); .
The following output is generated:
i=0, j=0
i=0, j=1
i=0, j=2
i=1, j=0
Both loops terminated.
Search WWH ::




Custom Search