Search Here

Java Break Statement

 When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.



The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop.

We can use Java break statement in all types of loops such as for loopwhile loop and do-while loop.

Syntax:

  1. jump-statement;    
  2. break;   

Flowchart of Break Statement

java break statement flowchart

Java Break Statement with Loop

Example:

BreakExample.java

  1. //Java Program to demonstrate the use of break statement    
  2. //inside the for loop.  
  3. public class BreakExample {  
  4. public static void main(String[] args) {  
  5.     //using for loop  
  6.     for(int i=1;i<=10;i++){  
  7.         if(i==5){  
  8.             //breaking the loop  
  9.             break;  
  10.         }  
  11.         System.out.println(i);  
  12.     }  
  13. }  
  14. }  

Output:

1
2
3
4

Java Break Statement with Inner Loop

It breaks inner loop only if you use break statement inside the inner loop.

Example:

BreakExample2.java

  1. //Java Program to illustrate the use of break statement    
  2. //inside an inner loop   
  3. public class BreakExample2 {  
  4. public static void main(String[] args) {  
  5.             //outer loop   
  6.             for(int i=1;i<=3;i++){    
  7.                     //inner loop  
  8.                     for(int j=1;j<=3;j++){    
  9.                         if(i==2&&j==2){    
  10.                             //using break statement inside the inner loop  
  11.                             break;    
  12.                         }    
  13.                         System.out.println(i+" "+j);    
  14.                     }    
  15.             }    
  16. }  
  17. }  

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java Break Statement with Labeled For Loop

We can use break statement with a label. The feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is outer or inner loop.

Example:

BreakExample3.java

  1. //Java Program to illustrate the use of continue statement  
  2. //with label inside an inner loop to break outer loop  
  3. public class BreakExample3 {  
  4. public static void main(String[] args) {  
  5.             aa:  
  6.             for(int i=1;i<=3;i++){    
  7.                     bb:  
  8.                     for(int j=1;j<=3;j++){    
  9.                         if(i==2&&j==2){    
  10.                             //using break statement with label  
  11.                             break aa;    
  12.                         }    
  13.                         System.out.println(i+" "+j);    
  14.                     }    
  15.             }    
  16. }  
  17. }  

Output:

1 1
1 2
1 3
2 1

Java Break Statement in while loop

Example:

BreakWhileExample.java

  1. //Java Program to demonstrate the use of break statement  
  2. //inside the while loop.  
  3. public class BreakWhileExample {  
  4. public static void main(String[] args) {  
  5.     //while loop  
  6.     int i=1;  
  7.     while(i<=10){  
  8.         if(i==5){  
  9.             //using break statement  
  10.             i++;  
  11.             break;//it will break the loop  
  12.         }  
  13.         System.out.println(i);  
  14.         i++;  
  15.     }  
  16. }  
  17. }  

Output:

1
2
3
4

Java Break Statement in do-while loop

Example:

BreakDoWhileExample.java

  1. //Java Program to demonstrate the use of break statement  
  2. //inside the Java do-while loop.  
  3. public class BreakDoWhileExample {  
  4. public static void main(String[] args) {  
  5.     //declaring variable  
  6.     int i=1;  
  7.     //do-while loop  
  8.     do{  
  9.         if(i==5){  
  10.            //using break statement  
  11.            i++;  
  12.            break;//it will break the loop  
  13.         }  
  14.         System.out.println(i);  
  15.         i++;  
  16.     }while(i<=10);  
  17. }  
  18. }  

Output:

1
2
3
4

Java Break Statement with Switch

To understand the example of break with switch statement, please visit here: Java Switch Statement.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad

Ads Section