Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I know I have to use break to exit from a for loop.
But if you have several loops within eachother then how do you exit all of the loops?
Do you have to use break at the end of each for loop or can I just use one break to exit all loops?

i.e.
C#
for (condition)
{
 for(condition)
 {
  for(condition)
  {
   if (myBoolean == false)
    {
     break;
    }
  }
 } 
}
Posted

It will depend on your logic of whether you would always break out all the loops, but you are going to need to implement some logic to break out of the loops.
You may need more than one variable depending on your logic, but this shows the basic principle

C#
bool breakAll = false;
for (condition1)
{
    for(condition2)
    {
        for(condition3)
        {
            if (myBoolean == false)
            {
                breakAll = true;
                break; // breaks condition3 for loop
            }
        }
        if (breakAll)
        {
            break; // breaks condition2 for loop
        }
    }

    if (breakAll)
    {
        break; // breaks condition1 for loop
    }
}
 
Share this answer
 
Comments
[no name] 14-Oct-11 10:06am    
Good answer!
I have had to do this in the past and you broke it down in a good simple form for the OP.
+5
A break statemnet will only exit from the current loop - there is no mechanism to exit from all loops. I would probably code them into a method, and use a return instead. There are purists who will tell that multiple exit points are the spawn of Satan however.
If you can't use return than this is one of the very, very few occaisions when a goto can be justified.
 
Share this answer
 
Comments
[no name] 14-Oct-11 10:09am    
Goto is the spawn of satan. Multiple returns are fine as long as they are clearly noted.

I think Reiss nailed it though. It just requires some extra flags.
arkiboys 14-Oct-11 10:25am    
Thank you
BobJanova 14-Oct-11 10:50am    
This is the way to do it. Having to put conditions all the way down your for chain is really ugly and unclear; I'd take a goto over that. But pulling it out into a method and using return is the best way.

I thought you could do this:

x: for(int i = 0; i < 100; i++)
for(int j = 0; j < 100; j++)
if(somecondition) break x;

Which language is that? I'm sure I didn't just make it up. But it doesn't work in C# (and nor does "continue x" which would be really useful sometimes).
You cannot use break to get out of nested loops.

You could either use a goto or place the code in a function and use a return to get out of it.
 
Share this answer
 
In connection with Reiss's post you can have multiple conditions for your ending (which can act as a break)

bool breakAll = false;
for (condition1 && !breakAll)
{
    for(condition2 && !breakAll)
    {
        for(condition3 && !breakAll)
        {
            if (myBoolean == false)
            {
                breakAll = true;
                break; // breaks condition3 for loop
            }
        }
    }
}


The gotcha is that if there is any logic after the embedded for loops it will also run.
For example

bool breakAll = false;
for (condition1 && !breakAll)
{
    for(condition2 && !breakAll)
    {
        for(condition3 && !breakAll)
        {
            if (myBoolean == false)
            {
                breakAll = true;
                break; //            }
        }
    }
    CallThisMethodEvenIfBreakAllIsTrue();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900