ZetCode

C continue

last modified November 14, 2026

The continue statement skips the rest of the current iteration of a loop and starts the next one. It is a jump statement, like break, goto and return, but it is gentler than break: the loop keeps running, only the remainder of the body is skipped. This tutorial shows the statement in for, while and do-while loops, compares it with break, and explains the looping mistake that continue makes easy to write.

The continue statement

continue may appear only inside the body of a loop. In a for loop it jumps to the increment expression, then the condition is tested; in a while or do-while loop it jumps straight to the condition. In every case the statements between continue and the closing brace of the body are not executed for that iteration.

while (condition) {

    ...

    if (skip) {
        continue;
    }

    ...   /* skipped when skip is true */
}

The loop condition is still evaluated, so a continue does not end the loop. That is the important difference from break, which leaves the loop entirely. continue also has nothing to do with switch: inside a switch it continues the enclosing loop, while break would leave the switch.

continue in a for loop

The next example adds the numbers from 1 to 19, but skips 7 and 13. The test is at the top of the body, so the two values never reach the addition.

continue_for.c
#include <stdio.h>

int main(void) {

    int sum = 0;

    for (int i = 1; i < 20; i++) {

        if (i == 7 || i == 13) {
            continue;
        }

        sum += i;
    }

    printf("sum = %d\n", sum);

    return 0;
}

When i is 7 or 13, continue jumps to i++ and then to the condition, so the loop advances normally and only the sum += i statement is skipped. The sum of 1 to 19 is 190, and removing 7 and 13 leaves 170.

$ gcc -std=c17 -Wall -Wextra -o continue_for continue_for.c
$ ./continue_for
sum = 170

continue in a while loop

A while loop has no increment expression of its own, so the counter must be advanced by hand. In this example the increment is placed before the continue, which keeps the loop moving while the even values are skipped.

continue_while.c
#include <stdio.h>

int main(void) {

    int i = 0;
    int count = 0;
    int sum = 0;

    while (i < 30) {

        i++;

        if (i % 2 == 0) {
            continue;
        }

        count++;
        sum += i;
    }

    printf("%d odd numbers, sum %d\n", count, sum);

    return 0;
}

The loop visits every number from 1 to 30. Even numbers are dropped before the counting statements, so only the 15 odd numbers from 1 to 29 are counted, and their sum is 225. If the i++ were below the continue, the loop would never advance past the first even number.

$ gcc -std=c17 -Wall -Wextra -o continue_while continue_while.c
$ ./continue_while
15 odd numbers, sum 225

continue versus break

The two statements are easiest to compare side by side. The next program runs the same loop twice: the first time it leaves the loop at 4, the second time it skips only the value 4.

break_vs_continue.c
#include <stdio.h>

int main(void) {

    printf("break:   ");

    for (int i = 1; i <= 6; i++) {

        if (i == 4) {
            break;
        }

        printf(" %d", i);
    }

    printf("\ncontinue:");

    for (int i = 1; i <= 6; i++) {

        if (i == 4) {
            continue;
        }

        printf(" %d", i);
    }

    printf("\n");

    return 0;
}

The first line stops at 3, because break ends the loop. The second line contains 5 and 6 as well, because continue skips only the iteration for 4. Both statements affect the nearest enclosing loop, which matters in nested loops.

$ gcc -std=c17 -Wall -Wextra -o break_vs_continue break_vs_continue.c
$ ./break_vs_continue
break:    1 2 3
continue: 1 2 3 5 6

Skipping invalid input

A common real use of continue is filtering a list: items that cannot be parsed are skipped without stopping the whole loop. The next program converts five strings to numbers and adds only the valid ones. strtol reports where parsing stopped, so the check compares that position with the start of the string and with its terminator.

continue_parse.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {

    const char *items[] = { "12", "x", "7", "3a", "25" };
    int total = 0;

    for (int i = 0; i < 5; i++) {

        char *end;
        long value = strtol(items[i], &end, 10);

        if (end == items[i] || *end != '\0') {
            continue;
        }

        total += (int) value;
    }

    printf("total = %d\n", total);

    return 0;
}

"x" produces no digits at all, so end still points at the beginning of the string. "3a" produces the value 3, but parsing stops at the letter, so *end is not the terminator. Both cases are rejected by the condition, and only 12, 7 and 25 are added, for a total of 44.

$ gcc -std=c17 -Wall -Wextra -o continue_parse continue_parse.c
$ ./continue_parse
total = 44

A loop that never ends

Because continue jumps to the condition in a while loop, any counter update below it is skipped as well. The following program looks correct at a glance, but the counter stays at zero and the loop never finishes.

hang.c
#include <stdio.h>

int main(void) {

    int i = 0;

    while (i < 10) {

        if (i % 2 == 0) {
            continue;
        }

        printf("%d\n", i);
        i++;
    }

    return 0;
}

The first value of i is 0, which is even, so the body jumps back to the condition before reaching i++. The condition is true again, the value is still 0, and the program spins forever without printing anything. The timeout command is needed to stop it; exit status 124 means the program was killed after the time limit.

$ gcc -std=c17 -Wall -Wextra -o hang hang.c
$ timeout 2 ./hang
$ echo $?
124

Moving the work into the for header

A for loop avoids the trap, because its increment expression runs after continue as part of the loop machinery rather than as a statement in the body. The corrected program is a single loop with the counter in the header.

continue_fixed.c
#include <stdio.h>

int main(void) {

    for (int i = 0; i < 10; i++) {

        if (i % 2 == 0) {
            continue;
        }

        printf("%d\n", i);
    }

    return 0;
}

Now the even values are skipped and the odd ones are printed. When a while loop must use continue, put the update before the jump, as in the earlier example, or collect the condition at the top of the body so that no update can be bypassed.

$ gcc -std=c17 -Wall -Wextra -o continue_fixed continue_fixed.c
$ ./continue_fixed
1
3
5
7
9

Source

cppreference: continue statement
cppreference: statements
GCC: warning options

This tutorial covered the continue statement: how it skips one iteration in for, while and do-while loops, how it differs from break, and why a while loop with the counter below continue never ends. The statement that leaves a loop completely is described in the C break tutorial, and the loops themselves in C for and C while.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all C tutorials.