Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

100 Part I: The Java Language


This program generates the following output:

Pass 0: 0 1 2 3 4 5 6 7 8 9
Pass 1: 0 1 2 3 4 5 6 7 8 9
Pass 2: 0 1 2 3 4 5 6 7 8 9
Loops complete.

As you can see, thebreakstatement in the inner loop only causes termination of that loop.
The outer loop is unaffected.
Here are two other points to remember aboutbreak. First, more than onebreakstatement
may appear in a loop. However, be careful. Too manybreakstatements have the tendency
to destructure your code. Second, thebreakthat terminates aswitchstatement affects only
thatswitchstatement and not any enclosing loops.

REMEMBEREMEMBER breakwas not designed to provide the normal means by which a loop is terminated.
The loop’s conditional expression serves this purpose. Thebreakstatement should be used to
cancel a loop only when some sort of special situation occurs.

Using break as a Form of Goto
In addition to its uses with theswitchstatement and loops, thebreakstatement can also be
employed by itself to provide a “civilized” form of the goto statement. Java does not have a
goto statement because it provides a way to branch in an arbitrary and unstructured manner.
This usually makes goto-ridden code hard to understand and hard to maintain. It also prohibits
certain compiler optimizations. There are, however, a few places where the goto is a valuable
and legitimate construct for flow control. For example, the goto can be useful when you are
exiting from a deeply nested set of loops. To handle such situations, Java defines an expanded
form of thebreakstatement. By using this form ofbreak, you can, for example, break out of
one or more blocks of code. These blocks need not be part of a loop or aswitch. They can be
any block. Further, you can specify precisely where execution will resume, because this form
ofbreakworks with a label. As you will see,breakgives you the benefits of a goto without its
problems.
The general form of the labeledbreakstatement is shown here:

breaklabel;

Most often,labelis the name of a label that identifies a block of code. This can be a stand-alone
block of code but it can also be a block that is the target of another statement. When this form of
breakexecutes, control is transferred out of the named block.The labeled block must enclose
thebreakstatement, but it does not need to be theimmediately enclosing block. This means,
for example, that you can use a labeledbreakstatement to exit from a set of nested blocks.
But you cannot usebreakto transfer control out of a block that does not enclose thebreak
statement.
To name a block, put a label at the start of it. Alabelis any valid Java identifier followed
by a colon. Once you have labeled a block, you can then use this label as the target of a
breakstatement. Doing so causes execution to resume at theendof the labeled block. For
example, the following program shows three nested blocks, each with its own label. The
breakstatement causes execution to jump forward, past the end of the block labeledsecond,
skipping the twoprintln( )statements.
Free download pdf