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

Related Post

Java If-else Statement Java If-else Statement The Java if statement is used to test the condition. It checks boolean condition: true or false. For example, if a number i...
New Features XCode 12 – IDE Supported Configurations Xcode 12 beta includes SDKs for iOS 14 iPadOS 14 tvOS 14 watchOS 7 macOS 11 The Xcode 12 beta release supports o...
Introduction to JVM Introduction to JVM JVM stands for Java Virtual Machine. It works as an engine that derives the Java Code. The JVM does not understand Java ty...
Emailing Phrases Most useful emailing phrases There are some standard phrases that are used in business or formal letters. These are essential to give a professional ...
Comments in C Comments in C Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types o...
Navigation Tab Bar in IOS | Swift tutorial Navigation Tab Bar in IOS | Swift tutorial Different Customizing techniques for Navigation bar with storyboard and code 1) Custom NavigationTabBar by ...
Author

Write A Comment