Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i exit from internal loop in nested loop and then return to parent loop to complete it ?
i need the internal loop in such a condition return to parent loop to complete it.
IF i use break or continue will exit from all loops
Posted
Updated 9-Mar-12 0:35am
v2

Use break. break will exit from the loop it is written inside and all other loops will just work fine.
 
Share this answer
 
<b>break</b> is the best way to come out from the loop if your condition is matched
 
Share this answer
 
Hi Ahmed,

Just use a boolean Flag - easy, extensible (you can repeat the pattern with any loop "nesting-depth".

C#
bool bBreak = false; ;
for (int i = 0; i < 10; i++)
{
    for (int i = 0; i < 20; i++)
    {
        if (i == 3) // break condition met?
        {
            bBreak = true; // set a flag to tell the outer loop(s) to break
            break; // break inner loop
        }
    }
    if (bBreak) // check if inner loop set break
        break; // break outer loop
}
 
Share this answer
 
Comments
aodeh 9-Mar-12 7:54am    
thanks johnnesnestler
ProEnggSoft 9-Mar-12 12:00pm    
I think this is a good alternative. My 5
You can use break to exit a loop. This will only exit the current (inner) loop.

Good luck!
 
Share this answer
 
Comments
aodeh 9-Mar-12 6:19am    
Thanks for answers , but i need the internal loop in such a condition return to parent loop to complete it.
IF i use break or continue will exit from all loops .
CPallini 9-Mar-12 6:39am    
You are wrong, from the documentation: "The break statement terminates the closest enclosing loop".
E.F. Nijboer 9-Mar-12 11:07am    
If break will exit all loops then there is something else wrong in your code I'm afraid. Try to step through it using the debugger and put a watch on all relevant variables to see what is happening.
CPallini 9-Mar-12 6:39am    
My 5.
aodeh 9-Mar-12 6:43am    
thanks, ok after break i want to return to parent (for) to complete it , not to complete sequential code .
C#
for (int i = 0; i < 15; i++)
           {
               for (int j = 0; j < 5; j++)
               {
                   //condition whatever you want the if statement to satisfy
                   if (j == 2)
                       j = 5;
               }
           }



Once you reassign the counter value the inner loop is then complete because it no longer needs to loop thus go into the parent loop again.
 
Share this answer
 
Comments
johannesnestler 9-Mar-12 7:22am    
I consider this as hack - what you do if your nesting depth is more than two? how will the second loop know to break? - look at my solution - with a simple flag... Don't you agree?
aodeh 9-Mar-12 7:56am    
Thanks Dean Oliver

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