Difference between break and continue in Java

Introduction

 
In Java programming language, break and continue are called jump statements. The jumping statements are the control statements which transfer the program execution control to a specific statement. In Java, Jump statements are used to unconditionally transfer program control from one point to elsewhere in the program. Jump statements are primarily used to interrupt loop or switch-case instantly.
 

The break Statement in Java

 
Sometimes, it is necessary to jump out of a loop irrespective of the conditional test value. The break statement is used inside any loop to allow the control to jump to the instant statement, following the loop. 
 
Syntax 
  1. if(condition)    
  2. {    
  3.    break;    
  4. }   

How we can use the break statement in Java? 

 
When we use a nested loop, break jumps the control from the loop, where it has been used. Break statement can be used inside any loop i.e. while, do-while, for and switch statement. In java use of break, the statement is to forcibly terminate the loop and resume at next statement of the loop. As soon as, the break statement is encountered inside the loop, that loop is immediately terminated without executing the remaining code in the body of that loop, and the program control reaches to next statement written after that loop.
 
break_statement_flowchart 
 
The break statement is different from the exit. Break jumps the control out of the loop and exit stops the execution of the entire program. The complete program of the break statement is listed below.
  1. public class BreakExample1 {  
  2.     public static void main(String args[]) {  
  3.         int[] test = {01234};  
  4.         for (int s : test) {  
  5.             if (s == 3) {  
  6.                 break;  
  7.             }  
  8.             System.out.print(s);  
  9.             System.out.print("\n");  
  10.         }  
  11.         System.out.println("break statement execution");  
  12.     }  
  13. }   
Output
 
break-example1-output 
 
Explanation: In this program, the for loop executed starting from s = 0 to 4 in steps of 1. Now when the condition (i==3) in the body of the loop is satisfied, the break statement causes the control to move out of for loop.
 
Here, one more example program for understanding the working of the break statement in Java. The complete program is listed below.
  1. public class BreakExample3 {  
  2.     //using the break to exit the loop  
  3.     public static void main(String args[]) {  
  4.         for (int i = 0; i < 100; i++) {  
  5.             System.out.println(+i);  
  6.             if (i == 10)  
  7.                 break// as the condition inside 'if' satisfies control comes out of 'for' loop.  
  8.         }  
  9.         System.out.println(" Loop breaks here");  
  10.     }  

Output
 
break-example3-output
 

Break statement with a nested loop

 
break only let the program exit the loop which encloses it. If the break is used with a nested loop, it breaks out only the innermost loop and does not affect the outer loop. It breaks nested loop only if we use break statement inside the nested loop. The complete program of break statement with a nested loop is listed below.
  1. public class BreakExample2 {  
  2.     public static void main(String[] args) {  
  3.         for (int a = 1; a <= 3; a++) {  
  4.             for (int b = 1; b <= 3; b++) {  
  5.                 if (a == 2 && b == 2) {  
  6.                     break;  
  7.                 }  
  8.                 System.out.println(a + " " + b);  
  9.             }  
  10.         }  
  11.     }  
  12. }  
Output 
 
break-example2-output 
 
Explanation: In this program, the nested for loop execution started from a = 0 to 3 and b = 1 to 3 in steps of 1. Now when the condition (a == 2 && b ==2) in the body of the loop is satisfied, the break statement causes the control to move out of for loop.
 

The labeled break statement

 
Java uses a break as a form of goto without its problem. As goto provides an unstructured branching which is hard to understand and also goto prohibits compiler optimization, Java uses an expanded form of break which is similar as goto and helps to exit more than one block at a time and resume the control of the program to the end of labeled block, mentioned with the break statement.
 
Syntax
  1. if(condition)    
  2. break label_name;  
The complete program is listed below.
  1. public class BreakExample4 {  
  2.     //using break as a form of goto   
  3.     public static void main(String args[]) {  
  4.         boolean t = true;  
  5.         first:{  
  6.             second:{  
  7.                 third:{  
  8.                     System.out.println(" this third block");  
  9.                     if (t) break first;  
  10.                 }  
  11.                 System.out.println(" this second block");  
  12.             }  
  13.             System.out.println(" this first block");  
  14.         }  
  15.         System.out.println(" this main block");  
  16.     }  

Output
 
break-example4-output
 

The continue statement in Java 

 
As break terminates the remaining iteration of the loop and lets the control exits the loop. Here, continue works somewhat as a break. Continue statement is sometimes required to skip some part of the loop and to continue the execution with next loop iteration. Continue statement is mainly used inside the loop helps to bypass the section of a loop and pass the control to the start of the loop to continue the execution with the next loop iteration.
 
Syntax
  1. if(condition)    
  2. {    
  3.    continue;    
  4. }   

How we can use the continue statement in Java? 

 
This statement is used only within looping statements. When the continue statement is encountered, then it skips the current iteration and the next iteration starts. 
 
continue_statement_feedback 
 
The remaining statements in the loop are skipped. The execution starts from the top of the loop again. We can use continue statement to skip current iteration and continue the next iteration inside loops. The complete program of continue statement is listed below.
  1. public class ContinueExample {  
  2.     public static void main(String[] args) {  
  3.         int i;  
  4.         for (i = 1; i <= 20; i++) {  
  5.             if ((i % 5) == 0) {  
  6.                 continue;  
  7.             }  
  8.             System.out.println(i);  
  9.         }  
  10.     }  
  11. }  
Output 
continue-example1-output 
 
Explanation: Here, the print statement is bypassed each time when the value stored in (i) is divisible by 5.
 
Here, one more example program for understanding the working of the break statement in Java. The complete program is listed below.
  1. public class ContinueExample3 {  
  2. //it prints the numbers which are divisible by 6 till 50.  
  3.     public static void main(String args[]) {  
  4.         for (int i = 0; i <= 50; i++) {  
  5.             if (i % 6 != 0continue//  continue resume the control  to the next iteration in for loop  
  6.             System.out.println(" i :  " + i);  
  7.         }  
  8.     }  

Output
 
continue-example3-output
 
Explanation: As the output, the following program prints the numbers which are divisible by 6 till 50.
 

continue statement with a nested loop 

 
The continue nested loop is used only if the continue statement is used inside the nested loop. The complete program of continue statement with a nested loop is listed below.
  1. public class ContinueExample2 {  
  2.  public static void main(String[] args) {  
  3.   for (int a = 1; a <= 3; a++) {  
  4.    for (int b = 1; b <= 3; b++) {  
  5.     if (a == 2 && b == 2) {  
  6.      continue;  
  7.     }  
  8.     System.out.println(a + " " + b);  
  9.    }  
  10.   }  
  11.  }  
  12. }  
Output 
 
continue-example2-output
 

The labeled continue statement

 
In the case of nested loops to continue a particular loop we should go for labeled continue statements. The Java labeled loops allows transferring to a particular line or statement. The complete program of labeled continue statement is listed below.
  1. public class ContinueExample4 {  
  2.     public static void main(String[] args) {  
  3.         out:  
  4.         for (int i = 1; i <= 3; i++) {  
  5.             System.out.println(+i);  
  6.             for (int j = 1; j <= 3; j++) {  
  7.                 System.out.println(+j);  
  8.                 if (j == 2) {  
  9.                     // continue; this will skip second(j==2) iteration of inner for loop only  
  10.                     continue out; // this will skip current iteration of both for loops  
  11.                 }  
  12.             }  
  13.         }  
  14.     }  

Output
 
continue-example4-output 
 
Explanation: As the output outer for loop will iterate 100 times but the inner for loop will iterate twice each time.
 

break vs continue 

 
break statement continue statement
It terminates the execution of the remaining iteration of the loop. It terminates only the current iteration of the loop.
break resumes the control of the program to the end of loop enclosing that 'break'. continue resumes the control of the program to the next iteration of that loop enclosing continue.
It causes early termination of a loop. It causes early execution of the next iteration.
break stops the continuation of the loop. continuation of loop. continue do not stop the continuation of the loop, it only stops the current iteration.
the break can be used with switch, label. continue can not be executed with switch and labels.
 

Summary

 
In this article, we learned about the jump statements of Java programming language break statement and continue statement and how we can use these statements in Java Programs.