In the code below, please take a look at the part which has the 'continue' statement. What difference would it make if I remove 'continue' statement and I put nothing in the place of it.
在下面的代码中,请查看有“继续”语句的部分。如果我删除了“continue”语句,而没有放入任何东西来替代它,这又有什么区别呢?
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;
}
}
6 个解决方案
#1
5
It makes no difference in your code, but think about something like this:
你的代码没有什么不同,但是可以这样想:
(Pseudo code):
(伪代码):
for(int i = 10; i > -5; i--) {
if(i == 0)
continue;
printf("%d", 10/i);
}
#2
6
It would make no difference at all. The continue
statement is a jump statement. Basically, it jumps back to your loop not executing the code after it. Since the continue
statement is last executed in your loop it has no effect.
这没有什么区别。continue语句是一个跳转语句。基本上,它会跳转回您的循环,而不是在循环之后执行代码。因为continue语句最后在您的循环中执行,所以它没有效果。
#3
3
'continue' jumps straight to the end of the 'for' or 'while' bracket, "}". In your case, since there is nothing after the continue keyword anyways, it makes no difference.
“继续”一直跳到“for”或“while”的末尾,“}”。在您的例子中,因为在continue关键字之后没有任何内容,所以它没有区别。
Just to make it clear, normally, there IS 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 jumps straight to the closing "}", ignoring the code-3 and code-4. The next piece of code that gets executed here is code-condition.
执行“continue”行之后,它直接跳到结束的“}”,忽略代码3和代码4。这里执行的下一段代码是代码条件。
#4
2
In your case there would be no difference.
对你来说没有区别。
However continue will move to the next iteration of the loop immediately and skip any code after it if there is any.
然而,continue将立即转移到循环的下一个迭代,如果有任何代码,则跳过它之后的任何代码。
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
}
So the continue statement can be useful in some instances but for your code here it makes absolutely no difference whatsoever(maybe depending on the compiler it will but then it would only increase the size of the end executable adding another jmp instruction, or maybe not since it may unwind the loop fully).
所以继续声明在某些情况下很有用,但对你的代码是绝对没有任何区别(也许取决于编译器将但那只会增加的大小最终可执行添加另一个jmp指令,或也许不是因为它可能解除循环完全)。
#5
2
The continue
statement is useless in your example. It is supposed to be used to move code execution to the end of a loop:
在您的示例中,continue语句是无用的。它应该用于将代码执行移动到循环的末尾:
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
as "evaluate the loop condition and continue the loop if the condition is false", and you can think of break
as "exit the loop, ignoring the loop condition".
可以将continue看作“评估循环条件并在条件为false时继续循环”,可以将break看作“退出循环,忽略循环条件”。
Same thing with do-while
:
与延伸的一样:
do
{
...
if (foo)
continue;
if (bar)
break;
...
/* 'continue' moves execution to here */
}
while (is_true);
/* 'break' moves execution to here */
#6
2
I would simplify the block
我会化简block
for (i = 3; i < x; i += 2){
if (x % i == 0){
return 0;
break;
}else {
continue;
}
}
to
来
for (i = 3; i < x; i += 2){
if (x % i == 0){
return 0;
}
}
The additional lines don't change the behavior of the code.
附加的行不会改变代码的行为。
#1
5
It makes no difference in your code, but think about something like this:
你的代码没有什么不同,但是可以这样想:
(Pseudo code):
(伪代码):
for(int i = 10; i > -5; i--) {
if(i == 0)
continue;
printf("%d", 10/i);
}
#2
6
It would make no difference at all. The continue
statement is a jump statement. Basically, it jumps back to your loop not executing the code after it. Since the continue
statement is last executed in your loop it has no effect.
这没有什么区别。continue语句是一个跳转语句。基本上,它会跳转回您的循环,而不是在循环之后执行代码。因为continue语句最后在您的循环中执行,所以它没有效果。
#3
3
'continue' jumps straight to the end of the 'for' or 'while' bracket, "}". In your case, since there is nothing after the continue keyword anyways, it makes no difference.
“继续”一直跳到“for”或“while”的末尾,“}”。在您的例子中,因为在continue关键字之后没有任何内容,所以它没有区别。
Just to make it clear, normally, there IS 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 jumps straight to the closing "}", ignoring the code-3 and code-4. The next piece of code that gets executed here is code-condition.
执行“continue”行之后,它直接跳到结束的“}”,忽略代码3和代码4。这里执行的下一段代码是代码条件。
#4
2
In your case there would be no difference.
对你来说没有区别。
However continue will move to the next iteration of the loop immediately and skip any code after it if there is any.
然而,continue将立即转移到循环的下一个迭代,如果有任何代码,则跳过它之后的任何代码。
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
}
So the continue statement can be useful in some instances but for your code here it makes absolutely no difference whatsoever(maybe depending on the compiler it will but then it would only increase the size of the end executable adding another jmp instruction, or maybe not since it may unwind the loop fully).
所以继续声明在某些情况下很有用,但对你的代码是绝对没有任何区别(也许取决于编译器将但那只会增加的大小最终可执行添加另一个jmp指令,或也许不是因为它可能解除循环完全)。
#5
2
The continue
statement is useless in your example. It is supposed to be used to move code execution to the end of a loop:
在您的示例中,continue语句是无用的。它应该用于将代码执行移动到循环的末尾:
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
as "evaluate the loop condition and continue the loop if the condition is false", and you can think of break
as "exit the loop, ignoring the loop condition".
可以将continue看作“评估循环条件并在条件为false时继续循环”,可以将break看作“退出循环,忽略循环条件”。
Same thing with do-while
:
与延伸的一样:
do
{
...
if (foo)
continue;
if (bar)
break;
...
/* 'continue' moves execution to here */
}
while (is_true);
/* 'break' moves execution to here */
#6
2
I would simplify the block
我会化简block
for (i = 3; i < x; i += 2){
if (x % i == 0){
return 0;
break;
}else {
continue;
}
}
to
来
for (i = 3; i < x; i += 2){
if (x % i == 0){
return 0;
}
}
The additional lines don't change the behavior of the code.
附加的行不会改变代码的行为。