continue statement in java

In this post, we will see what is continue statement in java.
The continue statement in Java uses in a loop control structure. If you want to skip some code and need to jump to the next iteration of the loop immediately. Then we should use it. It can be used in for loop or while loop.

continue;

Youtube video available in the Hindi language

Youtube video available in the Hindi language

continue statement in java inside for loop

If a continue statement encounters inside a loop, the control transfers to the next iteration and the rest of the code don’t execute for the iteration. It uses along with the if statement in loops. If the condition returns true, then the continue statement transfers the control of the loop immediately to the next iteration. Unlike the break statement, It doesn’t break the execution of the loop. It just skips one iteration of loops.
The continues statement is useful whenever use want to skip the iteration based on conditions.

continue statement in java example
public class ContinueExampleForExample 
{
   public static void main(String args[])
   {
	for (int i = 1; i <= 5; i++)
	{
               if (i == 2)
               {
        	  continue;
               } 
               System.out.println("Value of i ="+ i);
	}
   }
}

Output:
Value of i =1
Value of i =3
Value of i =4
Value of i =5

In the above example, the “value of i = 2” is missing in the output, because when the value of the variable i is 2, the program encountered a continue statement, which makes it jump for next iteration, skipping the statements for the current iteration.

Frequently Asked Questions about the continue statement in Java

What does the continue statement do in Java?

The continue statement skips the remaining statements of the current loop iteration and transfers control to the next iteration of the loop. Unlike the break statement, it does not terminate the loop.

Can the continue statement be used in both for and while loops?

Yes. As described on this page, the continue statement can be used inside a for loop or a while loop to skip part of an iteration based on a condition.

How is continue different from break in Java?

The continue statement only skips the current iteration and lets the loop continue running, whereas the break statement exits the loop entirely. The page links to the break statement article for comparison.

Is the continue statement usually used with an if statement?

Yes. The continue statement is typically used together with an if statement inside a loop so that the iteration is skipped only when a certain condition is true.

What happens to the code after continue in the same iteration?

Any statements placed after the continue statement within that iteration are not executed; the loop immediately moves on to the next iteration.

1. Quiz, Read the below code and do answer.

public class ExampleOfContinue
{
    public static void main(String[] args) 
    {
    	for(int i = 0; i < 5; i++)
    	{
    		continue;
    	}
    }
}

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

public class ExampleOfContinue
{
    public static void main(String[] args) 
    {
    	for(int i = 0; i < 5; i++)
    	{
    		continue;
    		System.out.println(i);
    	}
    }
}

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

public class ExampleOfContinue
{
    public static void main(String[] args) 
    {
    	for(int i = 0; i < 5; i++)
    	{
    		if(i == 2)
    			continue;
    		System.out.println(i);
    	}
    }
}

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

public class ExampleOfContinue
{
    public static void main(String[] args) 
    {
    	for(int i = 0; i < 3; i++)
    	{
    		System.out.println(i);
    		if(i == 2)
    			continue;
    		System.out.println(i);
    	}
    }
}

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

public class ExampleOfContinue
{
    public static void main(String[] args) 
    {
    	for(int i = 0; i < 3; i++)
    	{
    		if(i == 0)
    			continue;
    		if(i == 1)
    			continue;
    		if(i == 2)
    			continue;
    		System.out.println(i);
    	}
    }
}

Click on anyone to know the answer.

6. Quiz, Read the below code and do answer.

public class ExampleOfContinue
{
    public static void main(String[] args) 
    {
    	for(int i = 0; i < 3; i++)
    	{
    		if(i == 0)
    			break;
    		if(i == 1)
    			System.out.println(i);
    	}
    }
}

Click on anyone to know the answer.

Leave a Comment