C++:中断主循环
我正在准备一些代码:
for(int a = 1; a <= 100; a++) //loop a (main loop)
{
for(int b = 1000; b <= 2000; b++) //loop b
{
if(b == 1555)
break;
}
for(int c = 2001; c <= 3000; c++) //loop c
{
.
.
.
}
}
我想通过在 b 循环中使用 break;
语句(循环变量 int a
)来中断主循环(循环变量 int a
) b)。
我该怎么做呢?
I am preparing some code:
for(int a = 1; a <= 100; a++) //loop a (main loop)
{
for(int b = 1000; b <= 2000; b++) //loop b
{
if(b == 1555)
break;
}
for(int c = 2001; c <= 3000; c++) //loop c
{
.
.
.
}
}
I want to break the main loop (loop variable int a
) by using a break;
statement in the b loop (loop variable int b
).
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用
goto
。Use a
goto
.要么执行以下四件事之一:使用
goto
、使用throw
、使用标志或重构。许多人不同意使用
goto
,但有时这是一个干净的解决方案。 (大多数时候,它不是,但它的存在是有原因的。)但是,我发现使用goto
需要重构。第二种解决方案是抛出一些特殊异常,然后捕获它在主循环之外。这是对异常系统的滥用,基本上是一个更糟糕的
goto
;使用goto
代替它。第三种解决方案是使用 某种。这基本上是一个“更安全”的
goto
,但有些人可能会认为它有点难看。 (尤其是多级别。尽管在这种情况下,您担心的是代码有多难看。)我推荐的解决方案是重构。无论你在做什么,都太过分了。您应该将内部循环移至函数中,然后调用该函数。返回主循环只是从该函数返回。 (换句话说,“我的工作完成了。”)
Either do one of four things: use
goto
, usethrow
, use a flag, or refactor.Many will disagree with using
goto
, but sometimes it's a clean solution. (Most times, it isn't, but it exists for a reason.) However, I find the use ofgoto
warrants a refactor.The second solution is to throw some special exception and then catch it just outside the main loop. This is an abuse of the exception system and basically a worse
goto
; use agoto
instead of this.The third solution would be to use a flag of some sort. This is basically a "safer"
goto
, but some might argue it's a bit uglier. (Especially with multiple-levels. Though in such a case your concern is how ugly your code is.)The solution I would recommend is refactor. Whatever you're doing, it's too much. You should move the inner loops into a function, and call that function. Returning to the main loop is simply a return from that function. (In other words "My work is done.")
我建议将您的代码重构为函数。然后,您可以从该函数
返回
,而不是使用break
:这(或者甚至可能是更复杂的代码重构)应该有助于提供干净、优雅的解决方案。或者,如果您只想快速修复,可以使用条件变量:
其他人建议使用
goto
。虽然这是另一个快速解决方案,但我强烈建议不要这样做,特别是如果您在严格的环境中工作,代码将在未来几年内接受同行评审和使用。在我看来,
goto
方法比函数/返回重构更难维护,特别是当其他人对代码进行更改时。另外,您还必须向团队中偶然发现代码的其他人证明goto
的合理性。I recommend refactoring your code into a function. Then you can just
return
from that function instead of usingbreak
:This - or perhaps even a more involved refactoring of your code - should lend itself to a clean, elegant solution. Alternatively, if you just want a quick fix you could use a condition variable:
Others have suggested using
goto
. While that is another quick fix, I strongly recommend against it, especially if you are working in a rigorous environment where the code will be peer reviewed and used for years down the road.In my opinion the
goto
approach is a bit harder to maintain than a function/return refactoring, especially later on when someone else makes changes to the code. Plus you will have to justify thegoto
to anyone else on the team that happens to stumble onto the code.如果合适的话,您可以创建一个内容为循环的函数,并使用 return。
If it is appropriate, you could make a function who's contents are the a loop, and use return.
一次退出两个此类循环的唯一方法是 goto 或 throw 或 return 和 throw /code> 和
return
可能不合适(特别是throw
,如果条件不例外)。或者,您可以设置某种条件(bool breakout;
),并在为真时继续中断。The only way to head out of two such loops at a time is a
goto
or athrow
or areturn
, andthrow
andreturn
may not be appropriate (particularlythrow
, if the condition isn't exceptional). Alternately, you could set some sort of condition (bool breakout;
), and keep breaking if it's true.\(◕◡◕)/
\ (◕ ◡ ◕) /
使用
goto
:设置每个循环测试的哨兵值:
Use a
goto
:set a sentinel value tested by each loop:
一种简单的策略是将循环放在单独的函数中,并在选定的点执行返回:
也可以使用返回值或函数的引用参数返回任何类型的结果。
One simple strategy is to put the loop in a separate function and do a return at the selected point:
Any kind of result may also be returned with a return value, or with a reference parameter for the function.
理想的方法是重构代码,以便不再需要如此复杂的嵌套循环结构。根据代码的其余部分的外观,您的
b
和c
循环可能会成为单独的函数(如果不是整个a
)环形。既然它看起来像循环
b
和c
遍历相邻范围,为什么不将它们组合起来并稍微减少循环嵌套呢?您还可以使用条件变量来代替
goto
。如果您想跳出旧的b
循环,但仍处理旧的c
循环,只需在旧的内设置
循环代码。count = 2001
>b理想情况下,您至少能够将其重构为更像
函数
process_inner_loop
包装原始两个循环并返回非零(如果您想跳出封闭循环)的地方。现在,您可以简单地return 1;
,而不是使用goto
或条件变量。The ideal way would be to re-factor your code so that you no longer need such a complicated nested-loop structure. Depending on what the rest of your code looks like, your
b
andc
loops may be candidates for becoming individual functions, if not the entirea
loop.Since it looks like loops
b
andc
iterate over adjacent ranges, why not combine them and reduce your loop nesting a bit?You can also use a condition variable instead of the
goto
. If you want to break out of the oldb
loop but still process the oldc
loop, simply setcount = 2001
inside the oldb
loop code.Ideally, you would at least be able to re-factor this to something more like
where the function
process_inner_loop
wraps up your original two loops and returns non-zero if you want to break out of the enclosing loop. Now, instead of usinggoto
or condition variables, you can simplyreturn 1;
.使用这种模式
Use this kind of pattern