如果达到结束条件,是否可以在C ++中退出一个for before时间?

编程入门 行业动态 更新时间:2024-10-11 19:19:22
本文介绍了如果达到结束条件,是否可以在C ++中退出一个for before时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否可以在结束条件(不同于重写次数的迭代次数)的C ++中结束for循环。例如:

I want to know if it is possible to end a for loop in C++ when an ending condition (different from the reacheing right number of iterations) is verified. For instance:

for (int i = 0; i < maxi; ++i) for (int j = 0; j < maxj; ++j) // But if i == 4 < maxi AND j == 3 < maxj, // then jump out of the two nested loops.

我知道这是可能在Perl与下一个LABEL或最后LABEL调用和标记的块,是它可以用C ++或者我应该使用一个while循环?

I know that this is possible in Perl with the next LABEL or last LABEL calls and labeled blocks, is it possible to do it in C++ or I should use a while loop?

谢谢。

推荐答案

您可以使用 return 关键字:将嵌套循环移动到子例程中,调用子例程以运行嵌套循环,子程序退出[all]循环。

You can use the return keyword: move the nested loop into a subroutine, invoke the subroutine to run the nested loops, and 'return' from the subroutine to exit [all] the loops.

更多推荐

如果达到结束条件,是否可以在C ++中退出一个for before时间?

本文发布于:2023-10-26 18:15:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   结束   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!