C continue statement Tutorial

  • Post author:
  • Post category:C

In this section, we will learn what the continue statement is and how to use it in C.

Note: before reading this section, we’re expecting that you already familiar with the `while-loop`, `for-loop` and the `if-statement`. If don’t, please read the related sections to learn more about them.

Continue Statement in C

The continue statement in C is used to skip the execution of the current iteration of a loop and start from next iteration in that loop.

Note that this doesn’t mean skipping the entire loop, though! Using the continue statement, we’re still in the same loop, but just the current iteration is ignored and the next one begins.

Example: for loop continue statement

#include <stdio.h>

int main() {

    for (int i = 0 ; i<10 ; i++){
        if (i <5){
            continue;
        }
        printf("The value of the i is: %d\n",i);
    }
    printf("End\n");
    return 0;
}

Output:

The value of the i is: 5

The value of the i is: 6

The value of the i is: 7

The value of the i is: 8

The value of the i is: 9

End

How does continue statement work?

Within the loop in the example above, there’s the `if` statement with the condition that if the value of the `i` variable is less than 5, the body of the `if` statement should be run.

So for the first 5 loops, the body of the `if` statement will run and because the body has the `continue` statement. The rest of instructions within the `for` loop will be skipped each time and the loop will start from the `update_statement` and `condition_statement` which is considered as the starting point of the next iteration.

This is why the first 5 numbers didn’t get print.

C continue Statement in nested loop

We can use the continue statement in a nested loop as well. In that case, only the iteration of the inner loop will be skipped! This means the enclosing loop will be in its current iteration and calling the `continue` statement won’t effect the enclosing loop.

Example: nested loop and continue statement

#include <stdio.h>

int main() {
    for (int i = 0; i<4; i++){
        for (int b = 0; b<40;b++){
            if (b<36){
                continue;
            }
            printf("The value of the b variable is: %d\n",b);
        }
        printf("i variable: %d\n",i);
    }
    return 0;
}

Output:

The value of the b variable is: 36

The value of the b variable is: 37

The value of the b variable is: 38

The value of the b variable is: 39

i variable: 0

The value of the b variable is: 36

The value of the b variable is: 37

The value of the b variable is: 38

The value of the b variable is: 39

i variable: 1

The value of the b variable is: 36

The value of the b variable is: 37

The value of the b variable is: 38

The value of the b variable is: 39

i variable: 2

The value of the b variable is: 36

The value of the b variable is: 37

The value of the b variable is: 38

The value of the b variable is: 39

i variable: 3

Note that here, the call to the `continue` statement occurs for as long as the value of the `b` variable is less than 36.

That’s why the call to the `printf()` function in the inner for loop didn’t happen until the value of the `b` variable went above 36 so that no other call to the continue statement happened.

Leave a Reply