Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywords

break Keyword in Java

The break keyword in Java is used to terminate the execution of a loop or switch statement prematurely. When a break statement is encountered, control is transferred to the statement immediately following the enclosing loop or switch.

Usage

The break statement is commonly used in the following scenarios:

  • To exit a loop (e.g., for, while, do-while) before it has iterated over all elements.
  • To terminate a switch statement once a matching case has been executed.

Syntax

break;

Examples

Example 1: Using break in a for Loop

public class BreakInForLoop {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                break; // Terminate loop when i is 5
            }
            System.out.println("i: " + i);
        }
    }
}

In this example, the for loop iterates from 0 to 9. However, when i equals 5, the break statement is executed, terminating the loop early.

Example 2: Using break in a while Loop

public class BreakInWhileLoop {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            if (i == 5) {
                break; // Terminate loop when i is 5
            }
            System.out.println("i: " + i);
            i++;
        }
    }
}

Here, the while loop continues as long as i is less than 10. When i reaches 5, the break statement terminates the loop.

Example 3: Using break in a switch Statement

public class BreakInSwitch {
    public static void main(String[] args) {
        int day = 3;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break; // Terminate switch after matching case
            case 4:
                System.out.println("Thursday");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}

In this example, the switch statement checks the value of day. When day equals 3, "Wednesday" is printed, and the break statement terminates the switch statement, preventing the execution of subsequent cases.

Example 4: Using break in a do-while Loop

public class BreakInDoWhileLoop {
    public static void main(String[] args) {
        int i = 0;
        do {
            if (i == 5) {
                break; // Terminate loop when i is 5
            }
            System.out.println("i: " + i);
            i++;
        } while (i < 10);
    }
}

In this do-while loop, the loop will continue as long as i is less than 10. When i equals 5, the break statement will terminate the loop early.

Tips and Best Practices

  • Use Judiciously: While break can make loops and switch statements more efficient, overuse can make code harder to read and maintain.
  • Nested Loops: In nested loops, a break statement will only terminate the innermost loop. Use labeled break statements if you need to exit multiple levels of nested loops.
    outerLoop:
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (i == 2 && j == 2) {
                break outerLoop; // Terminates both loops
            }
            System.out.println("i: " + i + ", j: " + j);
        }
    }
  • Avoid Spaghetti Code: Using break statements excessively can lead to code that is difficult to follow. Always aim for clear and maintainable code.
  • Switch Cases: Always use break in each case of a switch statement unless you intentionally want to fall through to the next case.