Labelled continue statement in Java

This tutorial is about labelled continue statement in Java. Normally, a continue statement in a Java program is used to skip some code inside the loop if a certain condition is satisfied. A label i. e. a name for a loop can be used with a continue statement to specify where the control of execution is to be transferred. We can also specify a condition with labelled continue statements if required.

Continue statement without any label

Let us first see a Java program which uses a continue statement without any label. Have a good look at the below code to understand the basics of a continue statement.

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

Output:

1
2
4
5

The working of the continue statement is pretty much clear from the example program and its output. As you can see, we have used a continue statement in the program when i becomes 3. Hence the next statement “System.out.println(i)” is skipped for that iteration and the loop is continued for other iterations.

Now, let us see an example for labelled continue statement.

Labelled continue statement

See the below code and its output.

public class Main
{
  public static void main(String[] args) {
    for(int i=1; i<=3; i++)
    {
        for(int j=1; j<=3; j++)
        {
            System.out.println(i*j);
        }
    }
  }
}

Output:

1
2
3
2
4
6
3
6
9

Here’s how the above program works. For the first iteration of the outer loop, it prints 1,2, and 3. For the second iteration of the outer loop, it prints 2, 4, and 6 and for the third iteration of the outer loop, it prints 3, 6, and 9.

Now have a look at the below Java program which uses labelled continue statement.

public class Main
{
  public static void main(String[] args) {
    outer_loop: 
    for(int i=1; i<=3; i++)
    {
        inner_loop:
        for(int j=1; j<=3; j++)
        {
            if(i*j == 3)
                continue outer_loop;
            System.out.println(i*j);
        }
    }
  }
}

And, now the output is:

1
2
2
4
6

The above program uses a continue statement with the outer loop whenever the condition (i*j == 3) is satisfied. For the first iteration of the outer loop, we get (i*j) as 1, 2 and 3. 3 is not printed as the statements after the continue statement are not executed for the first iteration and the control of execution goes to the second iteration of the outer loop. Since we never get (i*j) as 3 in the second iteration, 2, 4 and 6 are printed. In the third iteration of the outer loop, we get (i*j) as 3, 6 and 9. Since the first value is 3, the continue statement is executed and the control moves to the next iteration and none of the values are printed in this iteration. As there is no next iteration, the loop is exited.

Hope this article was helpful. Keep coding and keep learning.

Also read: Exit from a loop in Java with examples

Leave a Reply

Your email address will not be published. Required fields are marked *