Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Continue Statement – Mastering Loop Control with Examples

Java Continue Statement

This tutorial explains Java's continue and labeled continue statements, complete with clear examples to help you control loop flow efficiently.

While working with loops, sometimes you might want to skip certain statements or terminate the loop prematurely. In such cases, the break and continue statements are the tools you’ll use.

To learn about the break statement, visit Java break. Here, we will focus on the continue statement.


Java continue

The continue statement skips the current iteration of a loop (for, while, do…while, etc.). After the continue statement, the program jumps to the loop’s termination check (and, for for loops, the update expression).

Here is the syntax:

continue;

Note: The continue statement is most commonly used inside conditional blocks (e.g., if statements).


Working of Java continue statement

Java Continue Statement – Mastering Loop Control with Examples

Example 1: Java continue statement

class Main {
  public static void main(String[] args) {

    // for loop
    for (int i = 1; i <= 10; ++i) {

      // if value of i is between 4 and 9
      // continue is executed
      if (i > 4 && i < 9) {
        continue;
      }
      System.out.println(i);
    }
  }
}

Output:

1
2
3
4
9
10

In this program, a for loop prints the value of i for each iteration. The continue statement activates when i is greater than 4 and less than 9, causing the println statement to be skipped for 5, 6, 7, and 8.


Example 2: Compute the sum of 5 positive numbers

import java.util.Scanner;

class Main {
  public static void main(String[] args) {

    Double number, sum = 0.0;
    // create an object of Scanner
    Scanner input = new Scanner(System.in);

    for (int i = 1; i < 6; ++i) {
      System.out.print(\"Enter number \" + i + \" : \");
      // takes input from the user
      number = input.nextDouble();

      // if number is negative
      // continue statement is executed
      if (number <= 0.0) {
        continue;
      }

      sum += number;
    }
    System.out.println(\"Sum = \" + sum);
    input.close();
  }
}

Output:

Enter number 1: 2.2
Enter number 2: 5.6
Enter number 3: 0
Enter number 4: -2.4
Enter number 5: -3
Sum = 7.8

Here, the continue statement skips any negative or zero input, ensuring that only positive values contribute to the total.

Note: We used the Scanner class to read user input. To learn more, visit Java Scanner.


Java continue with Nested Loop

When loops are nested, a continue inside the inner loop affects only that inner loop’s current iteration.

Java Continue Statement – Mastering Loop Control with Examples

Example 3: continue with Nested Loop

class Main {
  public static void main(String[] args) {

    int i = 1, j = 1;

    // outer loop
    while (i <= 3) {

      System.out.println(\"Outer Loop: \" + i);

      // inner loop
      while(j <= 3) {

        if(j == 2) {
          j++;
          continue;
        }

        System.out.println(\"Inner Loop: \" + j);
        j++;
      }
      i++;
    }
  }
}

Output

Outer Loop: 1
Inner Loop: 1
Inner Loop: 3
Outer Loop: 2
Outer Loop: 3

In this example, the continue inside the inner loop skips the iteration when j equals 2, preventing “Inner Loop: 2” from being printed.


Labeled continue Statement

Besides the basic continue, Java also supports a labeled version that directs the flow to a specific loop. The syntax is:

continue label;

This causes the program to skip the current iteration of the loop identified by label.

Java Continue Statement – Mastering Loop Control with Examples

In the illustration above, the label targets the outer loop, demonstrating how the continue can skip an entire iteration of that loop.


Example 4: labeled continue Statement

class Main {
  public static void main(String[] args) {

    // outer loop is labeled as first
    first:
    for (int i = 1; i < 6; ++i) {

      // inner loop
      for (int j = 1; j < 5; ++j) {
        if (i == 3 || j == 2)

          // skips the current iteration of outer loop
          continue first;
        System.out.println(\"i = \" + i + \"; j = \" + j);
      }
    }
  }
}

Output:

i = 1; j = 1
i = 2; j = 1
i = 4; j = 1
i = 5; j = 1

Here, the labeled continue jumps to the next iteration of the loop named first whenever i equals 3 or j equals 2.

Note: Labeled continue can be powerful but often reduces readability. When you encounter such a need, consider refactoring to keep the flow clear.


Java

  1. Mastering the C# Continue Statement: How to Skip Loop Iterations Effectively
  2. Mastering C++ Continue Statement: Practical Examples & Loop Control
  3. Mastering Java if…else: Control Flow Explained
  4. Mastering the Java Switch Statement: Syntax, Usage, and Best Practices
  5. Master Java For Loops: Syntax, Examples, and Best Practices
  6. Mastering the Java Enhanced For Loop: Syntax, Examples, and Best Practices
  7. Java Break Statement: How, When, and Labeled Breaks Explained
  8. Mastering Java Loop Control: Efficient Repetition Techniques
  9. Mastering C Loops: A Comprehensive Guide to Repetition Control
  10. Master C# Loops: Efficient Code Repetition Techniques