如何在C#中跳出多重循环的一次? [英] How to break out of multiple loops at once in C#?

查看:520
本文介绍了如何在C#中跳出多重循环的一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有嵌套循环,我想打破所有的人都在一次?



 ,而(真){
// ...
,而(shouldCont){
// ...
,而(shouldGo){
// ...
如果(timeToStop){
中断; //打破了一切?
}
}
}
}

在PHP,花费循环打出来的数的参数。可这样的事情在C#做什么?



有关的东西狰狞,像转到

$什么b
$ b

  //最内层循环
转到BREAK
// ...
BREAK:打破;打破;打破;


解决方案

提取您的嵌套循环到一个函数,然后就可以使用return从任何地方走出循环,而不是休息。


What if I have nested loops, and I want to break out of all of them at once?

while (true) {
    // ...
    while (shouldCont) {
        // ...
        while (shouldGo) {
            // ...
            if (timeToStop) {
                break; // Break out of everything?
            }
        }
    }
}

In PHP, break takes an argument for the number of loops to break out of. Can something like this be done in C#?

What about something hideous, like goto?

// In the innermost loop
goto BREAK
// ...
BREAK: break; break; break;

解决方案

Extract your nested loops into a function and then you can use return to get out of the loop from anywhere, rather than break.

这篇关于如何在C#中跳出多重循环的一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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