ios打破嵌套循环 [英] ios break nested loop

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

问题描述

如果我有一个while循环,并且while循环的内部 是一个for循环,那么我怎么能同时break两个循环?

If I have a while loop with a for loop inside of the while loop, how can I break both loops?

之所以这样做,是因为我发现自己想要的东西在一段时间后变得很有价值,这是因为我没有完成这些循环而获得的额外250毫秒.

I'm doing this because the extra 250ms I get from not completing these loops after I found what I want adds up to be valuable after a while.

伪代码:

while(alwaysTrue) {
    for(NSArray *arr in twoThousandItems) {
        if(IFoundWhatIWasLookingFor) {
            // assign some stuff here
            // break everything, not just the for loop.
        }
    }
}

推荐答案

goto是您的朋友.是的,那个goto.

This is where goto is your friend. Yes, that goto.

while(alwaysTrue) {
    for(NSArray *arr in twoThousandItems) {
        if(IFoundWhatIWasLookingFor) {
            // assign some stuff here
            // break everything, not just the for loop.
            goto BAIL;
        }
    }
}
BAIL:
NSLog(@"Freedom!");

另一种选择是使回路短路.

The other option is to have short circuits in your loops.

while(alwaysTrue && !found) {
    for(NSArray *arr in twoThousandItems) {
        if(IFoundWhatIWasLookingFor) {
            // assign some stuff here
            // break everything, not just the for loop.
            found = YES;
            break;
        }
    }
}

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

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