Jump to content
  • Advertisement
Sign in to follow this  
CuppoJava

C++ labeled break?

This topic is 4485 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, i'm just wondering if there is an equivalent for this common Java command in C++. How to I break out of an outer level loop? eg. in java:
OuterLoop:
while(true)
{
  //random code
  while(true)
  {
    //random code
    if(something)
      break OuterLoop;
  }
}

Share this post


Link to post
Share on other sites
Advertisement
There's no command for this that I know of in C++ but, you can achieve the same thing with something like this:


bool state = true;

while ( state != false )
{
while ( true )
if ( blah... )
{
state = false;
break;
}
}



- xeddiex

Share this post


Link to post
Share on other sites
Quote:
Original post by CuppoJava
hi,
i'm just wondering if there is an equivalent for this common Java command in C++.
How to I break out of an outer level loop?

eg. in java:

OuterLoop:
while(true)
{
//random code
while(true)
{
//random code
if(something)
break OuterLoop;
}
}

or depending on the situation, you can do this:


while(true)
{
//random code
while(true)
{
//random code
if(something){
break;
}
break;
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Tradone
Quote:
Original post by CuppoJava
hi,
i'm just wondering if there is an equivalent for this common Java command in C++.
How to I break out of an outer level loop?

eg. in java:

OuterLoop:
while(true)
{
//random code
while(true)
{
//random code
if(something)
break OuterLoop;
}
}

or depending on the situation, you can do this:


while(true)
{
//random code
while(true)
{
//random code
if(something){
break;
}
break;
}
}

The second break will never be reached if you break above it.

Share this post


Link to post
Share on other sites
I didn't realize that was possible in Java. It seems a little strange, too, seeing as how it boasts of being safer than C++. breaks are restricted gotos, as they only take program flow in one directly, directly after the given loop. A labeled break is slightly more dangerous in that the # of loops broken can be greater than 1.

I guess I can add that to one of the few features I like about Java.

Share this post


Link to post
Share on other sites
Either hoist your inner loops into a separate function and use a return, or use a goto.

Building a scaffolding of flag-based breaks is clumsy.

Share this post


Link to post
Share on other sites
Thanks for the comments guys.
Yeah, the only suggestion that applied to my situation was the flag check solution.
Unfortunately, this is quite an intricate piece of code with four layers of loops, so flag checking would result in an unreadable mess, so I opted to pull my code into methods, using returns as a break substitute.

Thanks for the ideas guys.

Share this post


Link to post
Share on other sites
If I saw a programmer use cumbersome (and easily buggy) flags where a goto would be clearer, I would recommend changing to the goto in a code review. Really, "named breaks" is one of the cases where goto is usually better than the alternatives.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!