Java Continue

    Java Continue

    The continue statement will allow you to jump to the next iteration directly and can be used with the for loop or the while loop . Whenever you encounter the continue statement, the flow of the control will go to the next iteration and skips the remaining code at the given condition.

    If the continue statement is within the inner loop, it will continue the inner loop only.

    Syntax

    jump-statement;    
    
    continue;   

    Example

    public class ContinueDemo {  
    
    public static void main(String[] args) {  
    
        for(int i=1;i<=5;i++){  
    
            if(i=2){  
    
                //using continue statement  
    
                continue;//it will skip the rest statement  
    
            }  
    
            System.out.println(i);  
    
        }  
    
    }  
    
    }  

    Output

    1
    3
    4
    5

    Continue Statement with Inner Loop

    Example

    public class Continuedemo2 {  
    
    public static void main(String[] args) {  
    
                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 a while Loop

    Example

    public class ContinueWhileExample {  
    
    public static void main(String[] args) {  
    
        //while loop  
    
        int i=1;  
    
        while(i<=5){  
    
            if(i==2){  
    
                //using continue statement  
    
                i++;  
    
                continue;//it will skip the rest statement  
    
            }  
    
            System.out.println(i);  
    
            i++;  
    
        }  
    
    }  
    
    }  

    Output

    1
    3
    4
    5

    Continue Statement with a do-while Loop

    Example

    public class ContinueDoWhiledemo {  
    
    public static void main(String[] args) {  
    
        int i=1;  
    
        do{  
    
            if(i==2){  
    
                    //using continue statement  
    
                     i++;  
    
                continue;//it will skip the rest statement  
    
            }  
    
            System.out.println(i);  
    
            i++;  
    
        }while(i<=5);  
    
    }  
    
    }  

    Output

    1
    3
    4
    5