break is a keyword in java which is used inside loops(for, while and do-while).
Please note that break can be used inside loops and switch statement while continue statement can only be used inside loops.
Using break outside a loop or switch will result in a compiler error break cannot be used outside of a loop or a switch.
How break works
As its name suggests, break statement terminates loop execution. It breaks the loop as soon as it is encountered.
Consider the below example.
It contains a loop which executes from 1 to 10 and prints the value of loop counter. Inside the loop, we check for counter value equal to 5.
If it is equal to 5 then break.
Check the output given after the program for better understanding.

public class BreakExample {
   public static void main(String args[]) {
      for (int counter = 1; counter <= 10; counter++) {
         if (counter == 5) {
            System.out.println("Terminating loop");
            // print statement will not be executed and loop will terminate
            break;
         }
         System.out.println("Loop iteration: " + counter);
      }
      System.out.println("Outside loop");
   }
}

Output

Loop iteration: 1
Loop iteration: 2
Loop iteration: 3
Loop iteration: 4
Terminating loop
Outside loop

See that when if condition is met and break is executed, loop statements after break are skipped and the statement after the loop is executed.
break statement can only be used along with an if statement.
break in nested loops
Nested loop means a loop inside another loop. What happens when break is placed inside a nested loop?
Answer is that the loop in which the break statement is written is terminated when break is executed.
Suppose there is Loop1 which contains another loop Loop2.
Now following two conditions may exist :

Scenario 1 : break is placed inside Loop2. When break is executed, then Loop 2 is terminated and Loop1 executes normally.
Scenario 2 : break is placed inside Loop1. When break is executed, then Loop 1 is terminated and hence Loop2 also terminates.

Look at the following code example for Scenario 1.
It contains two loops where one loop is nested. Both loops iterate from 1 to 3 and print the value of their loop counters.
Inner loop executes break when outer loop counter becomes equal to 2.
This means that when outer loop counter is equal to 2, inner loop should terminate but outer loop should execute normally.

public class NestedBreakExample {
   public static void main(String args[]) {
      for (int outerLoopCounter = 1; outerLoopCounter <= 3; outerLoopCounter++) {
         System.out.println("Outer Loop iteration: " + outerLoopCounter);
         for (int innerLoopCounter = 1; innerLoopCounter <= 3; innerLoopCounter++) {
            // check if outer loop counter is 2
            if (outerLoopCounter == 2) {
               // print message and terminate inner loop
               System.out.println("Terminating inner loop");
               break;
            }
            System.out.println("Inner Loop iteration: " + innerLoopCounter);
         }
      }
   }
}

Output

Outer Loop iteration: 1
Inner Loop iteration: 1
Inner Loop iteration: 2
Inner Loop iteration: 3
Outer Loop iteration: 2
Terminating inner loop
Outer Loop iteration: 3
Inner Loop iteration: 1
Inner Loop iteration: 2
Inner Loop iteration: 3

Note that when outer loop counter is 2, inner loop terminates and outer loop executes further.

Have a look at the following code example for Scenario 2.
It contains two loops where one loop is nested. Both loops iterate from 1 to 3 and print the value of their loop counters.
Outer loop executes break when its loop counter becomes equal to 2. This means that when outer loop counter is equal to 2, it should terminate execution and inner loop should also terminate.
Output given after the program further explains the concept.

public classNestedBreakExample {
   public static void main(String args[]) {
      for (int outerLoopCounter = 1; outerLoopCounter <= 3; outerLoopCounter++) {
         System.out.println("Outer Loop iteration: " + outerLoopCounter);
         // check if outer loop counter is 2
         if (outerLoopCounter == 2) {
            // print message and terminate outer loop
            System.out.println("Terminating outer loop");
            break;
         }
         for (int innerLoopCounter = 1; innerLoopCounter <= 3; innerLoopCounter++) {
            System.out.println("Inner Loop iteration: " + innerLoopCounter);
         }
     }
  }
}

Output

Outer Loop iteration: 1
Inner Loop iteration: 1
Inner Loop iteration: 2
Inner Loop iteration: 3
Outer Loop iteration: 2
Terminating outer loop

Note that when outer loop counter is 2, it terminates and inner loop also does not execute further.

Let’s tweak in :

  1. break statement can only be used along with an if statement. Use of break without an if will result in a compiler error.
    This is because a break placed without any condition will cause the loop to terminate and statements after break will never be executed.
    Compiler detects this and it flags an error.
  2. It is not mandatory to use break in switch but it is permissible to do so.
  3. It is necessary to use break inside an infinite loop otherwise there will be a compiler error.
    Reason is that if break is not used inside the loop then it will keep on executing forever and it will not allow the program to finish.
    Compiler checks this and raises an error.
  4. If break is not used inside a switch then all cases after the first matching case will be executed but there will be no compiler error in this case.
Hit the clap if the article was useful.

Leave a Reply