The Continue Statement Tutorial

The continue statement can only be used inside of the following looping control flow statements: for, while, and do-while. There are two types of continue statements, unlabeled and labeled. The unlabeled continue statement skips the current iteration for the looping block it is located inside of.
for (initialization; termination; increment) {
      if (condition) {
            continue;
      }
      Statements are not executed if the continue statement is called;
}


What can we do if we have nested loops and we want a conditional test inside of an inner loop to skip the current iteration in the outer loop as well? We can use the labeled continue statement. The first step for using a labeled continue statement is to make a label followed by a colon. The label must be located directly before the block of code that it is labeling. When the continue statement is called it must have the label name after the keyword continue, but before the semicolon. The rest of the statements in the inner loop and the outer loop will be skipped, and next iteration of the outer loop will be executed.
label:
for (initialization; termination; increment) {
      for (initialization; termination; increment) {
            if (condition) {
                  continue label;
            }
            Statements are not executed if the labeled continue statement is called;
      }
      Statements are not executed if the labeled continue statement is called;
}


This tutorial uses code and descriptions that are very similar to my Break Statement Tutorial. I highly recommend watching that tutorial as well.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ContinueStatement
C:\Java>cd ContinueStatement
C:\Java\ContinueStatement>Notepad ContinueStatement.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class ContinueStatement {
    public static void main(String args[]) {
        String stringArray[] = {"Duck", "Duck", "Duck", "Duck", "Goose", "Duck", "Duck", "Duck", "Duck" };
        for(int i = 0; i < stringArray.length; i++) {
            if (stringArray[i].equals("Goose")) {
                continue; // don't display Goose to the console.
            }
            System.out.print(stringArray[i] +" ");
        }
        System.out.println("\nOnly Ducks here, no Geese.");
        System.out.println();

        // build a number ladder of odd numbers
        int maxNumber = 11; 
       
        myLabel:
        for (int x=1; true; x++){
            inner:
            for (int y=1; y<=x; y++) {
                if (x > maxNumber) { // break if we are at our target number
                    break myLabel;
                }
                if (x % 2 == 0) { // skip row if number is even
                    continue myLabel;
                }
                if (y % 2 == 0) { // skip column if number is even
                    continue inner;
                }
                System.out.print(y + " ");
            }
            System.out.println();
        }
        System.out.println("Program is over");
    }
}

Now switch back to the command prompt (CMD) and type in javac ContinueStatement.java and press Enter.
Now type in java ContinueStatement and press Enter.


C:\Java\ContinueStatement>javac ContinueStatement.java
C:\Java\ContinueStatement>java ContinueStatement
Duck Duck Duck Duck Duck Duck Duck Duck
Only Ducks here, no Geese.

1
1 3
1 3 5
1 3 5 7
1 3 5 7 9
1 3 5 7 9 11


Final thoughts

Using the continue statement is very uncommon. There may be unique circumstances in which loop iteration skipping is required, and the continue statement provides a structured way to do just that.


Tutorials