c++继续与中断
“继续”或“中断”后将执行哪条语句?
for(int i = 0; i < count; ++i)
{
// statement1
for(int j = 0; j < count; ++j)
{
//statement2
if(someTest)
continue;
}
//statement3
}
for(int i = 0; i < count; ++i)
{
// statement1
for(int j = 0; j < count; ++j)
{
//statement2
if(someTest)
break;
}
//statement3
}
Which statement will be executed after "continue" or "break" ?
for(int i = 0; i < count; ++i)
{
// statement1
for(int j = 0; j < count; ++j)
{
//statement2
if(someTest)
continue;
}
//statement3
}
for(int i = 0; i < count; ++i)
{
// statement1
for(int j = 0; j < count; ++j)
{
//statement2
if(someTest)
break;
}
//statement3
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
继续:
++j
然后如果j
计数
然后statement2
否则statement3
中断:
statement3
continue:
++j
and then ifj < count
thenstatement2
otherwisestatement3
break:
statement3
continue 直接跳转到最内层循环的顶部,其中将执行每次迭代代码和连续性检查(
for
循环的第 3 部分和第 2 部分)。Break 直接跳转到最内层循环之后,而不做任何改变。
可能更容易想到前者跳转到最内循环的右大括号,而后者则跳过它。
Continue jumps straight to the top of the innermost loop, where the per-iteration code and continuance check will be carried out (sections 3 and 2 of the
for
loop).Break jumps straight to immediately after the innermost loop without changing anything.
It may be easier to think of the former jumping to the closing brace of the innermost loop while the latter jumps just beyond it.
continue
结束当前迭代,实际上等同于:break
退出循环:continue
ends the current iteration, virtually it is the same as:break
exits the loop:假设循环不在最后一次迭代中,statement2 将在 continue 之后执行。
语句3将在break之后执行。
“继续”(顾名思义)继续循环,同时跳过当前迭代中的其余语句。
'break' 中断并退出循环。
statement2 will execute after the continue, given that the loop was not in the last iteration.
statement3 will execute after the break.
'continue' (as the name suggests) continues the loop, while skipping the rest of the statements in the current iteration.
'break' breaks and exits from the loop.
继续
:这取决于。 continue 语句将执行 for 循环的“增量”部分,然后执行“测试”部分,然后决定是执行下一次迭代还是退出循环。所以它可能是语句 2 或 3。
Break
:语句 3。顺便说一句,这是作业吗?
Continue
: It depends. The continue statement will execute the 'increment' part of the for-loop, then the 'test' part, and then decide whether to execute the next iteration or leave the loop.So it could be statement 2 or 3.
Break
: statement 3.Btw, is this homework?
对于 continue,使用 i,j+1 的新 i,j 值执行内循环
对于break,内循环以新的 i,j 值 i+1,0 执行
,当然如果满足边界条件
For continue, innerloop is executed with new i,j values of i,j+1
For break, innerloop is executed with new i,j values of i+1,0
ofcourse if boundary conditions are satisfied