::break

Hi all,
say I have this code:
1
2
3
4
5
6
7
8
for ( ; ; ) {
    for ( ; ; ) {
        if ( /* some condition... */ ) {
            ::break; // I want to break from the outermost for loop. Is there
                     // some way to do that?
        }
    }
}

Thanks!
Jtpt break not ::break
No, not using break there isn't.
You could always enter a condition in the last loop that only breaks when a value for x is over say 300,000. If the value is below 300,000, the loop continues, once your value is where you need it, have x = x+300,001, then it should stop that loop.
Can create a flag that each loop checks, if you're using some increasing value for the loop (likely are) you can manually set each of these values to a value that breaks the condition, or use goto to jump out of the loop. People are gonna give me flak for this, but it's perfectly OK in this situation.

Personally, whenever I find myself in this situation I opt for a redesign. Chances are there is a better way to go about doing this.
OK, thanks for the help. And thanks residentbiscuit for the goto idea, hadn't thought of that.
The other thing to try - put the code into a function and return a value if it is the value you want, otherwise if it is an error of some sort, use a try catch block and throw an exception. The exception doesn't have to be a specific type - you can throw a general one if you want.

This can be a way you can avoid using a goto. There has been huge amounts of discussion in the past about whether goto's are needed in C++. As far as I can tell, they are not needed in C++ (use return or exceptions), and there a few situations where they may be used in C - along with a couple of rules (short distance and downward through the code)

Hopefully, we won't have huge amounts of discussion again :)

You can find info on how to do all this in the reference section on the top left of this page.

HTH
Topic archived. No new replies allowed.