Java

Java - Using Continue and Return

  • Java
  • 3 mins read

In this tutorial, you will learn about Java continue and return statements.

Using continue in Java

The continue statement does such an action. In while and do-while loops, a continue statement effects control to be transferred directly to the conditional expression that controls the loop.

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

// Demonstrate continue.
class Continue {
public static void main(String args[]) {
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if (i%2 == 0) continue;
System.out.println("");
} 
}
}

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

01 23 45 67 89

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

// Using continue with a label.
class ContinueLabel {
public static void main(String args[]) {
outer: for (int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
if(j > i) {
System.out.println();
continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}

The continue statement in this example exits the loop counting j and continues with the next iteration of the loop counting i.

Here is the output of this program:

0
01
02 4
036 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

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

Using return in Java

The last control statement is return. The return statement is employed to explicitly return from a method. That is, it initiates program control to transfer back to the caller of the method. As such, it is named as a jump statement.

The following sample illustrates this point.

Here, return makes execution to return to the Java run-time system because 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 result of 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 above program, the if(t) statement is essential. Without it, the Java compiler would flag an “unreachable code” error since the compiler would know that the last println( ) statement would never be executed. To counter this error, the if statement is used here to trick the compiler for the sake of this explanation.