Sometimes you may need to continue to the next iteration with in the loop with out executing the statements or you may need to terminate the loop altogether. For these two scenarios Java provides two control statements- continue and break. In this post we’ll go through Java break statement in detail along with usage examples.
Break statement in Java
break statement in Java has the following three uses-
- Exiting a loop- When the break statement is encountered with in a loop, the loop is terminated and the control transfers to the statement immediately following the loop.
- In switch statement- break statement is used to terminate the switch statement too.
- Used with label to work as "goto" statement- You can use labelled break statement to exit out of the labelled block.
Java break statement examples
Here are some examples showing the usage of break statement to exit a loop. Generally break statement is used with if-else statement.
1- Using break statement to come out of infinite while(true) loop.With in an infinite while(true) loop you can keep a condition which terminates the loop when the condition evaluates to true.
public class BreakDemo { public static void main(String[] args) { int i = 1; while(true){ if(i > 10) break; System.out.println("i- " + i); i++; } } }Output
i- 1 i- 2 i- 3 i- 4 i- 5 i- 6 i- 7 i- 8 i- 9 i- 102- Using break statement with for loop.
public class BreakDemo { public static void main(String[] args) { for(int i = 1; i <= 10; i++){ // break when value of i is 5 if(i == 5){ break; } System.out.println("i- " + i); } } }Output
i- 1 i- 2 i- 3 i- 43- Using break statement with nested loops.
When break statement is used with nested loops it exits the loop in whose scope it is used.
public class BreakDemo { public static void main(String[] args) { int rows = 3; for(int i = 1; i <= rows; i++){ System.out.print("Column " + i + "- "); for(int j = 0; j < 10; j++){ System.out.print(j); // break out of inner loop if(j == 5) break; } System.out.println(); } System.out.println("Printed the lines..."); } }Output
Column 1- 012345 Column 2- 012345 Column 3- 012345 Printed the lines...
Here break statement is used in the scope of the inner loop so it breaks out of that for loop when value of j is 5 in each iteration.
In the next example break statement is used with in the scope of the outer for loop so it breaks out of that loop.
public class BreakDemo { public static void main(String[] args) { int rows = 6; for(int i = 0; i <= rows; i++){ // break out of outer loop if (i == 3) break; System.out.print("Column " + i + "- "); for(int j = 0; j < 10; j++){ System.out.print(j); } System.out.println(); } System.out.println("Printed the lines..."); } }Output
Column 0- 0123456789 Column 1- 0123456789 Column 2- 0123456789 Printed the lines...
Java labelled break statement
Labelled break statement helps you to come out of deeply nested loops. With a normal break statement even in nested loop you can break out of a single loop which is in scope. With labelled break statement you can exit more than one block of codes or loops only requirement is that the label must enclose the break statement.
For labelling a block of code you just put a label (any name) in the starting of the block followed by a colon. To break out of that label you will use the following statement.
break label_name;
Java labelled break statement examples
In the code a label named outer is used with the outer for loop.
When break outer;
statement executes it exits out of the scope of the labelled statement.
public class BreakDemo { public static void main(String[] args) { int rows = 6; // Using label outer:for(int i = 0; i <= rows; i++){ System.out.print("Column " + i + "- "); for(int j = 0; j < 10; j++){ // exits loop labelled outer if(j == 5) break outer; System.out.print(j); } // Execution won't come here System.out.println(); } System.out.println("Printed the lines..."); } }Output
Column 0- 01234Printed the lines...
You don’t necessarily need loops to use labelled break statement, you can label a block of code and use break statement with label to break out of those labelled blocks.
public class BreakDemo { public static void main(String[] args) { boolean flag = true; firstLabel: { System.out.println("in first label block"); secondLabel: { System.out.println("in second label block"); thirdLabel: { System.out.println("in third label block"); if(flag) break secondLabel; } System.out.println("Out of thirdLabel"); } // control jumps here after the execution of break statement System.out.println("Out of secondLabel"); } System.out.println("Out of firstLabel"); } }Output
in first label block in second label block in third label block Out of secondLabel Out of firstLabel
That's all for the topic Java break statement With Examples. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment