Breaking out of multiple loops
Michel Colman
michelcolman at mac.com
Wed Sep 5 05:40:35 PDT 2012
I have a very simple suggestion for breaking out of nested loops.
Currently, there are a few ways of breaking out of multiple
nested loops but they all have unnecessary drawbacks:
- exceptions (performance penalty, complexity)
- using boolean flags that are checked in every iteration
(performance hit)
- goto (ugly, generally frowned apon, although this is often
cited as one of the few cases where using goto is actually
acceptable, which says a lot about the lack of other options)
I propose an extremely simple solution that is probably very easy
to implement in compilers:
break break; // breaks out of two loops
break break break; // breaks out of three loops
break break continue; // jumps to the end of the third enclosing
loop
You might also introduce a shorthand version for breaking out of
a whole lot of loops:
break 5; // breaks out of 5 loops
And, why not,
break 5 continue; // jumps to the end of the sixth enclosing loop
What do you think?
More information about the Digitalmars-d
mailing list