Breaking from all loops?

Started by
35 comments, last by Muppi 20 years ago
Hi, Ive got a deeply nested loop, deep inside my loop, i have something critical, so if this is excecuted, i want all the loops to break, like a quit, do you guys have any quick way of doing this?
Things i felt yesterday, don''t matter any more.
Advertisement
I had this problem to. If you''re talking about C++, I used the "goto" statement to get out, but apparently "goto" is not highly recommended.
"Learn as though you would never be able to master it,
hold it as though you would be in fear of losing it" - Confucius
What language?

If Java or C++, you can use exceptions. Or, if you don''t mind people looking at you funny, you can use goto.
Using goto isn''t a good idea to exit loops. The reason is because loops use stack space, and when you use GOTO to exit the loop, the stack space isn''t freed. This will eventually lead to you using up all of your stack space.

I''ve had this problem myself. I haven''t yet figured out a simple way to exit the loops freely. I always have to set a variable to true, and the BREAK upon that variable being true within all the loops.

This is why goto isn''t recommended. It''s a powerful command that has to be used properly to gain it''s awesome advantages.
I''m not sure if this is correct use of c++, but if your outer loop resides within its own function, just return from that function.
Umm... I always thought that variables local to a function were all allocated from the stack at the start of a function. EG:
void moo(int cow){ //inner_a, a, b, and the local copy of cow all get allocated here  if (cow)  {    int a = 4; //or here?    if (cow > 6)    {      int a = 6; //local override of a      int b = 3; //or here?    }  }}

If that''s right (and I''d like to know if it isn''t) then you can''t cause stack leaks using goto, since goto only works within functions. Have you actually had stack overflows caused provably by this?
You make a good point.
Since goto only works within functions, using it should be fine. I still have some BASIC blood running through my brain, and had problems back in the day doing that with basic, but basic lets you jump out of functions, and c dosen''t. So please excuse my assumption. :D
It depends on the loop, but you could set a boolean that tells the outer loops to break. You could also use exceptions, to do a throw and catch when you want to break.

Ultimately though it is usualy better to re-structure your loops so that you dont need to do this (if you can). Or use ''return'', like xCube suggested..
java allows "labeled breaks". or should it be called labeled loops... anyway it works like this :

outer:for (int i = 0; i < 10; i++) {   for (int j= 0; j< 10; j++) {      if (...) {         break outer;      }   }}
stayin = true;for (i = 0; (i < 10) && stayin; ++i)  for (j = 0; (j < 10) && stayin; ++j)    for (k = 0; (k < 10) && stayin; ++k)    {      if (ImNotHappyHere)      {        DoSomething();        stayin = false;      }    }  


or just set all i,j,k to 11

lonesock

Piranha are people too.

[edited by - lonesock on March 30, 2004 3:57:21 AM]

This topic is closed to new replies.

Advertisement