Break Statement in JAVA

The break statement is usually used in following two scenarios:

  • The break statement terminates the loop immediately, and the control of the program moves to the next statement following the loop.
    The important point to note here is that when a break statement is used inside a nested loop, then only the inner loop gets terminated.
    It is almost always used with decision making statements (if…else Statement).
  • It is also used in switch case control. Generally all cases in “switch-case” are followed by a break statement so that whenever the program control jumps to a case, it doesn’t execute subsequent cases. As soon as a break is encountered in switch-case block, the control comes out of the switch-case body.
Syntax:

The syntax of this is break word followed by semicolon i.e.

break;
Flow Diagram of Break Statement:
How break statement works?:

Example – Break Statement inside FOR Loop:
public class BreakExample1{
   public static void main(String[] args){
      for (int i = 1; i <= 10; ++i) {      
         if (i == 5) {
            break;
         }      
         System.out.println(i);
      }   
   }
}
Output:
1
2
3
4

In the above program, when the value of i becomes 5, expression i == 5 inside the parenthesis of if statement is evaluated to true. Then, The break statement is executed terminates the for loop.

Example – Break Statement inside WHILE Loop:

public class BreakExample2{
   public static void main(String args[]){
      int num =0;
      while(num<=100)
      {
          System.out.println("Value of variable is: "+num);
          if (num==2)
          {
             break;
          }
          num++;
      }
      System.out.println("Out of while-loop");
  }
}
Output:
Value of variable is: 0
Value of variable is: 1
Value of variable is: 2
Out of while-loop

In the above example, we have a while loop running from o to 100 but since we have a break statement that only occurs when the loop value reaches 2, the loop gets terminated and the control gets passed to the next statement in program after the loop body.

In case of nested loops, break terminates the innermost loop.
Example – Break Statement inside Switch-Case:

public class BreakExample3 {
   public static void main(String args[]){
    int num=2;
    switch (num)
    {
        case 1:
           System.out.println("Case 1 ");
           break;
        case 2:
           System.out.println("Case 2 ");
           break;
        case 3:
           System.out.println("Case 3 ");
           break;
        default:
           System.out.println("Default ");
    }
   }
}
Output:
Case 2

In this example, we have break statement after each Case block, this is because if we don’t have it then the subsequent case block would also execute. The output of the same program without break would be Case 2 Case 3 Default.

 

Labeled break Statement:

  • Till now we have discussed the unlabeled form of “break” statement, which terminates the innermost for, while, do…while and switch statement.
  • There is another form of “break” statement i.e. “labeled break”, that can be used to terminate the outer loop.

How labeled break statement works?:

In this, “label” is an identifier.

Example: labeled break Statement:
public class LabeledBreak{
   public static void main(String[] args){
      first:
      for( int i = 1; i < 5; i++){
         second:
         for(int j = 1; j < 3; j ++ ){
            System.out.println("i = " + i + "; j = " +j);
            if ( i == 2)
            break first;
         }
      }
   }
}
Output:
i = 1; j = 1
i = 1; j = 2
i = 2; j = 1

Get more stuff like this

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.