Jump Statements - Java

Java supports three jump statements: break, continue, and return. These statements transfer control to another part of your program. Each is examined here.

Jump
Statements

Java supports three jump
statements: break, continue, and return. These statements transfer control to another part of your
program. Each is examined here.

Using
break

In Java, the break statement has three uses. First,
as you have seen, it terminates a statement sequence in a switch statement. Second, it can be used to exit a loop. Third, it
can be used as a “civilized” form of goto. The last two uses are explained
here.

Using
break to Exit a Loop

By using break, you can force immediate termination of a loop, bypassing the
conditional expression and any remaining code in the body of the loop. When a break statement is encountered inside a
loop, the loop is terminated and program control resumes at the next statement
following the loop. Here is a simple example:

// Using break to exit a
loop.

class BreakLoop {

public static void
main(String args[]) { for(int i=0; i<100; i++) {

if(i == 10) break; //
terminate loop if i is 10

System.out.println("i: " + i);

}

System.out.println("Loop complete.");

}

}

This program generates the
following output:

i: 0 i: 1 i: 2 i: 3 i: 4 i: 5
i: 6 i: 7 i: 8 i: 9

Loop complete.

As you
can see, although the for loop is
designed to run from 0 to 99, the break
statement causes it to terminate early, when i equals 10.

The break statement can be used with any of
Java’s loops, including intentionally infinite loops. For example, here is the
preceding program coded by use of a while
loop. The output from this program is the same as just shown.

// Using break to exit a
while loop.

class BreakLoop2 {

public static void
main(String args[]) { int i = 0;

while(i < 100) {

if(i == 10) break; //
terminate loop if i is 10

System.out.println("i: " + i);

i++;

}

System.out.println("Loop complete.");

}

}

When
used inside a set of nested loops, the break
statement will only break out of the innermost loop. For example:

// Using break with nested
loops.

class BreakLoop3 {

public static void
main(String args[]) { for(int i=0; i<3; i++) {

System.out.print("Pass
" + i + ": "); for(int j=0; j<100; j++) {

if(j == 10) break; //
terminate loop if j is 10

System.out.print(j + " ");

}

System.out.println();

}

System.out.println("Loops
complete.");

}

}

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, the break statement in the
inner loop only causes termination of that loop. The outer loop is unaffected.

Here are two other points to
remember about break. First, more
than one break statement may appear
in a loop. However, be careful. Too many break
statements have the tendency to destructure your code. Second, the break that terminates a switch statement affects only that switch statement and not any enclosing
loops.

Using
break as a Form of Goto

In addition to its uses with
the switch statement and loops, the break statement 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 the break statement. By
using this form of break, you can,
for example, break out of one or more blocks of code. These blocks need not be
part of a loop or a switch. They can
be any block. Further, you can specify precisely where execution will resume,
because this form of break works
with a label. As you will see, break
gives you the benefits of a goto without its problems.

The
general form of the labeled break
statement is shown here: break label;

Most
often, label is 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 break executes, control is transferred
out of the named block. The labeled block must enclose the break statement, but it does not need to be the immediately
enclosing block. This means, for example, that you can use a labeled break statement to exit from a set of
nested blocks. But you cannot use break
to transfer control out of a block that does not enclose the break statement.

To name a block, put a label
at the start of it. A label is 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 break
statement. Doing so causes execution to resume at the end of the labeled block. For example, the following program shows
three nested blocks, each with its own label. The break statement causes execution to jump forward, past the end of
the block labeled second, skipping
the two println( ) statements.

// Using break as a civilized
form of goto.

class Break {

public static void
main(String args[]) { boolean t = true;

first: { second: {

third: {

System.out.println("Before the
break.");

if(t) break second; // break
out of second block

System.out.println("This won't execute");

}

System.out.println("This won't
execute");

}

System.out.println("This is after second
block.");

}

}

}

Running this program
generates the following output:

Before the break.

This is after second block.

One of the most common uses
for a labeled break statement is to
exit from nested loops. For example, in the following program, the outer loop
executes only once:

As you
can see, when the inner loop breaks to the outer loop, both loops have been
terminated. Notice that this example labels the for statement, which has a block of code as its target.

Keep in
mind that you cannot break to any label which is not defined for an enclosing
block. For example, the following program is invalid and will not compile:

// This program contains an error.

class BreakErr {

public static void main(String args[]) {

one: for(int i=0; i<3;
i++) {

System.out.print("Pass " + i + ": ");

}

for(int j=0; j<100; j++) {

if(j == 10) break one; //
WRONG

System.out.print(j + " ");

}

}

}

Since
the loop labeled one does not
enclose the break statement, it is
not possible to transfer control out of that block.

Using
continue

Sometimes it is useful to
force an early iteration of a loop. That is, you might want to continue running
the loop but stop processing the remainder of the code in its body for this
particular iteration. This is, in effect, a goto just past the body of the
loop, to the loop’s end. The continue
statement performs such an action. In while
and do-while loops, a continue statement causes control to be
transferred directly to the conditional expressionthat controls the loop. In a for
loop, control goes first to the iteration portion of the for statement and then to the conditional expression. For all three
loops, any intermediate code is bypassed.

Here is an example program
that uses continue to cause two
numbers to be printed on each line:

This code uses the % operator to check if i is even. If it is, the loop continues
without printing a newline. Here is the output from this program:

0 1

2 3

4 5

6 7

8 9

As with
the break statement, continue may specify a label to
describe which enclosing loop to continue. Here is an example program that uses
continue to print a triangular
multiplication table for 0 through 9:

The continue statement in this example terminates the loop counting j and continues with the next iteration
of the loop counting i. Here is the
output of this program:

0

0 1

0 2 4

0 3 6 9

0 4 8 12 16

0 5 10 15
20 25

0 6 12 18
24 30 36

0 7 14 21
28 35 42 49

0 8 16 24
32 40 48 56 64

0 9 18 27
36 45 54 63 72 81

Good uses of continue are rare. One reason is that
Java provides a rich set of loop statements which fit most applications.
However, for those special circumstances in which early iteration is needed,
the continue statement provides a
structured way to accomplish it.

return

The last control statement is
return. The return statement is used to explicitly return from a method. That
is, it causes program control to transfer back to the caller of the method. As
such, it is categorized as a jump statement. Although a full discussion of return must wait until methods are
discussed in Chapter 6, a brief look at return
is presented here.

At any
time in a method, the return
statement can be used to cause execution to branch back to the caller of the
method. Thus, the return statement
immediately terminates the method in which it is executed. The following
example illustrates this point. Here, return
causes execution to return to the Java run-time system, since it is the
run-time system that calls main( ):

// Demonstrate return.

class
Return {

public static void
main(String args[]) { boolean t = true;

System.out.println("Before the
return.");

if(t) return; // return to caller

System.out.println("This won't
execute.");

}

}

The output from this program
is shown here:

Before the return.

As you
can see, the final println( )
statement is not executed. As soon as return
is executed, control passes back to the caller.

One last point: In the preceding program, the if(t) statement is necessary. Without
it, the Java compiler would flag an “unreachable code” error because the
compiler would know that the last println(
) statement would never be executed. To prevent this error, the if statement is used here to trick the
compiler for the sake of this demonstration.