dsa

Java continue Statement

In this tutorial, you will learn about the continue statement, how to use continue statement with for-loop,while-loop,do-while loop in Java with the help of examples.

  • Introduction :

    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.

  • Syntax :

    
    continue;
  • Example of continue statement with for-loop :

    
    //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
    
  • Example of continue statement with while loop :

    
    //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
    
  • Example of continue statement with do-while loop :

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