What is the purpose of "continue" in C?

Asked

Viewed 2,778 times

25

  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 makes the function continue?

  • the execution of the code is exactly the same with 'continue' or without it, which is the advantage of having the continue in the code?

  • Explain this statement better, because it is not what it seems.

  • I executed the code with the 'continue' command and then I tried it without it, and the result was exactly the same, which seems to be no difference whether or not the command

  • You just took the continueand nothing else? The example is too bad to indicate its functionality. By the way the Tutorialspoint from where you got this is pretty bad. I’ll put better examples.

  • yes I only took the continue, if I could put another example was good. thank you.

  • Look at my answer.

  • I’ve seen your answer Mr Ramaral, your output is exactly what I get, with the command 'continue' or without it

  • It can’t be. I may be seeing wrong, but if I have the continue paragraph 15 is not shown.

  • it is true ! but without the continue the number 15 continues not to be represented

  • I went to test and verify that you are right. This is due to what a is incremented within the if == 15

  • I’ve already been cleared of the doubt, thank you Mr Ramaral

  • Here a simple example in php for you to understand the continue. It is skipping steps in this case.

Show 7 more comments

3 answers

23


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

When the code reaches continue he will be diverted to the final of the loop it is at the moment. Of the innermost loop if there are nested loops. All lines between the continue and the end of the loop will be ignored. So it makes no sense to use a continue out of a conditional structure, as this would cause the next lines of it to always be ignored.

At the end, depending on the loop it can check an existing condition, as is the case with 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 a for, it will go to the end, as there is nothing to do there, it will deviate to the beginning and there will operate the condition of the while or execute the step of for and the exit condition of it. If the condition indicates that the loop should be closed, it will deviate to the end again.

Finally understand a tag just before the first command after the loop block. So when you close the block, in your example, it goes to the return 0;

See your modified example to better demonstrate the effect of continue. It really doesn’t make a difference in certain flows.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference with continue.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference without continue.

Some people say that a for thus:

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

Is equal to while to:

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

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

Do you realize how different the execution will be because of continue? In the for the completion of the step is mandatory in the case of while the execution of the same step can be conditioned.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference with for.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference with while.

  • I understood well the function of continue with your examples, thank you

  • What is a bond?

  • @Ismaelmiguel is a loop, a repeat block.

  • Ah, a cycle... Why isn’t it called a cycle?

  • 4

    Because I am Brazilian :P

15

continue is one of the commands that can be used to modify the normal loop execution path.

The command continue passes control to the next iteration of loop, ignoring the remaining instructions. The next iteration begins by reevaluating the expression of the loop, whether the iteration is carried out in accordance with that assessment.

Another command is the break which causes the loop to be terminated immediately, continuing execution from the instruction after the key that closes the code of the loop. The code between break and key is ignored.

In the case of your code, continue, the assessment of the condition is true and the execution jumps to the line if( a == 15)

The purpose of the code is to make the number 15 not shown on the screen.

The output will be:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

13

Generally, in any language, the continue indicates that the current iteration of the repeat loop must "skip" to the next iteration.

Consider an example for printing even numbers only

Example in PHP:

for( $i = 0; $i < 10; $i++) {
    if ($i % 2 != 0) continue;
    echo $i;
}

Prints:

0
2
4
6
8
  • 1

    Simple answer with a great example +1.

  • 4

    @Guilhermenascimento, here’s a bad joke you could make: continue thus

  • 3

    @Wallacemaxters I suggest to give one break in this cycle of bad jokes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.