Exit Instance Of Loop

 

Hello,

I have like four nested while loops, I need to exit the top loop (that is the whole instance, all the loops nested inside the top loop as well) under a certain condition.

For example, the top loop is:

while (x < 4); { }

Now, I want to exit the top loop if x = 3, and proceed to x = 4

For some reason the break; operator won't work.

Will appreciate any help!

Thanks!

 
Saidar:

while (x < 4); { }

That semicolon shouldn't be there...

 
gordon:

That semicolon shouldn't be there...


Hehe yea I know sorry about that, I was just illustrating what I'm trying to do
 

You'll need to show us the whole chunk of code.

CB

 

The break command will only bust you up one level of nested loop chain. You need to tell each loop that your conditions are met and therefore multiple breaks would be required. Might help if you posted the actual code for us to look at.

 

Use continue operator:

if(x==3) continue;

 

Robo, I don't think so..........

Continue is used to "skip" an element of a loop but stay in the loop, so the loop will not execute the next statement but will rather return to the same point as 4.

 
kennyhubbard:

Robo, I don't think so..........

Continue is used to "skip" an element of a loop but stay in the loop, so the loop will not execute the next statement but will rather return to the same point as 4.

"Now, I want to exit the top loop if x = 3, and proceed to x = 4 "
 

robo, I see where you are going with this but I still beg to differ. Lets look at an example :-

for (int i = 0;i < 10;i++)   
   if(i = 5)continue;   
   Print(i);   
   if (i = 6) i = 1;
}

This is an endless loop that will print the numbers 0 to 6 excluding number 5. It will do this on and on and on..........forever, so the continue has not helped to get out of the loop at all.

for (int i = 0;i < 10;i++)   
   if(i = 5)break;   
   Print(i);   
   if (i = 6) i = 1;
}

This example, will print as far as 5 and then exit the loop, never to return. The point is, you need to bust out of each level of the loop and with 4 nested loops, I am not surprised a simple break is not doing the job.

Perhaps something like this :-

bool Found_It = false;

for (i = 0; i < 100;i++){
   for (1_2 = 0; i_2 < 50; i_2++){
      for(i_3 = 0; i_3 < 20; i_3++){
         for(i_4 = 0; i_4 < 5; i_4++){
            if(i_4 == 3){
               Found_It = true;
               break;
            }
         }
         if(Found_It)break;
      }
      if(Found_It)break;
   } 
   if(Found_It)break;
}

 
kennyhubbard:

The break command will only bust you up one level of nested loop chain. You need to tell each loop that your conditions are met and therefore multiple breaks would be required. Might help if you posted the actual code for us to look at.


Thanks I did not know that, I will try to do as you say.

Sorry people I did not want to post the code because the loop is at the moment 900 lines, I did not think anyone would actually read through all that code.

If I am still struggling after this I will post the structure of the loop, that would be enough to help me I think.

But thanks for the help everyone appreciate it

Reason: