How to Break out of multiple loops at once in C#?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-02-26T02:29:51Z Indexed on 2010/03/09 5:21 UTC
Read the original article Hit count: 186

Filed under:
|
|
|

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;

© Stack Overflow or respective owner

Related posts about break

Related posts about goto