c++继续与中断

发布于 2024-11-15 19:47:01 字数 1753 浏览 4 评论 0原文

“继续”或“中断”后将执行哪条语句?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

要走就滚别墨迹 2024-11-22 19:47:01

继续:++j 然后如果 j 计数 然后statement2 否则statement3

中断:statement3

continue: ++j and then if j < count then statement2 otherwise statement3

break: statement3

不疑不惑不回忆 2024-11-22 19:47:01

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.

甜宝宝 2024-11-22 19:47:01

continue 结束当前迭代,实际上等同于:

for(int i = 0; i < count; ++i)
 {
     // statement1                                                                                                                                                                                                                          
     for(int j = 0; j < count; ++j)
     {
         //statement2                                                                                                                                                                                                                       
         if(someTest)
             goto end_of_loop;
end_of_loop:
     }
     //statement3                                                                                                                                                                                                                           
 }

break 退出循环:

for(int i = 0; i < count; ++i)
 {   
     // statement1                                                                                                                                                                                                                          
     for(int j = 0; j < count; ++j)
     {   
         //statement2                                                                                                                                                                                                                       
         if(someTest)
             goto after_loop;
     }
after_loop:
     //statement3                                                                                                                                                                                                                           
 }

continue ends the current iteration, virtually it is the same as:

for(int i = 0; i < count; ++i)
 {
     // statement1                                                                                                                                                                                                                          
     for(int j = 0; j < count; ++j)
     {
         //statement2                                                                                                                                                                                                                       
         if(someTest)
             goto end_of_loop;
end_of_loop:
     }
     //statement3                                                                                                                                                                                                                           
 }

break exits the loop:

for(int i = 0; i < count; ++i)
 {   
     // statement1                                                                                                                                                                                                                          
     for(int j = 0; j < count; ++j)
     {   
         //statement2                                                                                                                                                                                                                       
         if(someTest)
             goto after_loop;
     }
after_loop:
     //statement3                                                                                                                                                                                                                           
 }
苏佲洛 2024-11-22 19:47:01

假设循环不在最后一次迭代中,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.

如果没结果 2024-11-22 19:47:01

继续:这取决于。 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?

不弃不离 2024-11-22 19:47:01
  1. 对于 continue,使用 i,j+1 的新 i,j 值执行内循环

  2. 对于break,内循环以新的 i,j 值 i+1,0 执行

,当然如果满足边界条件

  1. For continue, innerloop is executed with new i,j values of i,j+1

  2. For break, innerloop is executed with new i,j values of i+1,0

ofcourse if boundary conditions are satisfied

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文