Basic Loop Jump Statement: Break and Continue

Jump statements are used to transfer the control from one section of the program to another section as desired by the programmer. Java supports three jump statements- break, continue and return. Out of these three two jump statements, i.e. break and continue are mainly used in loops. Return is used to transfer control from the called method to the calling method.

The ‘break’ statement

The break statement usually takes control from the inside of block to the next statement on the outside of the block. It is used to terminate cases in a switch case statement. Without the break statement, the control will simply flow into the next case without terminating after the case is executed. In addition to this, break finds two other important uses in Java loop. These are given below:

1) To exit a loop

The break statement inside a loop forces the control to go out of the loop to the following statement, i.e. it terminates the loop or allows the control to exit the loop. One important thing to be kept in mind while using nested loops is that if the break is present inside the inner loop, it will only terminate the inner loop and not the outer loop. Break only takes the control out of the block where it is present. The break statement can be used in any of the three loops, i.e. for, while and do-while loops. Also, we can use more than one break statement inside the loop if need be. Here is a program which shows the use of break inside a nested for loop.

/*This program shows the use of break in a nested for loop. 
This program prints a simple pattern*/

public class TestBreak {

    public static void main(String args[]) {
        int i, j;

        for (i = 1; i <= 5; i++) { //outer loop
            for (j = 1; j <= 5; j++) { //inner loop
                if (j <= i) {
                    System.out.print(j + " ");
                } else {
                    break;//break terminates the inner loop as it is present inside the inner loop block.
                }
            }

            System.out.println();
        }
    }
}

Output:-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

2) As ‘goto’

Java does not have a goto expression. To compensate for that break statement can be used instead of goto in a special manner. By this expanded form of break statement, you can easily break out any nest of block and take the control flow to any part of the code as desired. This is achieved by the use of labels in the code. Its form is shown below:

break label;

This label is put on another block which contains a piece of the code. The label block must contain the break statement as it is used to break out of the labeled loop. The control then resumes from the statement after the labeled block. It is usually used when handling nested loops. An example of using the break as goto is shown below:

/* This program shows the use of labelled break statement. */

public class TestBreakLabel {

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

        outer:
        for (i = 0; i < 5; i++) {
            System.out.println("Inside first loop");

            for (j = 0; j < 5; j++) {
                System.out.println("Inside second loop");
                if (t) {
                    break outer;
                }
            }

            System.out.println("This won't get printed");
        } //the outer block ends here.

        System.out.println("After break");
    }
}

Output:-
Inside first loop
Inside second loop
After break

The Continue Statement

The continue statement is usually used to call for an early iteration or to skip iterations. The control is transferred to the update and then to conditional expression of the loop in case of for loop. But in the case of while or do while loop the control goes to the conditional expression of the loop. An example of continue statement is given below:

/*This program shows the use of continue in loops.
It prints the odd numbers in a pattern and skips the even numbers.*/

public class TestContinue {
    public static void main(String args[]) {
        int i, j;

        for (i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                continue; //skips all the even iterations
            } else {
                for (j = 1; j <= i; j++) {
                    System.out.print(j);
                }
            }
            System.out.println();
        }
    }
}


Output:-
1
123
12345
1234567 
123456789 

Like break statement, continue statement can also be used with a label. The label defines to which loop the control goes or which loop continues. The continues statement must be enclosed within the label. An example is shown below:

/*This program shows the use of continue with label.
It prints a triangular multiplication table from 0 to 9*/

public class TestContinueLabel {
    public static void main(String args[]) {
        int i, j;

        outer:
        for (i = 0; i < 10; i++) {
            for (j = 0; j < 10; j++) {
                if (j > i) {
                    System.out.println();
                    continue outer; //goes back to the outer loop and continues the next iteration
                }
                System.out.print((i * j) + " ");
            }
        }

        System.out.println();
    }
}

Output:-
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

An investment in knowledge always pays the best interest. Hope you like the tutorial. Do come back for more because learning paves way for a better understanding.

Do not forget to share and Subscribe.

Happy coding!! 🙂

Recommended -