如何打破嵌套循环? [英] How to break out of nested loops?

查看:281
本文介绍了如何打破嵌套循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for(int i = 0; i < 1000; i++) {
   for(int j = 0; j < 1000; i++) {
       if(condition) {
            // both the loop need to be break and control will go to stmt2
       }
   }

}

stmt2

如果我使用break语句,只会打破内部循环,我需要用一些标志来打破外环。但是,如果有很多嵌套循环中,code不会好看。

If I use break stmt, it will only break inner loop and I need to use some flag to break outer loop. But if there are many nested loop, the code will not look good.

是否有任何其他的方式来打破所有的循环? (请不要使用跳转语句

Is there any other way to break all the loop? (please don't use goto stmt)

推荐答案

什么:

if(condition) {
i = j = 1000;break;
}

这篇关于如何打破嵌套循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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