Go to the first, previous, next, last section, table of contents.


The continue Statement

The continue statement, like break, is used only inside for, while, and do loops. It skips over the rest of the loop body, causing the next cycle around the loop to begin immediately. Contrast this with break, which jumps out of the loop altogether.

The continue statement in a for loop directs awk to skip the rest of the body of the loop, and resume execution with the increment-expression of the for statement. The following program illustrates this fact:

awk 'BEGIN {
     for (x = 0; x <= 20; x++) {
         if (x == 5)
             continue
         printf "%d ", x
     }
     print ""
}'

This program prints all the numbers from zero to 20, except for five, for which the printf is skipped. Since the increment `x++' is not skipped, x does not remain stuck at five. Contrast the for loop above with this while loop:

awk 'BEGIN {
     x = 0
     while (x <= 20) {
         if (x == 5)
             continue
         printf "%d ", x
         x++
     }
     print ""
}'

This program loops forever once x gets to five.

As described above, the continue statement has no meaning when used outside the body of a loop. However, although it was never documented, historical implementations of awk have treated the continue statement outside of a loop as if it were a next statement (see section The next Statement). Recent versions of Unix awk no longer allow this usage. gawk will support this use of continue only if `--traditional' has been specified on the command line (see section Command Line Options). Otherwise, it will be treated as an error, since the POSIX standard specifies that continue should only be used inside the body of a loop (d.c.).


Go to the first, previous, next, last section, table of contents.