Using The break Statement in Java

Java

In Java, there is 2 types of break statement: unlabeled break statement and labeled break statement.

The unlabeled break statement is used to terminate and exit the for loop, enhanced for loop, while loop, do-while loop and switch statement. It will skip the rest of the statements in the loop body and continue after the enclosing statement.

The labeled break statement is used to terminate the labeled statement.

In this tutorial, we’ll show you some examples of how to use the unlabeled and labeled break statement in Java.

What You’ll Need

break in for Loop

This is the code snippet of using unlabeled break statement in for loop.

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

Output:

1
2
3
4
5

break in Enhanced for Loop

This is the code snippet of using unlabeled break statement in enhanced for loop.

int[] counts = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int count: counts) {
    if (count == 6) {
        break;
    }
    System.out.println(count);
}

Output:

1
2
3
4
5

break in while Loop

This is the code snippet of using unlabeled break statement in while loop.

int count1 = 1;
while (count1 <= 10) {
    if (count1 == 6) {
        break;
    }
    System.out.println(count1);
    count1++;
}

Output:

1
2
3
4
5

break in do-while Loop

This is the code snippet of using unlabeled break statement in do-while loop.

int count2 = 1;
do {
    if (count2 == 6) {
        break;
    }
    System.out.println(count2);
    count2++;
} while (count2 <= 10);

Output:

1
2
3
4
5

break in switch Statement

This is the code snippet of using unlabeled break statement in switch statement.

String day = "Thursday";
switch (day) {
    case "Monday":
    case "Tuesday":
    case "Wednesday":
    case "Thursday":
    case "Friday":
        System.out.printf("%s is weekday%n", day);
        break;
    case "Saturday":
    case "Sunday":
        System.out.printf("%s is weekend%n", day);
        break;
    default:
        System.out.printf("%s is an invalid day%n", day);
}

Output:

Thursday is weekday

Note: Beginning with Java 7, we can pass String type argument to switch statement to compare the value.

Labeled break Statement

This is the code snippet of using labeled break statement to terminate and exit the outer for loop.

LabeledBreakStatementExample.java

public class LabeledBreakStatementExample {

    public static void main(String[] args) {

        outerLoop: 
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5; j++) {
                if (i == 3 && j == 3) {
                    break outerLoop;
                }
                System.out.printf("i = %d, j = %d%n", i, j);
            }
        }

    }

}

Output:

i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 1, j = 4
i = 1, j = 5
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 2, j = 4
i = 2, j = 5
i = 3, j = 1
i = 3, j = 2

Summary

In this tutorial, we have shown you several examples of using unlabeled and labeled break statement in Java.

You can get the example codes of this tutorial on GitHub.