Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
hello friends,

suppose we have nested loops as below....
for (int y = 0; y < someLength; y++)
            {
                for (int x = 0; x <somelength;x++)          
                 {
                    if (condition becomes true)
                    {
                       Break both loops inner loop and outer loop.......
                    }
                }
            }

is there any standard way to break both loops..........when condition becomes true in just Inner loop

as we know.... can break loop in which we are nicely...... using break...

Thanks........
Posted
Updated 24-May-11 23:46pm
v5

The only thing I can think of is to put the nested loops into their own method, and if the appropriate condition is met, simply return from the method.

C#
private void LooperMethod()
{
    for (int i = 0; i < somevalue; i++)
    {
        for (int j = 0; j < someOtherValue; j++)
        {
            if (conditionExists)
            {
                if (specialConditionExists)
                {
                    return;
                }
                break;
            }
            if (outerConditionExists)
            {
                break;
            }
        }
    }
}

If you can't abstract the loop code into its own method, your only real choice is to maintain a variable that indicates when to break the outer loop:

C#
bool breakOuter = false;
for (int i = 0; i < somevalue; i++)
{
    for (int j = 0; j < someOtherValue; j++)
    {
        if (conditionExists)
        {
            if (specialConditionExists)
            {
                breakOuter = true;
            }
            break;
        }
        if (outerConditionExists || breakOuter)
        {
            break;
        }
    }
}
 
Share this answer
 
v2
Comments
Pritesh Aryan 25-May-11 6:01am    
Thank you so much.....
i think i will go for first one....that is......
nested loops into their own method.......
Thanks........
Sergey Alexandrovich Kryukov 25-May-11 10:46am    
You know what? This is the least universal solution... but most robust.
I would avoid solutions like the one by hitesh_tech.

There is another solution I would use: throw a special exception and catch in on a desired level. In Delphi there was event an Exception type preserved specially for this technique. I would use it.
But I'm not posting it you know why? To avoid getting in flame wars. :-)

My 5.
--SA
#realJSOP 25-May-11 11:38am    
I try not to throw exceptions unless an exception has actually happened. I don't feel like throwin an exception is the correct approach in this case.
Sergey Alexandrovich Kryukov 28-May-11 1:13am    
This is questionable issue, one of the sources for flame wars. I would say it's fine, Borland also offers it as regular technique. You say "I don't feel", not so rational explanation. But I can understand you. I could be very biased as was the author of a very early implementation of fully-fledged exception handling (in inline assembly), when the technique was hot yet available in C++ (at that time, only in CLU and Ada, maybe somewhere else).
--SA
below code will not return but come out of loop and will execute next code after loops

C#
bool loopbreak=new bool()  ;
           loopbreak = false;
           for (int y = 0; y < 10 && loopbreak==false; y++)
           {
               for (int x = 0; x <10;x++)
                {
                   if (x==5)
                   {
                      loopbreak=true;
                      break;
                   }

               }
          }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-May-11 10:48am    
Will work, but I would prefer a simple solution by John. Please see my comments.
--SA
try this code
C#
for (int i = 0; i < 5; i++)
           {
               for (int j = 0; j < 5; j++)
               {

                   if (i == 1)
                   {
                       return;
                   }
                   MessageBox.Show("j" + j.ToString());
               }
               MessageBox.Show("i" +i.ToString());
           }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-May-11 10:49am    
You should show the method you're returning from, otherwise it's not quite clear. This is what I would prefer to do.
Will you improve you solution like that (see also my comments to John's solution)?
--SA

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