Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Muppi

Breaking from all loops?

This topic is 5375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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;
}
}
}

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!