What is a break statement in Java and how to use it?



This article will help you understand what the break statement in Java Programming language is.

Break Statement

Sometimes, it is needed to stop a look suddenly when a condition is satisfied. This can be done with the help of break statement.

A break statement is used for unusual termination of a block. As soon as you apply a break statement, the control exits from the block (A set of statements under curly braces).

Syntax

for (condition){
   execution continues
   if (another condition is true){
      break;
   }
   Line 1;
   Line 2;
}

Given below are some examples where Break statement is used.

Example 1 − Break Statement with Loop

public class BreakExample1 { // class declaration
   public static void main(String[] args){ // main function declaration
      int i; // variable initialization
      for (i = 0; i <= 10; i++) { // for loop to increment value of i by 1
         System.out.println(i); // printing value of i on each loop
         if (i == 3){ // checking if value of i is 3 or not
            break; // if yes, then break is applied which takes the control out of the loop
         }
         System.out.println("Output 1"); // print statement which will execute only if break statement doesn't activate
      }
      System.out.println("Output 2"); // print statement which will execute only if break statement activates properly
   }
}

Output

0
Output 1
1
Output 1
2
Output 1
3
Output 2

Example 2 − Break Statement with Inner Loop

public class BreakExample2 { // class declaration
   public static void main(String[] args) { // main function declaration
      int i,j; // variable initialization
      for (i = 0; i <= 5; i++) { // for loop to increment value of i by 1
         for (j = 0; j <= 5; j++){ // for loop to increment value of j by 1
            if (i == 3 && j == 3) { // checking if value of i and j is 3
               break; // if yes, then break is applied which takes the control out of the loop
            }
            System.out.println(i + " " + j); // printing value of i and j on each loop
         }
         System.out.println(); // printing next line
      }
   }
}

Output

0 0
0 1
0 2
0 3
0 4
0 5

1 0
1 1
1 2
1 3
1 4
1 5

2 0
2 1
2 2
2 3
2 4
2 5

3 0
3 1
3 2

4 0
4 1
4 2
4 3
4 4
4 5

5 0
5 1
5 2
5 3
5 4
5 5

Example 3 − Break Statement with Labelled For Loop

public class BreakExample3 { // class declaration
   public static void main(String[] args) { // main function declaration
      Label1: // A label is a component for placing a text in a container (in here, a for loop block)
      for (int i = 0; i <= 5; i++) { // for loop to increment value of i by 1
         Label2:
         for (int j = 0; j <= 5; j++) { // for loop to increment value of j by 1
            if (i == 3 && j == 3) { // checking if value of i and j is 3
               break Label1; // if yes, then break is applied which takes the control out of the loop
            }
            System.out.println(i + " " + j); // printing value of i and j on each loop
         }
         System.out.println(); // printing next line
      }
   }
}

Output

0 0
0 1
0 2
0 3
0 4
0 5

1 0
1 1
1 2
1 3
1 4
1 5

2 0
2 1
2 2
2 3
2 4
2 5

3 0
3 1
3 2

Example 4 − Break Statement in While Loop

public class BreakExample4 { // class declaration
   public static void main(String[] args) { // main function declaration
      int i = 0; // variable declaration
      while(i <= 10) { // while loop to increment value of i by 1
         if (i == 5){ // checking if value of i is equal to 5
            break; // if yes, then break is applied which takes the control out of the loop
         }
         System.out.println(i); // printing value of i on each loop
         i++; // incrementing value of i by 1
      }
   }
}

Output

0
1
2
3
4

Example 5 − Break Statement in Do-while Loop

public class BreakExample5 { // class declaration
   public static void main(String[] args) { // main function declaration
      int i = 0; // variable declaration
      do {// start of do-while block
         if (i == 5){ // checking if value of i is equal to 5
            break; // if yes, then break is applied which takes the control out of the loop
         }
         System.out.println(i); // printing value of i on each loop
         i++; // incrementing value of i by 1
      }
      while(i <= 10); // while statement to increment value of i by 1
   }
}

Output

0
1
2
3
4

Example 6 − Break Statement in Switch Case

import java.util.*; // Scanner class imported
public class BreakExample6 { // class declaration
   public static void main (String[] args) { // main function declaration
      Scanner in = new Scanner(System.in);
      int i; // variable declaration
      System.out.println("Is 5 Greater than 3");
      System.out.println("Option 1: Yes");
      System.out.println("Option 2: No");
      System.out.println("Enter Choice");
      i = in.nextInt();
      switch(i) { // switch statement to take user input as choice
         case 1: // case 1 if user enters 1 as input
            System.out.println("Correct Choice");
            break; // break out of switch case
         case 2: // case 2 if user enters 2 as input
            System.out.println("Incorrect Choice");
            break; // break out of switch case
         default: // default case if user enters anything other than 1 and 2 as input
         System.out.println("Invalid Choice");
      }
   }
}

Output

Is 5 Greater than 3
Option 1: Yes
Option 2: No
Enter Choice
1
Correct Choice

I hope you have understood what the Break Statement in Java is. Thanks for reading the article.


Advertisements