Java break Statement

In this tutorial, you will learn to use break to terminate a loop. Also, you will learn about break labels.

Java break example

The break is a loop control statement which is used to terminate the loop.

Basically break statements are used in the situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.
As soon as the break statement is encountered, it terminates the nearest enclosing loop and control returns from the loop immediately to the first statement after the loop.

Syntax

Syntax of break in for loop:

for (...) {
    if (test-expression) {
        break;
    }
}

If test-expression is evaluated to true, break is executed which terminates the for loop.

Example

public class BreakDemo {

    public static void main(String[] args) {

        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                break;
            }
            System.out.println(i);
        }
    }
}

Output

1
2
3
4

Example explained:

In the above example, when i= 5

  • if condition (i == 5) is checked, yields true. The break statement is executed which terminates the for loop.

Java break example in nested loop

When break is used in the nested loop (loop inside another loop), it terminates the inner loop when it is encountered.

Example

public class BreakDemo {

    public static void main(String[] args) {

        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 2; j++) {
                System.out.println("i = " + i + " and j = " +j);
                if (i == 2)
                    break;
            }
        }
    }
}

Output

i = 1 and j = 1
i = 1 and j = 2
i = 2 and j = 1
i = 3 and j = 1
i = 3 and j = 2

Example explained:

In the above example, when i= 2:

  • i=2 and j=1 gets printed.
  • Then, if condition (i == 2) is checked, yields true. The break statement gets executed which terminates the inner for loop.

Java Labeled break

In Kotlin, labeled break is used to terminate the desired loop when certain condition is satisfied. It can be done with the help of labels.

Label starts with an identifier which is followed by ‘ : ‘. e.g.- inner: , outer: etc.

Syntax

Syntax of labeled break in for loop:

outer:
for (...){
    // code
    inner:
    for (...){
        // code
        if (break condition){
            break outer;
        }
    }
}

Here, outer: is a label marked at the outer for loop.

Now, by using break with a label (break outer in this case), breaks the specific loop(outer loop), when the break condition is evaluated to true.

Example

public class BreakDemo {

    public static void main(String[] args) {

        outer:
        for (int i = 1; i <= 3; i++) {
            inner:
            for (int j = 1; j <= 2; j++) {
                System.out.println("i = " + i + " and j = " + j);
                if (i == 2)
                    break outer;
            }
            System.out.println("Outside inner loop");
        }
        System.out.println("Outside outer loop");

    }
}

Output

i = 1 and j = 1
i = 1 and j = 2
Outside inner loop
i = 2 and j = 1
Outside outer loop

Example explained:

In the above example, when i= 2:

  • if condition (i == 2) is checked, yields true. The break outer is executed which terminates the desired loop marked with label outer: 

In the below program, break terminates the loop marked with label inner: .

Example

public class BreakDemo {

    public static void main(String[] args) {

        outer:
        for (int i = 1; i <= 3; i++) {
            inner:
            for (int j = 1; j <= 2; j++) {
                System.out.println("i = " + i + " and j = " + j);
                if (i == 2)
                    break inner;
            }
            System.out.println("Outside inner loop");
        }
        System.out.println("Outside outer loop");        
    }
}

Output

i = 1 and j = 1
i = 1 and j = 2
Outside inner loop
i = 2 and j = 1
Outside inner loop
i = 3 and j = 1
i = 3 and j = 2
Outside inner loop
Outside outer loop

Example explained:

In the above example, when i= 2:

  • if condition (i == 2) is checked, yields true. The break inner is executed which terminates the loop marked with label inner:

Note: Since, break is used to terminate the innermost loop, in the above program, it is not necessary to use labeled break in this case.

Leave a Reply