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
.
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?
– kingwarrior05
Explain this statement better, because it is not what it seems.
– Maniero
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
– kingwarrior05
You just took the
continue
and 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.– Maniero
yes I only took the continue, if I could put another example was good. thank you.
– kingwarrior05
Look at my answer.
– ramaral
I’ve seen your answer Mr Ramaral, your output is exactly what I get, with the command 'continue' or without it
– kingwarrior05
It can’t be. I may be seeing wrong, but if I have the
continue
paragraph 15 is not shown.– ramaral
it is true ! but without the continue the number 15 continues not to be represented
– kingwarrior05
I went to test and verify that you are right. This is due to what
a
is incremented within theif == 15
– ramaral
I’ve already been cleared of the doubt, thank you Mr Ramaral
– kingwarrior05
Here a simple example in php for you to understand the continue. It is skipping steps in this case.
– Ivan Ferrer