Java Reference
In-Depth Information
10.7.
break
A
break
statement can be used to exit from any block, not just from a
switch
. There are two forms of
break
statement. The
unlabeled break:
break;
and the
labeled break:
break
label
;
An unlabeled
break
terminates the innermost
switch
,
for
,
while
, or
do
statementand so can appear only within one of those statements. A
labeled
break
can terminate any labeled statement.
A
break
is most often used to break out of a loop. In this example, we
are looking for the first empty slot in an array of references to
Contained
objects:
class Container {
private Contained[] objs;
// ...
public void addIn(Contained obj)
throws NoEmptySlotException
{
int i;
for (i = 0; i < objs.length; i++)
if (objs[i] == null)
break;
if (i >= objs.length)
throw new NoEmptySlotException();
objs[i] = obj; // put it inside me