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

Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use of xib in swift 3.1 and XCode 8.3.2 . Custom views are common ...
C Language – Introduction Why C? There are three important aspects of any language so as C has : The way it stores data, The way it operates on this data and How th...
Use of CollectionViewDataSource method moveItemAt ... Use of CollectionViewDataSource method moveItemAt for Reordering CollectionView cell In IOS (Swift 4) In this tutorial we are study about how we can ...
Invitation Letter for Visa Invitation Letter Samples These letter are necessary for taking visa of another country. so these samples are the most important document. Sample 1 ...
Control Flow Statements | Swift tutorial Control Flow : Swift tutorial Swift Provides diffrent type of controll flow statements like: Loops Statements : Loops are used for perform the t...
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...
Author

Write A Comment