C++ labeled break?

Started by
37 comments, last by Conner McCloud 18 years ago
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;
  }
}
Advertisement
The infinately abuseable goto command with a label after the loop is about as close as your going to get...
Steven ToveySPUify | Twitter
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
one..
yes as eddie mentioned.
you can use Flags.
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;  }}

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.
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.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement