如何在 Objective-C 中跳出两个嵌套的 for 循环? [英] How can I break out of two nested for loops in Objective-C?
本文介绍了如何在 Objective-C 中跳出两个嵌套的 for 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个这样嵌套的 for 循环:
I have two for loops nested like this:
for(...) {
for(...) {
}
}
我知道有一个 break
语句.但是我很困惑它是中断了两个循环还是只中断了它被调用的那个?一旦我发现迭代更多次没有意义,我就需要打破这两个.
I know that there is a break
statement. But I am confused about if it breaks both loops or just the one in which it was called? I need to break both ones as soon as I see that it doesn't make sense to iterate more times over.
推荐答案
break 跳出一个循环,但您可以在外循环中添加一个检查,当内循环中断时会中断.
break breaks out of one loop, but you can add a check to the outer loop which breaks when the inner breaks.
bool dobreak = false;
for ( ..; !dobreak && ..; .. ) {
for ( ... ) {
if (...) {
dobreak = true;
break;
}
}
}
这篇关于如何在 Objective-C 中跳出两个嵌套的 for 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文