Skip to main content

Java: Using a labeled continue statement

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
Submitted by arilio666 on December 2, 2021

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.