Continue Statement in Java

A continue statement skips the remaining statements of current iteration of a loop and the program control moves to the end of the loop. Next iteration of the loop starts by evaluating the test condition (update statement is executed in case of a for loop). Unlike break statement, continue won't terminate loop but skips remaining statements of loop's code block and start new iteration from beginning.

We use continue statement, when we want to skip execution of current iteration if some condition becomes true. The continue statement is generally used with decision making statements.

In case of nested loops, if continue statement is encountered inside inner loop then it will only skip current iteration of inner loop. In general, it skips the current iteration of loop enclosing it.

By the end of this tutorial, you'll have a solid understanding of how to leverage the continue statement effectively in your Java programs.

Syntax of continue Statement

continue;

Variations of the continue Statement

The basic syntax of the continue statement has already been introduced, but let's explore some of its variations:
  • Labeled continue : A labeled continue statement allows you to specify which loop to continue when dealing with nested constructs. The label is placed before the loop, followed by a colon.
    outerLoop: // Label for the outer loop
    for (int i = 1; i <= 3; i++) {
        innerLoop: // Label for the inner loop
        for (int j = 1; j <= 3; j++) {
            // Code before continue
    
            if (/* some condition */) {
                System.out.println("Skipping inner loop.");
                continue innerLoop;
            }
    
            System.out.println("Processing inner loop.");
    
            // Code after continue
        }
    }    
        
    In this example, the labeled continue statement skips the remaining code in the inner loop and moves on to the next iteration of the inner loop.

  • Unlabeled continue : An unlabeled continue statement is used to continue the innermost loop without specifying a label.
    for (int i = 1; i <= 5; i++) {
        // Code before continue
    
        if (/* some condition */) {
            System.out.println("Skipping iteration.");
            continue; 
        }
    
        System.out.println("Processing iteration.");
    
        // Code after continue
    }    
        
    In this example, the unlabeled continue statement continues the innermost loop without affecting any outer loops.

Continue statement inside for loop

When a continue statement is executed inside a for loop, it skips the remaining statements of the loop code block immediately and program control starts execution of update statement and evaluation of control statement of for loop for next iteration.

for (initialization; condition; update) {
    statements;
    if(condition to continue) {
        continue;
    }
    statements;
}

Continue statement in for loop example program

public class ForLoopContinueStatement {
    public static void main(String[] args) {
        int count;
        for (count = 0; count < 10; count++) {
            // Skip if count is even number
            if (count%2 == 0) {
                continue;
            }
            System.out.println(count);
        }
    }
}
Output
1
3
5
7
9

Continue statement inside while loop

When a continue statement is executed inside a while loop, it skips the remaining statements of the while code block immediately and program control starts evaluates condition statement for next iteration of while loop.

while (condition) {
    statements;
    if(condition to continue) {
        continue;
    }
    statements;
}

Continue statement in while loop example program

public class WhileLoopContinueStatement {
    public static void main(String[] args) {
        int count = 0;
        while (count < 10) {
            count++;
            // Skip if count is even number
            if (count % 2 == 0) {
                continue;
            }
            System.out.println(count);
        }
    }
}
Output
1
3
5
7
9

Continue statement inside do-while loop

When a continue statement is executed inside a do-while loop, it skips the remaining statements of the do-while code block immediately and program control starts evaluation of condition statement of do-while loop for next iteration.

do {
    statements;
    if(condition to continue) {
        continue;
    }
    statements;
} while (condition);

Continue statement in do-while loop example program

public class DoWhileLoopContinueStatement {
    public static void main(String[] args) {
        int count = 0;
        do {
            count++;
            // Skip if count is even number
            if (count % 2 == 0) {
                continue;
            }
            System.out.println(count);
        } while (count < 10);
    }
}
Output
1
3
5
7
9

Continue statement in nested loop

In case of nested loop, if continue statement is encountered it will only skip the execution of loop enclosing it.

  • If continue statement is encountered inside inner loop then it will only skip the execution of inner loop.
  • If continue statement is encountered inside outer loop then it will skip the execution of outer loop.

public class ContinueNestedLoop {
    public static void main(String[] args) {
        int i, j;
        for (i = 0;i < 5;i++) {
            for (j = 0;j < 5;j++) {
                if (i != j) {
                    continue;
                }
                System.out.println("i="+ i +",j="+ j);
            }
        }
    }
}
Output
i=0,j=0
i=1,j=1
i=2,j=2
i=3,j=3
i=4,j=4

Best Practices for Using the continue Statement

To use the continue statement effectively and maintain code clarity, consider the following best practices:
  • Use continue Sparingly : While the continue statement is a powerful tool, excessive use can lead to code that is hard to understand and maintain. Use it judiciously and consider alternative approaches, such as restructuring your loops or using boolean flags, when appropriate.

  • Provide Clear Conditions : When using the continue statement, ensure that the conditions triggering the continue are clear and well-documented. This helps improve code readability and makes it easier for others (or yourself) to understand the logic.

  • Consider Alternative Control Flow : In some cases, the continue statement can be replaced with alternative control flow constructs, such as if-else statements or boolean flags. Consider these alternatives to improve code structure and maintainability.

  • Use Labels Judiciously : While labeled continues can be useful in continuing outer loops in nested constructs, excessive use of labels can make the code harder to read. Use labels judiciously and consider refactoring code to reduce the need for labeled continues.

  • Document Labeled Continues : When using labeled continues, provide comments documenting the purpose of the label and continue statement. This helps improve code readability, especially when dealing with complex nested constructs.

Conclusion

The continue statement in Java is a valuable tool for controlling the flow of loops in your programs. Whether you need to skip specific iterations, handle certain cases differently, or streamline the execution of your loops, the continue statement provides an effective solution.

By understanding the syntax, variations, and best practices associated with the continue statement, you can use it judiciously to improve the readability and maintainability of your code. Consider alternative control flow constructs when appropriate, and provide clear documentation for labeled continues.

As you continue to develop your Java programming skills, the continue statement will become an integral part of your programming toolkit, allowing you to write more expressive and efficient code.