Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The continue statement

Java Programming Language continue Statement

The continue statement forces loop to skip statements written after it & continue next execution of the loop.

Syntax

continue;

Note

If the continue statement is encountered in the for loop, program control jumps directly to the update expression.
If the continue statement is encountered in the while & do-while loops, program control jumps directly to the condition.

Java Programming Language continue Statement Working in for, while and do-while loops

Case 1Using continue statement in for loop.

Example

class ContinueDemo1 
{
    public static void main(String args[])
    {
        int i;
	 	
        for(i=11;i<=20;i++)
        {
            if(i==14)
                 continue;
			    
            System.out.println(i);
        }
        System.out.println("After for loop");
    }
}

Output

11
12
13
15
16
17
18
19
20
After for loop

In the above program, 14 is not printed.

Case 2 Using continue statement in while loop

Example

class ContinueDemo2 
{
    public static void main(String args[])
    {
        int i;
        i = 11;
        while(i<=20)
        {
            if(i==14)
            {
                continue;
            }
            System.out.println(i);
            i++;
        }
        System.out.println("Outside while loop");
    }
}

Output

11
12
13

After printing 13, while loop executes infinitely because condition becomes always true.

Case 3 Using continue statement in do-while

Example

class ContinueDemo3 
{
    public static void main(String args[]) 
    {
        int i;
        i = 11;
		
        do
        {
            if(i==14)
            {
                continue;
            }
            System.out.println(i);
            i++;
         }while(i<=20);
		
         System.out.println("Outside while loop");
    }
}

Output

11
12
13

After printing 13, do-while loop executes infinitely because condition becomes always true.

 

Leave a reply