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

52,450

Solution 1

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

Solution 2

Introduce another control flag and put it in all your nested while condition like below. Also replaces the while(true) condition you have with that

bool keepLooping = true;
while (keepLooping) {
    // ...
    while (shouldCont && keepLooping) {
        // ...
        while (shouldGo && keepLooping) {
            // ...
            if (timeToStop) { 
                keepLooping  = false;
                break; // break out of everything?
            }
        }  
    }
}

Solution 3

Goto is only hideous when abused. To drop out of the innermost loop of some nesting it's acceptable. BUT... one has to ask why there is so much nesting there in the first place.

Short answer: No.

Solution 4

If you want to break out of an entire method, then use the code below. If you only want to break out of a series of loops within a method without breaking out of the method, then one of the answers that have already been posted will do the job.

if (TimeToStop)
{
   return;
}
Share:
52,450
Nick Heiner
Author by

Nick Heiner

JS enthusiast by day, horse mask enthusiast by night. Talks I've Done

Updated on July 05, 2022

Comments

  • Nick Heiner
    Nick Heiner almost 2 years

    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;
    
  • Rich
    Rich about 14 years
    +1. Basically, every control structure is goto underneath; just fitted into a specific purpose. For everything that isn't covered by those special purposes there is no other “elegant” option than goto.
  • Mark Carpenter
    Mark Carpenter about 14 years
    +1 Very nice! :) I posted something sort of similar to this, but yours was better, so I deleted it.
  • Mark Carpenter
    Mark Carpenter about 14 years
    IMO GOTO is only hideous when used outside assembly programming. To each his own, though :)
  • Mark Carpenter
    Mark Carpenter about 14 years
    I wish I could vote for this more than once. The 'if(timeToStop && keepLooping)' can probably lose the 'keepLooping' part, other than that, brilliant!
  • matt
    matt about 14 years
    I don't get it, how is this any easier to read than goto? you have to come up with a variable name that describes the intention of it to break out of all the loops, why don't you just put the same effort into coming up with a name for a goto label? this is after all slower. you have to look for everywhere that keepLooking is used, not just one label.
  • matt
    matt about 14 years
    that's a hack, not what functions are for, most people who hate goto consider early returns in functions just as bad. I disagree with such broad sweeping statements, but I would say that using goto is just as valid as this solution to the problem, though certainly not as neat.
  • Fadrian Sudaman
    Fadrian Sudaman about 14 years
    Edited to remove the unnecessary keepLooping on the last loop like suggested by @Pwninstein
  • Fadrian Sudaman
    Fadrian Sudaman about 14 years
    @Matt - Certainly you can do the same and personal preference I will use the above over GOTO. In this scenario, GOTO can be just as readable, but in many cases GOTO can make code very unreadable and really not advisable in structure programming environment. Most coding guidelines I have seen have been stirring away from it and why break it for a simple structure problem like above? The only time I have used GOTO and find it really useful is when I wrote a complex parser where I need to interpret token against hundreds of keywords with some interesting repeating logic.
  • matt
    matt about 14 years
    lol, nothing tests the limits of structured programming more than parsing.
  • Michael Anderson
    Michael Anderson about 14 years
    I disagree entirely matt. The number of people who believe in requiring a single exit from functions is much smaller than those who hate gotos. The reason is that in many languages goto doesn't perform correct resource cleanup (dont know about C# for sure) but return does. Also IMHO functions are for grouping, scoping and simplifying related code. That's exactly what this does.
  • Stewart
    Stewart over 8 years
    From a structured programming point of view, goto is just a low-level construct on which the high-level constructs of selection and iteration are implemented. Return, break and continue break the structured programming module and are effectively just special cases of goto, but nonetheless I think the usefulness of them tends to win the day.
  • Russell Hankins
    Russell Hankins over 7 years
    I agree with this answer. One reason to put the nested loops into a separate function is that the original function is getting too big. If the original function is this big, the nested loops is a good place to break the function up into two functions. You have task A which is a series of nested loops, then you want to go to task B after the loops have found what they're looking for. If you feel having multiple returns is a problem, it's a sign your function is too big.
  • Calmarius
    Calmarius over 5 years
    We are in 2018 C# have all the bells and whistless... But still no multilevel break or continue...
  • TravisO
    TravisO about 5 years
    If your loops are for statements, all you need to do is take the iterating value and set it to or higher than the max, you can do it for the current for but also each outer for... I had to do this when I had four nested FOR statements and needed to break out of the inner most two.
  • İpek
    İpek over 2 years
    But what if one of the upper whiles is foreach? Then even though foreach is over you will be keep looping until timeToStop occurs right?