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
snippet
jump-statement;  
continue;
Example
snippet
//Java Program to demonstrate the use of continue statement
//inside the for loop.
public class ContinueExample {
public static void main(String[] args) {
    //for loop
    for(int i=1;i<=10;i++){
    	if(i==5){
    	    //using continue statement
    		continue;//it will skip the rest statement
    	}
    	System.out.println(i);
    }
}
}
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.

Continue Statement with Inner Loop

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

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

Continue Statement with Labeled For Loop

We can use continute 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
snippet
//Java Program to illustrate the use of continue statement
//with label inside an inner loop to continue outer loop
public class ContinueExample3 {
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 continue statement with label
	                        continue aa;  
	                    }  
	                    System.out.println(i+" "+j);  
	                }  
	        }  
}
}
Output
1 1 1 2 1 3 2 1 3 1 3 2 3 3

Continue Statement in while loop

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

Continue Statement in do-while loop

Example
snippet
//Java Program to demonstrate the use of continue statement
//inside the Java do-while loop.
public class ContinueDoWhileExample {
public static void main(String[] args) {
    //declaring variable
    int i=1;
    //do-while loop
    do{
        if(i==5){
    	        //using continue statement
                 i++;
    		continue;//it will skip the rest statement
    	}
    	System.out.println(i);
        i++;
    }while(i<=10);
}
}
Output
1 2 3 4 6 7 8 9 10
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +