break statement in java & break statement example

Understanding the Java break Statement

The break statement is one of the most essential control-flow statements in Java, and it lets a programmer interrupt the normal sequence of execution. You will typically see it inside loops and switch statements. In practice, the break keyword serves two main purposes: it can exit a loop, and it can also be used within a switch statement.

Using break in a Java for loop

When the JVM encounters a break statement inside a loop, it stops that loop right away and moves program control to the statement that follows the loop. It is commonly paired with an if statement inside the loop: once the condition evaluates to true, the break statement ends the loop immediately. The syntax is simply break;, and it works in for, while, and do-while loops.

There is also a Hindi-language video walkthrough available for this topic.

Watch the Hindi Video

Example: break inside a for loop

In the sample program below, the loop is set to run from 1 to 10, but once the variable i reaches 5, the break statement inside the if block ends the loop. Control then passes to the statement outside the loop, which prints its message.

class ExampleBreakStaementInForLoop {
    public static void main(String args[]) {
        for(int i = 1 ; i <= 10 ; i++) {
            System.out.println(i);
            if(i == 5)
                break;
        }
        System.out.println("After the for loop");
    }
}

Output: 1 2 3 4 5, followed by "After the for loop".

Break statement in a nested for loop

Break statements also work inside nested loops. When a break appears in a nested loop, only the inner loop is terminated; the outer loop keeps running.

class ExampleOfBreak {
    public static void main(String args[]) {
        for(int i = 1 ; i <= 3 ; i++) {
            System.out.println("Before start of inner loop and value of i = "+ i);
            for(int j = 1; j <= 3 ; j++) {
                System.out.println("Inside the for loop and value of j ="+ j);
                if(j == 2)
                    break;
            }
        }
        System.out.println("After the for loop");
    }
}

Explanation: two for loops are running here. The break statement sits inside the inner loop and triggers when the inner loop value reaches 2, so the inner loop ends and control returns to the outer loop.

Labeled break in a for loop

By attaching a label to a break statement, you can terminate any loop you choose, whether it is the outer loop or an inner one. This capability has been available since JDK 1.5.

public class BreakWithLabledExample3 {
    public static void main(String[] args) {
        outer:
        for(int i = 1 ; i <= 3 ; i++) {
            System.out.println("Before start of the inner loop");
            inner:
            for(int j = 1; j <= 3; j++) {
                System.out.println("inside the inner loop and Value of j = "+j);
                if( j ==2) {
                    //using break statement with label
                    break outer;
                }
            }
        }
    }
}

Output: "Before start of the inner loop", then "inside the inner loop and Value of j = 1", then "inside the inner loop and Value of j = 2".

Break in a switch statement

To see a worked example of break used with a switch statement, explore the dedicated guide.

Switch Statement in Java

You may also find the if statement tutorial helpful for building the conditions that trigger a break.

If Statement in Java

Keep Exploring JavaGoal

Return to the JavaGoal homepage for more Java tutorials and examples.

JavaGoal Home

Have a question or a comment about this lesson? Share your thoughts below.

Leave a Comment