Continue Statement in Java


If you want to continue the loop, the code processes the memory in its body for this particular iteration.

Continue statement performs such action. In the while and do-while loops, controlling a continue statement moves directly to the conditional expressions that control the loop.


Here is an example program that uses a continue statement to print two numbers on each line.

Program
public class Main
{
  public static void main (String[]args)
  {
    for(int i=0;i<10;i++)
    {
        System.out.print(i+" ");
        if(i%2==0) continue;
        System.out.println("");
    }
  }
}
Run

As with the break statement, the continue statement may specify a label to describe which enclosing loop to continue.

Program
public class Main
{
  public static void main (String[]args)
  {
    outer: 
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            if(j>i)
            {
                System.out.println();
                continue outer;
            }
            System.out.print(" "+(i*j));
        }
        System.out.println();
    }
  }
}
Run

The continue statement in the above example terminates the loop counting j and continues with the next iteration of the loop counting i.


Good uses of continue are rare. One reason is that Java provides a rich set of loop statements that fit most applications. However, for those special circumstances in which early iteration is needed, the continue statement provides a structured way to accomplish it.