Java Break, Continue and return

break, continue and return are branching statements in Java. Each of these statement has their importance while doing programming in Java.

1. break statement

  • 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 the example program illustrating break statement.

// Using break to exit a loop. 
class BreakLoopExample {
    public static void main(String args[]) {
        for (int i = 0; i < 100; i++) {
            if (i * 10 == 50)
                break; // terminate loop if i is 5 
            System.out.println("i: " + i); 
        } 
        System.out.println("Loop completed."); 
    } 
}

The ouput of the program is:

i: 0 
i: 1 
i: 2 
i: 3 
i: 4 
Loop completed.

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

// Using break statement with nested loops. 
class BreakNestedLoop {
    public static void main(String args[]) {
        for (int i = 0; i < 3; i++) {
            System.out.print("Iteration " + i + ": ");
            for (int j = 0; j < 10; j++) {
                if (j == 5) 
                      break; // terminate loop if j is 5 
                System.out.print(j + " ");
            }
            System.out.println();
        }
        System.out.println("Loops completed.");
    }
}

The ouput of the program is:

Iteration 0: 0 1 2 3 4 
Iteration 1: 0 1 2 3 4  
Iteration 2: 0 1 2 3 4 
Loops completed.

2. continue statement

There may be situation in which you might want to continue running the loop but stop processing the remainder of the code in its body for this particular iteration. In other words, sometimes it is useful to force an early iteration of a loop. The continue statement performs such an action.

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

// Demonstrate continue. 
class ContinueExample {
    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("");
        }
    }
}

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

0 1 
2 3 
4 5 
6 7 
8 9

3. return statement

The return statement exits from the current method, and control flow returns to where the method was invoked.

Types:

1. return statement that returns a value. To return a value, simply put the value (or an expression that calculates the value) after the return keyword.

return ++count;

2. return statement that doesn't return a value. The data type of the returned value must match the type of the method's declared return value. When a method is declared void, use the form of return that doesn't return a value.

return;
Java Iteration Statements
Java Arrays