continue statement in java

In this post, we will see what is continue statement in java or continue statement in java.
The continue statement in Java is used 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 for loop or while loop.

continue;

continue statement in java inside for loop

If a continue statement is encountered inside a loop, the flow of execution of the loop is immediately transferred to the next iteration and all the code skips that written after the continue statement. It is used along with if statement in loops. If the condition is true, then the continue statement to transfer 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