C Continue Statement

Continue statement skips the execution of further statements in the block for this iteration and continues with the next iteration.

We can use continue statement in While Loop, Do-while Loop and a For Loop.

In this tutorial, we shall go through examples illustrating C looping statements with continue statements in them.

Syntax of Continue

The syntax of continue statement is

continue;

Please note that continue statement can be used only inside a loop statement.

ADVERTISEMENT

Continue Statement in While Loop

In the following example, while loop tries to print numbers from 0 to 9. But during fourth iteration when i becomes 4, continue statement skips the execution of printing the number.

Refer C While Loop tutorial.

main.cpp

#include <stdio.h>

int main() {
    int i=0;
    while (i < 10) {
        if (i==4) {
            i++;
            continue;
        }
        printf("%d  ", i);
        i++;
    }
    printf("\n");
    return 0;
}

Output

0  1  2  3  5  6  7  8  9  
Program ended with exit code: 0

Also, the control variable i is not incremented because of the continue statement. So, we incremented i before executing the continue statement. If i not modified here, the while loop may become indefinite loop.

Continue Statement in Do-while Loop

In the following example, do-while loop tries to print numbers from 0 to 9. But during fifth iteration when i becomes 5, continue statement skips the execution of further statements in the loop.

Refer C Do-while Loop tutorial.

main.cpp

#include <stdio.h>

int main() {
    int i = 0;
    do {
        if (i == 5) {
            continue;
        }
        printf("%d  ", i);
    } while (++i < 10);
    printf("\n");
    return 0;
}

Output

0  1  2  3  4  6  7  8  9  
Program ended with exit code: 0

Continue Statement in For Loop

In the following example, we have written a for loop to print numbers from 0 to 9. Also, we have conditionally employed a continue statement to execute when the number reaches 7. When continue statement executes, the program control skips the execution of next statements in the loop, and continues with the next iteration.

Refer C For Loop tutorial.

main.cpp

#include <stdio.h>

int main() {
    for (int i = 0; i < 10; i++) {
        if (i == 7) {
            continue;
        }
        printf("%d  ", i);
    }
    printf("\n");
    return 0;
}

Output

0  1  2  3  4  5  6  8  9  
Program ended with exit code: 0

Continue Statement in Nested Loop

Continue statement affects only its surrounding loop. So, if you are using a continue statement in a nested loop, please note that it continues with next iteration only in its surrounding loop.

In the following example, we shall write a continue statement inside nested For Loop. The program prints a pattern.

main.cpp

#include <stdio.h>

int main() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (j > i) {
                continue;
            }
            printf(" *");
        }
        printf("\n");
    }
    printf("\n");
    return 0;
}

Output

*
 * *
 * * *
 * * * *
 * * * * *

Program ended with exit code: 0

Continue Statement in If Else Block

We know that continue statement could be written only inside a loop statements.

In the following example, we shall write a continue statement inside C If statement, with no loop surrounding the continue statement.

mian.cpp

#include <stdio.h>

int main() {
    int i = 10;
    if (i % 2 == 0) {
        continue;
    }
    return 0;
}

Output

'continue' statement not in loop statement

We got a compilation error stating that continue statement is not within a loop.

Conclusion

In this C Tutorial, we learned about continue statement, what it does to surrounding loop statements, with examples.