OuterLoop:
while(true)
{
//random code
while(true)
{
//random code
if(something)
break OuterLoop;
}
}
C++ labeled break?
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:
The infinately abuseable goto command with a label after the loop is about as close as your going to get...
There's no command for this that I know of in C++ but, you can achieve the same thing with something like this:
- xeddiex
bool state = true;while ( state != false ) { while ( true ) if ( blah... ) { state = false; break; } }
- xeddiex
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 TradoneQuote: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.
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.
Building a scaffolding of flag-based breaks is clumsy.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement