In Java, when a break statement is encountered within a loop, the loop is abruptly terminated, and the program control resumes at the next statement following the loop.
The break statement in Java is specifically designed to disrupt the normal flow of a program based on certain conditions. It can be used within various types of loops, including loops, while loops, and do-while loops. When used inside an inner loop, the break statement will only terminate the inner loop, allowing the outer loop (if present) to continue its execution.
Syntax:
jump-statement; break;
Flowchart of Break Statement:
Break Statement with Loop in Java:
Example:
BreakExample.java
//Java Program to demonstrate the use of break statement //inside the for loop. public class BreakExample { public static void main(String[] args) { //using for loop for(int i=1;i<=10;i++){ if(i==5){ //breaking the loop break; } System.out.println(i); } } }
Output:
1 2 3 4
Break Statement with Inner Loop in Java
When the break statement is used within a loop in Java, it will only terminate the innermost loop if the break statement is specifically placed inside that inner loop.
Example:
BreakExample1.java
//Java Program to illustrate the use of break statement //inside an inner loop public class BreakExample1 { public static void main(String[] args) { //outer loop for(int i=1;i<=3;i++){ //inner loop for(int j=1;j<=3;j++){ if(i==2&&j==2){ //using break statement inside the inner loop break; } System.out.println(i+" "+j); } } } }
Output:
1 1 1 2 1 3 2 1 3 1 3 2 3 3
Break Statement with Labeled For Loop in Java
Since JDK 1.5, Java introduced the ability to use a break statement with a label. This feature allows us to break out of any loop, whether it is an outer loop or an inner loop. By labeling the loop we want to break, we can specify the target loop for the break statement, enabling more flexible control flow within nested loops. This enhancement provides greater versatility in breaking out specific loops in Java.
Example:
BreakExample2.java
//Java Program to illustrate the use of continue statement //with label inside an inner loop to break outer loop public class BreakExample2 { public static void main(String[] args) { aa: for(int i=1;i<=3;i++){ bb: for(int j=1;j<=3;j++){ if(i==2&&j==2){ //using break statement with label break aa; } System.out.println(i+" "+j); } } } }
Output:
1 1 1 2 1 3 2 1
Break Statement in while loop
Example:
BreakWhileExample.java
//Java Program to demonstrate the use of break statement //inside the while loop. public class BreakWhileExample { public static void main(String[] args) { //while loop int i=1; while(i<=10){ if(i==5){ //using break statement i++; break;//it will break the loop } System.out.println(i); i++; } } }
Output:
1 2 3 4
Break Statement in do-while loop
Example:
BreakDoWhileExample.java
//Java Program to demonstrate the use of break statement //inside the Java do-while loop. public class BreakDoWhileExample { public static void main(String[] args) { //declaring variable int i=1; //do-while loop do{ if(i==5){ //using break statement i++; break;//it will break the loop } System.out.println(i); i++; }while(i<=10); } }
Output:
1 2 3 4
Break Statement with Switch
To understand the example of a break with a switch statement, please visit here: Java Switch Statement
To gain a deeper understanding of the Break Statement in Java, make sure to follow tutorials.freshersnow.com for more insights.