Java break Statement



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

While working with loops, we sometimes need to skip statements inside the loop or terminate the loop immediately without checking the test condition.


Java break Statement

The break statement is used to terminate a loop immediately, and the program's control moves to the next statement following the loop.

The break statement also can be used with decision-making Java if-else Statement.

The syntax of the break statement can be given as follows:

break;

How break statement works?

The following illustration demonstrates how the break statement works with different loops.

Flowchart of the Java break Statement

Example: Java break statement with a for loop

In the following example, we will use the break statement with a for loop.

public class Main {
    public static void main(String[] args) {
        
        for(int i = 0;  i <= 8; i++) {
            
            // if the value of i is 4 the loop terminates
            if(i == 4) {
                break;
            }
            System.out.println(i);
        }
    }
}

Output:

0
1
2
3

In the above example, we used the for loop to print the value of i in each iteration.

When the value of i is equal to 4, the loop terminates by the break statement.

if(i == 4){
    break;
}

Example: Java break statement with a while loop

In the example below, we will calculate the sum of numbers entered by the user until the user enters a negative number.

We will use the Scanner class to take input from the user.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {

    int number, sum = 0;

    // creating an object of Scanner class
    Scanner input = new Scanner(System.in);

    while(true) {
      // taking integer input from the user
      System.out.println("Enter a positive number : ");
      number = input.nextInt();

      // if the number is negative the loop terminates
      if(number < 0) {
        break;
      }

      sum +=  number;
    }

    System.out.println("Sum = " + sum);
  }
}

Output:

Enter a positive number : 
7
Enter a positive number : 
23
Enter a positive number : 
8
Enter a positive number : 
-5
Sum = 38

In the above program, the test condition of the while loop is always true, and using the ' break ' statement, we can exit from the while loop.

if(number < 0) {
    break;
}

Java break with Nested Loop

Using the break statement with nested loops terminates the innermost loop.

Working of break statement with nested loops

As we can see above, the break terminates the innermost while loop, and the control jump to the outer loop (upper loop).


Labeled break statement

We have already used the unlabeled break statement, which terminates the innermost loop statement and switch statement. There is another form of break statement known as the labeled break.

The labeled break statement can be used to terminate the outermost loop.

In the following illustration, we can see how the labeled break statement works.

Working of the labeled break Statement in Java

As we can see in the above illustration, we have used the label identifier to specify the outer loop. And we can see how we used the break statement with the label break label;.

Here the break statement terminates the labeled statement (outer loop). And, the control of the program moves to the statement after the labeled statement.

Let us have another example:

while(testCondition) {
    // code
    here:
    while(testCondition) {
        // code
        while(testCondition) {
            //code
            break here;
        }
    }
    // control moves here
}

In the above program, when the statement break here; is executed, the while loop labeled as here is terminated. Then, the program's control moves to the statement after the second while loop.


Example: labeled break statement

In the following example, we will use the labeled break statement with for loop.

public class Main{
    public static void main(String[] args) {
        
        first:
        for(int i = 0; i < 4; i++) {
            
            second:
            for(int j = 0; j < 2; j++) {

                System.out.println("i = " + i + "; j = " + j);

                if (i == 1) {
                    break second;
                }
            }
        }
    }
}

Output

i = 0; j = 0
i = 0; j = 1
i = 1; j = 0
i = 2; j = 0
i = 2; j = 1
i = 3; j = 0
i = 3; j = 1

Here above, the labeled break statement is used to terminate the loop labeled as "second".



ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.