Search Here

Java Continue Statement

 The continue statement is used in loop control structure when you need to jump to the next iteration of the loop immediately. It can be used with for loop or while loop.



The Java continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition. In case of an inner loop, it continues the inner loop only.

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

Syntax:

  1. jump-statement;    
  2. continue;   

Java Continue Statement Example

ContinueExample.java

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

Test it Now

Output:

1
2
3
4
6
7
8
9
10

As you can see in the above output, 5 is not printed on the console. It is because the loop is continued when it reaches to 5.

Java Continue Statement with Inner Loop

It continues inner loop only if you use the continue statement inside the inner loop.

ContinueExample2.java

  1. //Java Program to illustrate the use of continue statement  
  2. //inside an inner loop  
  3. public class ContinueExample2 {  
  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 continue statement inside inner loop  
  11.                             continue;    
  12.                         }    
  13.                         System.out.println(i+" "+j);    
  14.                     }    
  15.             }    
  16. }  
  17. }  

Output:

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

Java Continue Statement with Labelled For Loop

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

Example:

ContinueExample3.java

  1. //Java Program to illustrate the use of continue statement  
  2. //with label inside an inner loop to continue outer loop  
  3. public class ContinueExample3 {  
  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 continue statement with label  
  11.                             continue aa;    
  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 Continue Statement in while loop

ContinueWhileExample.java

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

Test it Now

Output:

1
2
3
4
6
7
8
9
10

Java Continue Statement in do-while Loop

ContinueDoWhileExample.java

  1. //Java Program to demonstrate the use of continue statement  
  2. //inside the Java do-while loop.  
  3. public class ContinueDoWhileExample {  
  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 continue statement  
  11.                  i++;  
  12.             continue;//it will skip the rest statement  
  13.         }  
  14.         System.out.println(i);  
  15.         i++;  
  16.     }while(i<=10);  
  17. }  
  18. }  

Test it Now

Output:

1
2
3
4
6
7
8
9
10

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