It is one of the most important statements is the break statement in java and let’s see how to use the java break label. One of the most common statements is the break statement in java which is used by the programmer to break the sequence of execution. The break statement uses in loops and switch statements.
The break keyword uses in two scenarios:
1. break the loop in java
2. Switch statement
In java break for loop
If a break statement encounters inside a loop, the JVM immediately terminates the loop, and the control of the program goes to the next statement following the loop. It uses along with if statement in loops. If the condition is true, then the break statement terminates the loop immediately.
break;
We can use break statements in for, while, do-while loop.
Youtube video available in the Hindi language
class ExampleBreakStaementInForLoop { public static void main(String args[]) { for(int i = 1 ; i <= 10 ; i++) { System.out.println(i); if(i == 5) break; } System.out.println(“After the for loop”); } }
Output:
1
2
3
4
5
After the for loop
In the above example, For loop runs from 0 to 10 but when the value of the variable “i” reaches 5. It encounters a break statement inside the if statement and terminates the loop immediately. After that control pass to the next statement(Outside the loop) in the program and prints the statement.
break statement in nested for loop
We can use break statements in the nested loop. When a break statement uses inside a nested loop, then only the inner loop terminates.
class ExampleOfBreak { public static void main(String args[]) { for(int i = 1 ; i <= 3 ; i++) { System.out.println("Before start of inner loop and value of i = "+ i); for(int j = 1; j <= 3 ; j++) { System.out.println("Inside the for loop and value of j ="+ j); if(j == 2) break; } } System.out.println("After the for loop"); } }
Output:
Before start of inner loop and value of i = 1
Inside the for loop and value of j =1
Inside the for loop and value of j =2
Before start of inner loop and value of i = 2
Inside the for loop and value of j =1
Inside the for loop and value of j =2
Before start of inner loop and value of i = 3
Inside the for loop and value of j =1
Inside the for loop and value of j =2
After the for loop
Explanation: In the example, there are two for loops running. We placed a break statement inside the inner for loop that only occurs when the inner loop value reaches 2, the inner loop gets terminated and the control gets passed to the outer loop.
break statement with Labeled in for loop
By using the break statement with a label, you can terminate any loop whether it is the outer loop or inner. This feature is introduced since JDK 1.5
public class BreakWithLabledExample3 { public static void main(String[] args) { outer: for(int i = 1 ; i <= 3 ; i++) { System.out.println("Before start of the inner loop"); inner: for(int j = 1; j <= 3; j++) { System.out.println("inside the inner loop and Value of j = "+j); if( j ==2) { //using break statement with label break outer; } } } } }
Output:
Before start of the inner loop
inside the inner loop and Value of j = 1
inside the inner loop and Value of j = 2
Break in a switch statement
To understand the example of a break with a switch statement, please visit here
1. Quiz, Read the below code and do answer.
Click on anyone to know the answer.