Difference between "no statement" and continuation
In the code below, please take a look at the part that has a continue statement. what difference would it make if I remove the continue statement and I don't put anything in its place.
int prime_or_not(int x){
int i;
if (x == 1 || x == 2){
return 1;
}else if (x % 2 == 0){
return 0;
}else if (x > 2){
for (i = 3; i < x; i += 2){
if (x % i == 0){
return 0;
break;
}else {
continue;
}
}
return 1;
}else {
return 0;
}
}
source to share
'continue' jumps straight to the end of the 'for' or 'while' brackets, "}". In your case, since there is nothing after the continue keyword anyway, it doesn't matter.
Just to be clear, there is usually a big difference between "continue" and no statement. For example,
for (code-condition){
code-1
code-2
continue;
code-3
code-4
}
Once the "continue" line is executed, it goes straight to the closing "}", ignoring code-3 and code-4. The next piece of code that runs here is code.
source to share
There won't be any difference in your case.
However, the continuation will jump to the next iteration of the loop immediately and skip any code after it, if any.
Consider this:
int x;
for(x = 0; x < 100; x++){
if(x == 50){
//do something special
continue;
}
//code here is skipped for 50
//do something different for every other number
}
Therefore, the continue statement may be useful in some cases, but it makes absolutely no difference to your code here (maybe depending on its compiler, but then it will only increase the size of the final executable by adding another jmp statement, or maybe not since it can turn off the loop completely).
source to share
The operator is continue
useless in your example. It is supposed to be used to bring code to the end of the loop:
while (is_true)
{
...
if (foo)
continue;
if (bar)
break;
...
/* 'continue' makes code jump to here and continues executing from here */
}
/* 'break' makes code jump to here and continues executing from here */
You can think of continue
how to “evaluate the loop condition and continue the loop if the condition is false”, and you can think of break
how to “exit the loop ignoring the loop condition”.
Same with do-while
:
do
{
...
if (foo)
continue;
if (bar)
break;
...
/* 'continue' moves execution to here */
}
while (is_true);
/* 'break' moves execution to here */
source to share