Java Reference
In-Depth Information
Let's look closely at the program to understand precisely why this output is produced.
When
i
is 1, the first
if
statement succeeds, causing a
break
to the end of the block of code
defined by label
one
. This causes
After block one
. to print. When
i
is 2, the second
if
suc-
ceeds, causing control to be transferred to the end of the block labeled by
two
. This causes
the messages
After block two
. and
After block one
. to be printed, in that order. When
i
is
3, the third
if
succeeds, and control is transferred to the end of the block labeled by
three
.
Now, all three messages are displayed.
Here is another example. This time,
break
is being used to jump outside of a series of
nested
for
loops. When the
break
statement in the inner loop is executed, program control
jumps to the end of the block defined by the outer
for
loop, which is labeled by
done
. This
causes the remainder of all three loops to be bypassed.