Continue Statement in java

Java continue statement is used to skip the current iteration of a loop. Continue statement in java can be used with for, while and do-while loop.
When continue statement is used in a nested loop, it only skips the current execution of the inner loop. Java continue statement can be used with label to skip the current iteration of the outer loop too.


Java Continue Statement Example

Syntax :

jump-statement;    
continue; 


//example of using continue statement
class Example {
    public static void main(String arr[]) { 
           for (int i=1;i<=10 ;i++ ) {
                if(i==5)
                {
                    continue;
                }
                System.out.println("value of i= "+i);
               
           }

    }
}
value of i= 1
value of i= 2
value of i= 3
value of i= 4
value of i= 6
value of i= 7
value of i= 8
value of i= 9
value of i= 10

Continue Statement with Inner Loop

When continue statement is used in a nested loop, it only skips the current execution of the inner loop.


//example of using continue statement
class Example {
    public static void main(String arr[]) { 
           for (int i=1;i<=5 ;i++ ) {
                for (int j=1;j<=5 ;j++ ) {
                if(j==3)
                {
                    continue;// it will skip the inner loop at j==3
                }
                System.out.println(i+" , "+j);
                }

                               
           }

    }
}
1 , 1
1 , 2
1 , 4
1 , 5
2 , 1
2 , 2
2 , 4
2 , 5
3 , 1
3 , 2
3 , 4
3 , 5
4 , 1
4 , 2
4 , 4
4 , 5
5 , 1
5 , 2
5 , 4
5 , 5

Continue Statement with Labeled Loop

Java continue statement can be used with label to skip the current iteration of the outer loop too.


//example of using continue statement
class Example {
    public static void main(String arr[]) { 
           Outer :for (int i=1;i<=5 ;i++ ) {
               Inner: for (int j=1;j<=5 ;j++ ) {
                if(j==3)
                {
                    continue Outer;// it will skip the outer loop at j==3
                }
                System.out.println(i+" , "+j);
                }
                               
           }

    }
}
1 , 1
1 , 2
2 , 1
2 , 2
3 , 1
3 , 2
4 , 1
4 , 2
5 , 1
5 , 2