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.

flowchart of continue statement in java
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.

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