What is the purpose of "continue" in C?

Question:

  int main ()
    {
       /* local variable definition */
       int a = 10;

       /* do loop execution */
       do
       {
          if( a == 15)
          {
             /* skip the iteration */
             a = a + 1;
             continue;
          }
          printf("value of a: %d\n", a);
          a++;

       }while( a < 20 );

       return 0;
    }

What does the continue function do?

Answer:

The continue is a language command and not a function. This distinction is important.

When the code reaches continue it will branch to the end of the loop it is currently in. From the innermost loop if there are nested loops. All lines between the continue and the end of the loop will be ignored. So it doesn't make sense to use a continue outside of a conditional structure, as this would cause the lines following it to always be ignored.

In the end, depending on the loop, it can check an existing condition, as is the case in your example, and decide whether or not to continue executing the loop. If it continues, it will obviously jump to the beginning of the loop, the do .

If it is a while simple at the beginning of the loop or for he will go to the end, as there is nothing to do there, he will divert to the beginning and there will operate the condition while or perform the step for and the output condition his. If the condition indicates that the loop must be terminated, it will branch to the end again.

Understand by final a mark just before the first command after the loop block. So when you finish the block, in your example, it goes to return 0;

See your modified example to better demonstrate the effect of the continue . It doesn't really matter on certain streams.

See working on ideone . And on repl.it. Also posted on GitHub for future reference with continue .

See working on ideone . And on repl.it. Also put it on GitHub for future reference without continue .

Some people say that one for like this:

for (int x = 0; x < 10; x++) {
    printf("%d\n", x);
    if (x % 2 == 0) {
        x++;
        continue;
    }
}

It is the same as while a:

int x = 0;
while (x < 10) {
    printf("%d\n", x);
    if (x % 2 == 0) {
        x++;
        continue;
    }
    x++;
}

Regardless of the scope of x which is more local in the first case, this is beside the point in this example, they seem to do the same. It declares and initializes the variable, repeats until the variable reaches 10 and at the end of each step of the loop the variable is incremented by 1. In each step it checks if the value is even, if it is it increments one and jumps to the end of the loop ignoring what comes next.

See how the execution will be different because of the continue ? In for the execution of the end of the step is mandatory, in the case of while the execution of the same step can be conditioned.

See working on ideone . And on repl.it. I also put it on GitHub for future reference with for .

See working on ideone . And on repl.it. Also put it on GitHub for future reference with while .

Scroll to Top