如果达到结束条件,是否可以在C ++中退出一个for before时间? [英] Is it possible to exit a for before time in C++, if an ending condition is reached?

查看:164
本文介绍了如果达到结束条件,是否可以在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时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆