Home >>Java Tutorial >Java Continue

Java Continue Statement

Java Continue Statement

The continue statement in Java is generally used in the loop control structure whenever there is a need to jump to the next iteration of the loop instantly by the programmer. The continue statement can be used with both the loops that are for loop or while loop. The continue statement in Java is basically used in order to continue the loop. The current flow of the program is continued by it and it skips the remaining code at the much specified condition. Whenever there is a case of inner loop then it continues the inner loop. Java continue statement can be used by the programmers in all types of loops like do-while loop, for loop, and while loop.

Syntax

Here is the syntax of the continue statement in Java is depicted below:

jump-statement;    
continue;   

Java Continue Statement Example

Here is an example of the continue statement in Java that is depicting the work aspect of it:

public class ContinueEx
{  
public static void main(String[] args) 
{  
    for(int i=1;i<=5;i++)
	{  
        if(i==2)
		{  
            //Here need to use continue statement  
            continue;//This continue statement will skip the rest statement  
        }  
        System.out.println(i);  
    }  
}  
}  
Output :
1
3
4
5

Java Continue Statement with Nested(Inner) Loop

If the continue statement is used inside of a loop then it will only continue the inner loop. Here is an example that is depicting the same:

public class ContinueEx2
{  
public static void main(String[] args)
{  
		for(int i=1;i<=3;i++)
		{    
			for(int j=1;j<=3;j++)
			{    
				if(i==2&&j==2)
				{    
					continue;    
				}    
				System.out.println(i+" "+j);    
			}    
		}    
	}  
}  
Output :
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

Java Continue Statement with Labeled For Loop

The continue statement can be used with a label by the programmers. This label feature was introduced by the JDK 1.5 version of Java. By the use of this function, now the programmers can basically continue any loop in Java now whether it is an outer loop or an inner.

Here is an example that will make you understand it better:

public class ContinueEx3{  
public static void main(String[] args) 
{  
            xy:  
            for(int i=1;i<=3;i++){    
                    xz:  
                    for(int j=1;j<=3;j++)
					{    
                        if(i==2&&j==2)
						{    
                            continue xy;    
                        }    
                        System.out.println(i+" "+j);    
                    }    
            }    
}  
}  
Output :
1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java Continue Statement in while loop

Here is an example of the continue statement in while loop:

public class ContinueEx4{  
public static void main(String[] args)
{  
    int i=1;  
    while(i<=5)
	{  
        if(i==2)
		{  
            i++;  
            continue;//This continue statement will skip the rest statement  
        }  
        System.out.println(i);  
        i++;  
    }  
}  
}
Output :
1
2
4
5

Java Continue Statement in do-while loop

Here is an example of the continue statement in do-while loop:

public class ContinueEx5 
{  
public static void main(String[] args) 
{  
    int i=1;  
    do{  
        if(i==3)
		{  
         i++;  
         continue;
        }  
        System.out.println(i);  
        i++;  
    }while(i<=10);  
}  
}  
Output :
1
2
4
5
6
7
8
9
10

No Sidebar ads