Home Java Tutorial Java Break Statement with examples

Java Break Statement with examples

by Anup Maurya
28 minutes read

In this tutorial, you will learn about the break statement, labeled break statement in Java with the help of examples.

In Java, the break statement is used to immediately terminate the current loop, switch statement or labeled block. It is a powerful control statement that allows you to exit a loop prematurely based on some condition. In this tutorial, we’ll discuss the break statement in detail with some examples.

Syntax of the break statement

The break statement has the following syntax:

break;

The break statement is used inside loops, switch statements, or labeled blocks. When the break statement is executed, the control flow immediately jumps to the next statement after the loop, switch or labeled block.

Example: Using the break statement inside a loop

The break statement is often used inside loops to terminate the loop when a certain condition is met. Here’s an example that uses a while loop to print out the numbers 1 to 10, but stops when the number 5 is reached:

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

In this example, the loop iterates from 1 to 10, and the if statement checks whether i is equal to 5. If i is equal to 5, the break statement is executed, which immediately terminates the loop. The output of this program would be:

1
2
3
4
5

Example: Using the break statement inside a switch statement

The break statement can also be used inside a switch statement to immediately terminate the switch and continue with the next statement after the switch. Here’s an example that uses a switch statement to determine the number of days in a given month:

int month = 2;
int days = 0;

switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        days = 31;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        days = 30;
        break;
    case 2:
        days = 28;
        break;
    default:
        System.out.println("Invalid month");
        break;
}

System.out.println("Days in month " + month + ": " + days);

In this example, the switch statement checks the value of the variable month. Depending on the value of month, the switch statement sets the variable days to the number of days in the month. The break statement is used after each case to immediately terminate the switch and continue with the next statement after the switch. If none of the cases match the value of month, the default case is executed, which prints out an error message.

Conclusion

In this tutorial, we discussed the break statement in Java, which is used to immediately terminate the current loop, switch statement, or labeled block. We looked at some examples of how to use the break statement inside a loop and a switch statement. By using the break statement, you can write more efficient and powerful programs in Java.

related posts

Leave a Comment

Enable Notifications OK No thanks