Java: Using a labeled continue statement

Profile picture for user arilio666

In Java, we all know that 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. Now at times if required we can use the labeled continue statement too for the right loop it will be a perfect asset.

Syntax

continue LabelName;

Let us see it in action we will be using two for-loop within one another and be iterating the i and j to get the series of numbers as counts.

Example

import java.util.ArrayList;

public class Test
{
    public static void main(String[] args)
    {
    JumpLabel:for (int i = 0; i < 6; i++)
    {
        for(int j=0;j<i;j++)
        {
            if(i==4)
                continue JumpLabel;
            System.out.println(i+"\t");
        }
        System.out.println();
    }
    System.out.println("Outside loop");
}

Output:

1    

2    
2    

3    
3    
3    

5    
5    
5    
5    
5    

Outside loop
  • So from we have taken two loops and placed a loop within a loop to iterate the I value within j and get the series of numbers in the form of count representation.
  • So here we will be using the label called JumpLabel to call the loop to perform he continue on it.
  • Provided with the if condition within the loop that when the iteration comes to the point of 4 then the iteration jumps leaving 4 behind and continuing further with the iteration.
  • This is how we can use the labeled continue statement.
Tags