break statement in Java
-
Last Updated: November 2, 2023
-
By: javahandson
-
Series
In this article, we will learn what is a break statement in Java and how to use it with proper examples. If you want to learn about other Java topics you can click the above Java menu or you can click on this link Java topics.
– break statement is used when we have to terminate a single loop and come out of that loop based on some condition.
– break statement can also be used in nested loops. Say if we write a break statement inside an inner loop then the inner loop gets terminated and the control returns to the next iteration of the outer loop.
– The break statement can also be used with the switch statement. We can write a break statement inside any of the switch cases to terminate the switch block.
– We can use break statements inside any loop such as while, do…while, or for loop.
We can use a break statement when we don’t know how many times the loop will iterate. If we do not add any break statement to such a loop then it will execute an infinite number of times and we may encounter a stack overflow error or out-of-memory exception.
In the next section, we will see some examples to understand the break statement in more depth.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { for (int i=1; i<=10; i++) { if (i == 5) { break; } System.out.println("iteration number : "+i); } System.out.println("Control is outside of the for loop"); } } Output : iteration number : 1 iteration number : 2 iteration number : 3 iteration number : 4 Control is outside of the for loop
In the above program, we have written a for loop where i should iterate from 1 to 10.
Inside the for loop, we have also added an if condition that checks if i == 5.
If the i == 5 then we execute a break statement and the control comes out of the for loop and the next statement gets executed hence the i gets printed only 4 times.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { for (int i=1; i<=5; i++) { for (int j=1; j<=5; j++) { if (i == 2) { System.out.println("Inside if condition value of i : "+i+ " and value of j : "+j); break; } System.out.println("Inner for loop value of i : "+i+ " and value of j : "+j); } } System.out.println("Control is outside of the outer for loop"); } } Output: Inner for loop value of i : 1 and value of j : 1 Inner for loop value of i : 1 and value of j : 2 Inner for loop value of i : 1 and value of j : 3 Inner for loop value of i : 1 and value of j : 4 Inner for loop value of i : 1 and value of j : 5 Inside if condition value of i : 2 and value of j : 1 Inner for loop value of i : 3 and value of j : 1 Inner for loop value of i : 3 and value of j : 2 Inner for loop value of i : 3 and value of j : 3 Inner for loop value of i : 3 and value of j : 4 Inner for loop value of i : 3 and value of j : 5 Inner for loop value of i : 4 and value of j : 1 Inner for loop value of i : 4 and value of j : 2 Inner for loop value of i : 4 and value of j : 3 Inner for loop value of i : 4 and value of j : 4 Inner for loop value of i : 4 and value of j : 5 Inner for loop value of i : 5 and value of j : 1 Inner for loop value of i : 5 and value of j : 2 Inner for loop value of i : 5 and value of j : 3 Inner for loop value of i : 5 and value of j : 4 Inner for loop value of i : 5 and value of j : 5 Control is outside of the outer for loop
In the above program, we have written an Outer for loop where i should iterate from 1 to 5.
We have also written an Inner for loop that will iterate 5 times for each iteration of i.
But we have also added an if condition which says if i == 2 then we should terminate the inner for loop.
Hence in the above results, we can see that when the value of i == 2 the sysout statement of the inner for loop will not execute and the control goes to the next iteration of the outer for loop i.e i is incremented to 3.
The label represents the name of the block. We can use a label in a break statement to terminate a particular block.
In the above section, we have seen when we execute a break statement in the inner for loop, the control comes out of the inner loop but what if we want to terminate the block according to our requirements? Below are our requirements.
Let us understand it better with an example.
Write a program to terminate the inner block with the label
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { B1: for (int i=1; i<=5; i++) { B2: for (int j=1; j<=5; j++) { if (i == 2) { System.out.println("Inside if condition value of i : "+i+ " and value of j : "+j); break B2; } System.out.println("Inner for loop value of i : "+i+ " and value of j : "+j); } } System.out.println("Control is outside of the outer for loop"); } } Output: Inner for loop value of i : 1 and value of j : 1 Inner for loop value of i : 1 and value of j : 2 Inner for loop value of i : 1 and value of j : 3 Inner for loop value of i : 1 and value of j : 4 Inner for loop value of i : 1 and value of j : 5 Inside if condition value of i : 2 and value of j : 1 Inner for loop value of i : 3 and value of j : 1 Inner for loop value of i : 3 and value of j : 2 Inner for loop value of i : 3 and value of j : 3 Inner for loop value of i : 3 and value of j : 4 Inner for loop value of i : 3 and value of j : 5 Inner for loop value of i : 4 and value of j : 1 Inner for loop value of i : 4 and value of j : 2 Inner for loop value of i : 4 and value of j : 3 Inner for loop value of i : 4 and value of j : 4 Inner for loop value of i : 4 and value of j : 5 Inner for loop value of i : 5 and value of j : 1 Inner for loop value of i : 5 and value of j : 2 Inner for loop value of i : 5 and value of j : 3 Inner for loop value of i : 5 and value of j : 4 Inner for loop value of i : 5 and value of j : 5 Control is outside of the outer for loop
In the above program, we have written an Outer for loop with label B1 where i should iterate from 1 to 5.
We have also written an Inner for loop with label B2 that will iterate 5 times for each iteration of i.
But we have also added an if condition which says if i == 2 then we should terminate the B2 loop i.e. the inner for loop.
Hence in the above results, we can see that when the value of i == 2 the sysout statement of the inner for loop will not execute and the control goes to the next iteration of the outer for loop i.e i is incremented to 3. The results of this program are the same as the results from the above section.
Write a program to terminate the outer block with the label
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { B1: for (int i=1; i<=5; i++) { B2: for (int j=1; j<=5; j++) { if (i == 2) { System.out.println("Inside if condition value of i : "+i+ " and value of j : "+j); break B1; } System.out.println("Inner for loop value of i : "+i+ " and value of j : "+j); } } System.out.println("Control is outside of the outer for loop"); } } Output: Inner for loop value of i : 1 and value of j : 1 Inner for loop value of i : 1 and value of j : 2 Inner for loop value of i : 1 and value of j : 3 Inner for loop value of i : 1 and value of j : 4 Inner for loop value of i : 1 and value of j : 5 Inside if condition value of i : 2 and value of j : 1 Control is outside of the outer for loop
In the above program, we have written an Outer for loop with label B1 where i should iterate from 1 to 5.
We have also written an Inner for loop with label B2 that will iterate 5 times for each iteration of i.
But we have also added an if condition which says if i == 2 then we should terminate the B1 loop i.e. the outer loop.
Hence in the above results, we can see that when the value of i == 2 the sysout statement of the inner for loop will not execute and the control comes out of both the inner and outer loop.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { String colour = "blue"; switch (colour) { case "red": { System.out.println("It is red"); break; } case "blue": { System.out.println("It is blue"); break; } default: { System.out.println("No colour"); } } System.out.println("Control is outside of the switch loop"); } } Output: It is blue Control is outside of the switch loop
In the above program, we have written a switch block that takes a colour as a param and tries to match it with the switch cases.
The second switch case matches as the value of the param is blue and we print it. After printing we have written a break statement that terminates the switch block and the control goes to the next statement that is outside the switch block.
If we do not write the break statement in the second switch case then the default block will also get executed. Please check the example below.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { String colour = "blue"; switch (colour) { case "red": { System.out.println("It is red"); break; } case "blue": { System.out.println("It is blue"); } default: { System.out.println("No colour"); } } System.out.println("Control is outside of the switch loop"); } } Output: It is blue No colour Control is outside of the switch loop
So this is all about break statements in Java. I hope you like the article. If you have any questions on this topic please raise them in the comments section.
Please share this post and this website with your friends and colleagues so that they can learn Java and its frameworks in more depth.