C Continue Statement With Example

C Continue Statement With Example


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

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

 

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

 

int main() {

    int i = 0;

    do {

        if (i == 5) {

            continue;

        }

        printf("%d  ", i);

    } while (++i < 10);

    printf("\n");

    return 0;