Java Break


JavaViews 1873

We use Java break statement to exit or terminate from any of the loop execution based on a certain condition. We mainly use java break for loop control statements like for, while, and do-while loops. Another main usage is in java switch-case statement.

Syntax

In java, break is a keyword and it is used followed by a semicolon(;).

break;

Flowchart

java break

Java break in for loop

When we use java break statement in for loop, it exits the loop when a particular condition is met. In the below example, even though for loop has 6 iterations starting from i=0 to i=5 when i=4, it executes the break statement and terminates the for loop. This is the reason that the output does not display the values i=4 and i=5.

public class breakForLoop {

  public static void main(String[] args) {
    for(int i=0;i<=5;i++) {
      if(i==4) {
        System.out.println("Exit for loop");
        break;
      }
        
      System.out.println("i: " + i);
    }

  }

}
i: 0
i: 1
i: 2
i: 3
Exit for loop

Java break in nested for loop

When we use java break in nested for loop, especially in the inner loop, it exits the inner loop and continues with the outer loop execution.

In the below example, when i=2 and j=3, it executes the break statement and exits the inner loop for that iteration and continues the execution of the outer loop for the next iteration. Hence the output does not display the values i:2 j=3, i=2 j=4, and i=2 j=5.

public class breakNestedFor {

  public static void main(String[] args) {
    for(int i=0;i<=3;i++) {
      for(int j=0;j<=5;j++) {
        if(i==2 & j==3) {
          System.out.println("Exit inner for loop");
          break;
        }
        System.out.println("i:" + i + " " + "j:" + j);
      }
      
    }

  }

}
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
Exit inner for loop
i:3 j:1
i:3 j:2
i:3 j:3
i:3 j:4
i:3 j:5

Java break in Labeled for loop

We can use java break in labeled for loop by specifying the loop name. Whichever loop name we specify, it exits that particular loop.

In the below example, we have used break loop1, which means when i=2 and j=2, it stops the execution of outer loop loop1. Hence when it reaches this condition, the entire loop execution stops.

public class JavabreakLabeledFor {

  public static void main(String[] args) {
    loop1:
    for(int i=1;i<=3;i++) {
      loop2:
      for(int j=1;j<=3;j++) {
        if(i==2 && j==2) {
          System.out.println("Exit labeled loop");
          break loop1;
        }
        System.out.println("i:" + i + " " + "j:" + j);
      }
    }

  }

}
i:1 j:1
i:1 j:2
i:1 j:3
i:2 j:1
Exit labeled loop

Now suppose, we specify break loop2, it stops the execution of the loop2 and continues with remaining iterations of loop1. In the below output we can see that the values i:2 j:2 and i:2 j:3 are not displayed. It continues with the iteration of i:3  and corresponding j values.

public class JavabreakLabeledFor {

  public static void main(String[] args) {
    loop1:
    for(int i=1;i<=3;i++) {
      loop2:
      for(int j=1;j<=3;j++) {
        if(i==2 && j==2) {
          System.out.println("Exit labeled loop");
          break loop2;
        }
        System.out.println("i:" + i + " " + "j:" + j);
      }
    }

  }

}
i:1 j:1
i:1 j:2
i:1 j:3
i:2 j:1
Exit labeled loop
i:3 j:1
i:3 j:2
i:3 j:3

Java break in Enhanced for Loop

Similar to break in for loop, when it executes the break statement in enhanced for loop i.e for-each loop, it exits the execution of the for loop. In the below example, when the variable value has the string “Chandru”, it executes the break statement and does not display the other values in the output.

public class breakForEach {

  public static void main(String[] args) {
    String[] names = {"Adithya","Balu","Chandru","Dev"};
    for(String n: names) {
      if(n.equalsIgnoreCase("chandru")) {
        System.out.println("Exit for each loop");
        break;
      }
      System.out.println(n);
    }

  }

}
Adithya
Balu
Exit for each loop

Java break in while loop

In the while loop, when it executes the break statement in java, it terminates the execution of the while loop and does not execute the remaining iterations.

In the below example, when i=2, the break statement executes and does not display the remaining i values i.e i:2 and i:3.

public class breakWhileLoop {

  public static void main(String[] args) {
    int i=0;
    while(i<=5) {
      if(i==2) {
        System.out.println("Exit while loop");
        break;
      }
      System.out.println("i: "+ i);
      i++;
    }

  }

}
i: 0
i: 1
Exit while loop

Java break in nested while loop

When we use break statement in nested while especially in the inner while loop, it stops the execution of the inner loop and proceeds with the next iteration of the outer loop.

In the below example, we can see that when i=3 and j=3, it stops the execution of the inner while loop. This is the reason that the output does not contain the values i:3 j:3 and i:3 j:4. It then proceeds with the next iteration of the outer loop which is i=2.

public class breakNestedWhile {

  public static void main(String[] args) {
    int i=5;
    while(i>1) {
      int j=1;
      while(j<5) {
        if(i==3 && j==3) {
          System.out.println("Exit inner while loop");
          break;
        }
        System.out.println("i:" + i + " " + "j:" + j);
        j++;
      }
      
      i--;
      
    }

  }

}
i:5 j:1
i:5 j:2
i:5 j:3
i:5 j:4
i:4 j:1
i:4 j:2
i:4 j:3
i:4 j:4
i:3 j:1
i:3 j:2
Exit inner while loop
i:2 j:1
i:2 j:2
i:2 j:3
i:2 j:4

Java break in do-while loop

When we use break statement in do-while loop, it exits the loop. In the below example, when i=3, it stops the execution of the loop and continues with other statements outside the loop. Hence it displays only the values i:1 and i:2.

public class breakDoWhile {

  public static void main(String[] args) {
    int i=1;
    do {
      if(i==3) {
        System.out.println("Exit do-while loop");
        break;
      }
      System.out.println("i: " + i);
      i++;
    }while(i<=5);
    System.out.println("Outside do-while loop");
  }

}
i: 1
i: 2
Exit do-while loop
Outside do-while loop

break in switch-case

Generally, we use the break statement in the switch-case within each case statement. When the variable value matches a particular case, it executes the code inside that case. Finally, to exit the switch-case, we use java break within each case. Please refer to the switch-case in java tutorial to understand in detail.

public class SwitchCaseBreakDemo {

  public static void main(String[] args) {
    String color = "red";
    switch(color) {
    case "red":
      System.out.println("You have selected red color");
      break;
    case "green":
      System.out.println("You have selected green color");
      break;
    case "blue":
      System.out.println("You have selected blue color");
      break;
    default:
      System.out.println("No matching color");
    }

  }

}
You have selected red color

Reference

Translate »