Java Break Statement

Break statement in java is used to terminate loops and switch case statements immediately without checking the test expression and the control of the program moves to the next statement following the loop or switch case statement. Use of break statement interrupts the normal execution sequence of loops or switch statement whenever specified condition is true.

We can use break statement to terminate for loop, while loop, do-while loop and swtch case statement.

In case of nested loop, if break statement is encountered inside inner loop then it will only terminate the execution of inner loop. In general, it breaks the execution of loop enclosing it.

Syntax of break statement

break;

Break statement inside for loop

When a break statement is executed inside for loop, it terminates for loop immediately and program control resumes at next statement after for loop.

for (initialization; condition; update) {
    statements;
    if(condition to break) {
        break;
    }
    statements;
}

Break statement in for loop example program

public class ForLoopBreakStatement {
    public static void main(String[] args) {
        int count;
        for (count = 0; count < 10; count++) {
            if (count == 4) {
                break;
            }
            System.out.println(count);
        }
    }
}
Output
0
1
2
3

Break statement inside while loop

When a break statement is executed inside while loop, it terminates while loop immediately and program control resumes at next statement after while loop.

while (condition) {
    statements;
    if(condition to break) {
        break;
    }
    statements;
}

Break statement in while loop example program

public class WhileLoopBreakStatement {
    public static void main(String[] args) {
        int count = 0;
        while (count < 10) {
            if (count == 4) {
                break;
            }
            System.out.println(count);
            count++;
        }
    }
}
Output
0
1
2
3

Break statement inside do-while loop

When a break statement is executed inside do-while loop, it terminates do-while loop immediately and program control resumes at next statement after do-while loop.

do {
    statements;
    if(condition to break) {
        break;
    }
    statements;
} while (condition);

Break statement in do-while loop example program

public class DoWhileLoopBreakStatement {
    public static void main(String[] args) {
        int count = 0;
        do {
            if (count == 4) {
                break;
            }
            System.out.println(count);
            count++;
        } while (count < 10);
    }
}
Output
0
1
2
3

Break statement in nested loop

In case of nested loop, if break statement is encountered it will only terminate the execution of loop enclosing it.

  • If break statement is encountered inside inner loop then it will only terminate the execution of inner loop.
  • If break statement is encountered inside outer loop then it will terminate the execution of outer loop.

public class BreakNestedLoop {
    public static void main(String[] args) {
        int i, j;
        for (i = 0;i < 5;i++) { // outer loop
            for (j = 0;j < 5;j++) { // inner loop
                if (i == j) {
                    System.out.println("i="+ i +",j="+ j);
                    break; // terminates inner loop only
                }
            }
        }
    }
}
Output
i=0,j=0
i=1,j=1
i=2,j=2
i=3,j=3
i=4,j=4

Break statement inside switch statement

When a break statement is encountered inside switch case, it terminates switch case statement and program control resumes at next statement after switch statement block.

The break statement is optional in switch statement. The break statement at the end of each case block cause switch statement to terminate.

public class BreakSwitchStatement {
    public static void main(String[] args) {
        int number = 2;
        switch (number) {
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
                System.out.println("Three");
                break;
            default:
                System.out.println("Default");
        }
    }
}
Output
Two

If break statement is not used inside case code block then all statements below that case block will also get executed until it found a break statement or end of switch statement.

public class NoBreakSwitchStatement {
    public static void main(String[] args) {
        int number = 2;
        switch (number) {
            case 1:
                System.out.println("One");
            case 2:
                System.out.println("Two");
            case 3:
                System.out.println("Three");
            default:
                System.out.println("Default");
        }
    }
}
Output
Two
Three
Default