Continue Statement in Java

In the loop control structure, the continue statement is employed when there is a need to immediately jump to the next iteration of the loop. It can be utilized both for loops and while loops.

The Java continue statement allows the program flow to continue and bypasses the remaining code within the loop based on a specified condition. When encountered within an inner loop, it exclusively applies to that inner loop.

Java provides the flexibility to utilize continue statements in all types of loops, including loops, while loops, and do-while loops.

Syntax:

jump-statement;    
continue;

Continue Statement Example in Java

ContinueExample.java

//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 5.

Continue Statement with Inner Loop in Java

When the continue statement is used within a loop in Java, it will only affect the innermost loop if the continue statement is specifically placed inside that inner loop. In other words, the continue statement will only skip the current iteration of the loop in which it is directly located, allowing the outer loops (if any) to continue their iterations as usual.

ContinueExample1.java

//Java Program to illustrate the use of continue statement  
//inside an inner loop  
public class ContinueExample1 {  
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 Labelled For Loop in Java

Since JDK 1.5, Java introduced the ability to use a continue statement with a label. This enhancement allows us to continue any loop, whether it is an outer loop or an inner loop. By labeling the loop we want to continue, we can specify the target loop for the continue statement, allowing for a more flexible control flow within nested loops. This feature provides greater versatility in continuing specific loops in Java.

Example:

ContinueExample2.java

//Java Program to illustrate the use of continue statement  
//with label inside an inner loop to continue outer loop  
public class ContinueExample2 {  
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

ContinueWhileExample.java

//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==6){  
            //using continue statement  
            i++;  
            continue;//it will skip the rest statement  
        }  
        System.out.println(i);  
        i++;  
    }  
}  
}

Output:

1
2
3
4
5
7
8
9
10

Continue Statement in do-while Loop

ContinueDoWhileExample.java

//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==7){  
                //using continue statement  
                 i++;  
            continue;//it will skip the rest statement  
        }  
        System.out.println(i);  
        i++;  
    }while(i<=10);  
}  
}

Output:

1
2
3
4
5
6
8
9
10

We hope that you have acquired a comprehensive understanding of the Continue Statement in Java. To expand your knowledge further on similar topics, we recommend following tutorials.freshersnow.com.