Java: Continue Statement

Profile picture for user arilio666

In Java, there is a Continue statement where its sole purpose is to jump and iteration from being executed. Continue statement is used up in a loop to skip or jump an iteration from ever being executed with a specified or provided condition for it to jump.

It continues the flow of the iteration after the jump and proceeds to perform the next step of iteration without any obstruction.

Syntax

continue;

For real-time example, we will be using the for loop to iterate numbers from 1 to 10 and here we will use the if statement provided with the necessary condition to jump or skip an iteration from this for a loop.

Example

import java.util.ArrayList;

public class Test
{
    public static void main(String[] args)
    {
        for (int counter = 1; counter <= 10; counter++)
        {
            if (counter == 8)
            {
                continue;
            }
            System.out.println(counter+ "Rocks");
        }
    }
}

Output:

1Rocks
2Rocks
3Rocks
4Rocks
5Rocks
6Rocks
7Rocks
9Rocks
10Rocks
  • Here I have written a for loop and told it to iterate from 1 to 10.
  • So from here, I want to skip the 8th iteration from ever being printed so I will be using the if statement to provide the necessary condition to jump from it.
  • Here in this case I have provided the condition of when the counter variable is equal to 8 during the incrementation simple jump it so here I used the continue statement as we all know what it does.
  • So it goes through the if block and jumps that part of iteration alone and continues to work after the jump normally and proceeds to resume the iteration until it gets completed.

So this is basically how the continue statement works in all other loops too.

Tags