The break statement in Java with examples

The ability to control the flow of execution of their code is an essential tool for any programmer. The "break" statement in Java is one tool that can be used to control this flow. You can use "break" to halt the execution of a loop or switch statement and jump to the next statement outside of the loop or switch. In this article, we'll go over the "break" statement in depth and show you how to use it to write more robust and flexible Java code.

What is the "break" statement used for in Java?

A "break statement" in Java is used to exit a loop or switch statement before its normal termination condition is met. Java's break statement is a reserved keyword that is used in conjunction with a for loop, while loop, do-while loop, or switch statement.

Java break statement syntax

The general form for using the "break" statement in Java is as follows:

break;

only the "break" keyword and followed by a semicolon, which makes it a "break" statement.

Java break statement example

Consider the following Java program demonstrating the "break" statement:

Java Code
public class BreakStatementExample {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            System.out.println("The value of i is: " + i);
            if (i == 5) {
                break;     // stop the loop when "i" is 5
            }
        }
    }
}
Output
The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
The value of i is: 5

The program demonstrates the use of the "break" statement in a "for" loop in Java. The "for" loop starts with an initialization statement "int i = 1," and then checks the condition "i < 10" before executing the loop body. Inside the loop body, the program prints the value of "i" using System.out.println("The value of i is: " + i) statement. Then, it checks if "i" is equal to 5 using the "if (i == 5)" statement. If this condition is true, the "break" statement is executed which terminates the loop and control is transferred to the next statement after the loop. Therefore, the program will print the value of "i" from 1 to 5 and then terminate the loop.

Here's the same program made with the "while" loop to show how the Java "break" statement works.

public class BreakStatementExample {
    public static void main(String[] args) {
        int i = 1;
        while (i < 10) {
            System.out.println("The value of i is: " + i);
            if (i == 5) {
                break;
            }
            i++;
        }
    }
}

Let me create one more example using the "do-while" loop, this time.

public class BreakStatementExample {
    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.println("The value of i is: " + i);
            if (i == 5) {
                break;
            }
            i++;
        } while (i < 10);
    }
}

Now the following program demonstrates the use of the "break" statement in the "switch" case in Java.

Java Code
public class BreakStatementExample {
    public static void main(String[] args) {
        int day = 4;
        switch(day){
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            default:
                System.out.println("Weekend");
        }
    }
}
Output
Thursday

The value 4 is assigned to the variable "day." The "switch" statement compares the value of the "day" variable to various cases. In each case, the System.out.println() statement is used to print a message to the console. The "break" statement is used to exit the "switch" statement and prevent the execution of subsequent cases after executing the statement corresponding to the matching case. Finally, if none of the cases match, the "default" case is run, and the message "Weekend" is displayed on the console.

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!