Continue Statement in Java

This article will discuss the continue statement in the Java programming language. It will cover topics such as the use of the continue statement, its syntax, and a flowchart.

The continue statement is used to skip the remaining statements in the current iteration of a loop and continue with the next iteration. It is typically used to skip over a statement that is not relevant to the current iteration of the loop.

We will also be examining programs that use the continue statement in loops, switch statements, and if-else statements. The most effective approach to learning is to practice the codes provided in the article and experiment with different versions of the same codes to grasp the concept.

What is the Java continue statement, and what is it used for?

U might think that the break statement just breaks the loop, so the continue statements will just continue the loop, well, that is not completely true. The continue statement skips the current iteration of a loop when a specific condition is met.

The continue statement is different from the break statement in that it skips the current iteration of a loop while the break statement terminates the entire loop.

The break statement moves the program to the line immediately after the loop, but the continue statement takes the program to the end of the loop (in case of a do-while loop), or the beginning of the loop (in case of a for loop or while loop) to check the condition again with the updated parameter.

Syntax and Flowchart depicting the use of continue:

The continue statement does not have a syntax. To use a continue statement in Java or most other programming languages, simply write the word “continue” followed by a semicolon, as shown below:

continue;

To gain a better understanding of how the continue statement works in a loop, let us examine a flowchart.

Java Continue Statement flowchart

Usage of Java continuing statements

Now that we have a solid understanding of how the continue statement works, let’s look at how to use it in different scenarios.

1. Using “continue” in a for loop:

public class FirstCode
{
     public static void main(String[] args)
    {
        for (int i = 1; i <= 10; i++) 
        {
            if (i == 2 || i == 4 || i == 6 || i == 8) 
            {
                continue;
            }
            System.out.print(i + " ");
        }
    }
}

Output:

1 3 5 7 9 10

In this program, the for loop prints all numbers from 1 to 10. However, we add an if statement to not print the numbers 2, 4, 6, and 8. Therefore, when the value of i is either 2, 4, 6 or 8, the continue statement will skip the entire iteration and then go to the top of the for loop to check the condition for the next number.

2. Using “continue” in a while loop:

public class FirstCode
{
     public static void main(String args[])
    {
        int i = 1;
        while (i <= 10) 
        {
            if (i == 3 || i==5 || 1==7) 
            {
                i++;
                continue;
            }
            System.out.print(i + " ");
            i++;
        }
    }
}

Output:

1 2 4 6 8 9 10

In this program, the while loop prints all numbers from 1 to 10; however, we add an if statement to not print the numbers 3, 5, and 7. Therefore, when the value of i is either 3, 5, or 7, the continue statement will skip the entire iteration and then go to the top of the for loop to check the condition for the next number.

3. Using “continue” in a do-while loop:

public class FirstCode
{
     public static void main(String args[])
    {
        int i = 1;
        do
        {
            if (i == 3 || i==5 || 1==7) 
            {
                i++;
                continue;
            }
            System.out.print(i + " ");
            i++;
        }
        while (i <= 10); 

    }
}

4. Using “continue” as a goto statement:

The continue statement is a more civilized form of the goto statement.

Here is an example of how the continue statement can also be used as a goto statement:

class FirstCode
{
  public static void main(String[] args) 
  {
    first:
    for (int i = 1; i < 6; ++i) 
    {
      for (int j = 1; j < 5; ++j) 
      {
        if (i == 3 || j == 2){continue first;}
        System.out.println("Value of i = " + i + "\t Value of j = " + j);
      }
    }
  }
}

Output:

Value of i = 1 Value of j = 1
Value of i = 2 Value of j = 1
Value of i = 4 Value of j = 1
Value of i = 5 Value of j = 1

In this program, the label “first” acts as a block of code, so, when the program hits the continue statement, it will go back to the label “first”. Here, the first loop is responsible for traversing the variable ‘i’ from 1 to 6, and the second loop is responsible for traversing the variable “j” from 1 to 5.

The if statement checks if the value of i is 3 or if the value of j is 2. If either of these conditions is true, the continue statement is executed. This means that the code inside the loop will be skipped, and the next iteration of the loop will begin. In the output, the value of i=3 is not printed because the continue statement is executed. Only the value of j=1 is printed because the continue statement is not executed for this value.

5. Using “continue” in a nested for loop

When we have a continue statement in a nested for loop, upon meeting the condition for the continue statement, the iteration ends and goes to the next iteration of the nested loop.

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

Output:

1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

Conclusion

You have now mastered the continue statements as you have seen their uses and the syntax of the continue statement, along with a flowchart. We have also discussed the usage of the continue statement as we saw programs of its implementation in different loops like for, while, and do while. We have also seen the usage of continue as a goto statement.

Leave a Reply

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