Java Break Statement

Author: Tatyana Milkina

The Java break statement can be used in the following cases:

  • To terminate a statement sequence in a switch block, as discussed in the Switch Statement section.
  • It can be used to exit a loop.
  • It can serve as a "civilized" form of the goto statement.

Using break to exit a loop

Let's look at an example of using the Java break statement to exit a while loop. The loop is set to run 100 times, but in case of an unexpected condition, we want to exit early using break.

public class BreakLoopExample1 {
    public static void main(String[] args) {
        int i = 0, n = 100;
        while (i < n) {
            if (i == 10) {
                break;
            }
            System.out.println("i: " + i++);
        }
        System.out.println("Loop finished.");
    }
}

The break statement in Java should not be used as a regular way to exit a loop — that’s the job of the loop condition. Use it only when needed, and only inside loops.

Using break inside nested loops

When multiple loops are nested, the break statement exits only the innermost loop. For example:

public class BreakLoopExample2 {
    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;
                }
                System.out.print(j + " ");
            }
            System.out.println();
        }
        System.out.println("Loops finished");
    }
}

Output of the program:


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 finished

You can use more than one break statement in a Java loop, but it’s not considered good practice.

Java break statement with label

Let’s explore the labeled break in Java. This is often seen as a structured substitute for the goto statement, which Java does not support directly.

Syntax of a labeled break:
break label;

Here, the label is an identifier marking a block of code — this can be any code block or loop. To label a block, place the label name followed by a colon at the beginning of the block.

Labels are valid Java identifiers followed by a colon.

Using labeled break to exit nested loops

The following code uses three labeled blocks: first, second, and third. When break second; is reached, it exits the second block.

public class BreakLoopExample3 {
    public static void main(String[] args) {
        boolean t = true;
        first:
        {
            second:
            {
                third:
                {
                    System.out.println("Before break.");
                    if (t) {
                        break second;
                    }
                    System.out.println("This line won't execute.");
                }
                System.out.println("This line won't execute.");
            }
            System.out.println("After second block.");
        }
    }
}

Program output:


Before break.
After second block.

Labeled break is commonly used to exit inner loops, as shown below:

public class BreakLoopExample4 {
    public static void main(String[] args) {
        outer:
        for (int i = 0; i < 3; i++) {
            System.out.print("Pass " + i + " : ");
            for (int j = 0; j < 100; j++) {
                if (j == 10) {
                    break outer; // exits both loops
                }
                System.out.print(j + " ");
            }
            System.out.println("This line won't be printed.");
        }
    }
}
Курс 'Java для начинающих' на Udemy Курс 'Java для начинающих' на Udemy
Read also:
Comments