The continue Statement – Control Flow

The continue Statement – Control Flow

4.12 The continue Statement

Like the break statement, the continue statement comes in two forms: unlabeled and labeled.

Click here to view code image

continue;             // the unlabeled form
continue
label
;        // the labeled form

The continue statement can be used only in a for(;;), for(:), while, or do-while loop to prematurely stop the current iteration of the loop body and proceed with the next iteration, if possible. In the case of the while and do-while loops, the rest of the loop body is skipped—that is, the current iteration is stopped, with execution continuing with the loop condition. In the case of the for(;;) loop, the rest of the loop body is skipped, with execution continuing with the update expression.

In Example 4.12, an unlabeled continue statement is used to skip an iteration in a for(;;) loop. Control is transferred to (2) when the value of i is equal to 4 at (1), skipping the rest of the loop body and continuing with the update expression in the for(;;) statement.

Example 4.12 The continue Statement

Click here to view code image

class Skip {
  public static void main(String[] args) {
    System.out.println(“i    sqrt(i)”);
    for (int i = 1; i <= 5; ++i) {
      if (i == 4) continue;             // (1) Control to (2).
      // Rest of loop body skipped when i has the value 4.
      System.out.printf(“%d    %.2f%n”, i, Math.sqrt(i));
      // (2) Continue with update expression in the loop header.
    } // end for
  }
}

Output from the program:

i    sqrt(i)
1    1.00
2    1.41
3    1.73
5    2.24

A labeled continue statement must occur within a labeled loop that has the same label. Execution of the labeled continue statement then transfers control to the end of that enclosing labeled loop. In Example 4.13, the unlabeled continue statement at (3) transfers control to (5) when it is executed; that is, the rest of the loop body is skipped and execution continues with the update expression in the inner loop. The labeled continue statement at (4) transfers control to (6) when it is executed; that is, it terminates the inner loop but execution continues with the update expression in the loop labeled outer. It is instructive to compare the output from Example 4.11 (labeled break) with that from Example 4.13 (labeled continue).

Example 4.13 Labeled continue Statement

Click here to view code image

class LabeledSkip {
  public static void main(String[] args) {
    int[][] squareMatrix = {{4, 3, 5}, {2, 1, 6}, {9, 7, 8}};
    int sum = 0;
    outer: for (int i = 0; i < squareMatrix.length; ++i){   // (1) label
        for (int j = 0; j < squareMatrix[i].length; ++j) {  // (2)
          if (j == i) continue;                             // (3) Control to (5).
          System.out.println(“Element[” + i + “, ” + j + “]: ” +
              squareMatrix[i][j]);
          sum += squareMatrix[i][j];
          if (sum > 10) continue outer;                     // (4) Control to (6).
          // (5) Continue with update expression in the inner loop header.
        } // end inner loop
        // (6) Continue with update expression in the outer loop header.
      } // end outer loop
    System.out.println(“sum: ” + sum);
  }
}

Output from the program:

Element[0, 1]: 3
Element[0, 2]: 5
Element[1, 0]: 2
Element[1, 2]: 6
Element[2, 0]: 9
sum: 25