Get out of multiple loops?

user1153455 picture user1153455 · Jan 17, 2012 · Viewed 7k times · Source

Possible Duplicate:
Breaking out of a nested loop

I have this code

foreach (___)
{
    foreach (___)
    {
        foreach (___)
        {
            if (condition)
            {
                //break out of all loops
            }
        }
    }
}

But break only "breaks" the most inner loop (sorry for my english) and I want to leave them all...

I was thinking about :

  1. GOTO, but I've always been told to not use it
  2. A sort of boolean-flag

Is there any more elegant way ? like provided by C# ?

Thanks in advance for any help !

Answer

Robert Rouhani picture Robert Rouhani · Jan 17, 2012

A very elegant solution to this is to move the entire nest of loops to a separate method and return; when you want to break out of all loops.