Java Language Reference

Previous Chapter 6
Statements and Control Structures
Next
 

6.9 The continue Statement

A continue statement stops the current iteration of an iteration statement and transfers control to the start of the next iteration:

[Graphic: Figure from the text]

A continue statement must occur within a while, for, or do statement or the compiler issues an error message.

If a continue statement does not contain an identifier, the statement stops the current iteration in the innermost enclosing while, for, or do statement and attempts to transfer control to the start of the next iteration. This means that in a while or do statement, the continue statement transfers control to just after the contained statement of the while or do statement. In a for statement, the continue statement transfers control to the increment portion of the for statement. Here is an example of a continue statement that contains no identifier:

public static void main (String[] argv) {
    for (int i=0; i<=15; i++) {
        System.out.println(i);
        if ( (i&1) == 0 )
            continue;
        System.out.println("That's odd");
    }
}

The above example outputs the numbers 0 through 15, printing "That's odd" after each odd number.

The innermost while, for, do, or switch statement that encloses the continue statement must be in the immediately enclosing method or initializer block. The continue statement in the following example is used incorrectly and generates an error:

while (true) {
    class X {
        void doIt() {
            continue;
        } 
    } 
    new X().doIt();
} 

If a continue statement contains an identifier, the identifier must be defined as the label of an enclosing while, for, or do statement. A continue statement that contains an identifier stops the current iteration of the labeled iteration statement and attempts to transfer control to the start of the next iteration of that loop. Here is an example of a continue statement that contains an identifier:

public boolean search(int x, int a[][]) {
    int count = 0;
  top:
    for (int i=0; i<a.length; i++) {
        int b[] = a[i];
        for (int j=0; j < b.length; j++) {
            if (x == b[j])
                return true;
            if ( x < b[j])
                continue top;
        } // for j
        count++;
        if (count > 100)
            return false;
    } // for i
    return false;
} // search()

The above method searches an array of arrays of integers for a specified value. The method assumes that the values in the sub-arrays are in descending order. The method gives up after checking 100 values.

The label used in a continue statement must be in the immediately enclosing method or initializer block.

The statement to which a continue statement attempts to transfer control is called the target statement. If a continue statement occurs inside a try statement, control may not immediately transfer to the target statement. If a try statement has a finally clause, the finally block is executed before control leaves the try statement for any reason. This means that if a continue statement occurs inside a try statement (but not in its finally block) and the target statement is outside of the try statement, the finally block is executed first, before the control transfer can take place.

If the finally block contains a break, continue, return, or throw statement, the pending control transfer for the previously executed continue statement is forgotten. Instead, control is transferred to the target of the break, continue, return, or throw statement in the finally block.

If the finally block does not contain a break, continue, return, or throw statement, the pending control transfer happens after the finally block is done executing, unless the target statement is enclosed by another try statement. If there is another enclosing try statement and it has a finally clause, that finally block is also executed before the control transfer can take place. Execution proceeds in this manner until the target statement of the continue is executed.

References Identifiers; Labeled Statements; The break Statement; The do Statement; The for Statement; The return Statement; The throw Statement; The try Statement; The while Statement


Previous Home Next
The break Statement Book Index The return Statement

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java