CBFalconer said:
So don't be a wuss. Use a goto. Works fine, is unambiguous.
Sure the goto works but I also come down on the side of the
(possible future) extension:
break N
The main problem with the goto is that once labels are introduced
the potential for typo's accidentally directing to the wrong label
is also introduced. For instance, if code like:
for(){
for(){
if(whatever)goto doneloop3;
}
}
doneloop3:
was replicated several times in one function and then modified
slightly for different uses it is relatively easy to leave
one of the exit goto's pointing to the wrong label. At least
the compiler can catch cases where the label is duplicated, but
it can't catch cases where the target is not as intended (but
the label exists).
The "break N" syntax is more robust under editing. For instance:
for(){
for(){
if(whatever)break 2;
}
}
can be replicated and no further editing is required - it is still
valid. Nedit (and probably other editors) has a "find matching {}"
command, this could be easily extended to "find break N }". Granted
the goto target can be found now with a regular text search.
The weakest part of the break N syntax that I see is that
if an outer loop is eliminated the break N might then
point to the wrong place. However if it then points to
a nonexistant loop terminator the compiler could catch it.
There is also the somewhat special case, which may not even be that
great an idea, but:
break outer;
which would break to the _outermost_ loop structure. It is in many
ways the complement of the existing break, which breaks to the
innermost control structure. In smallish functions break [inner]
and break outer should be sufficient for most needs.
Of course if "break" is extended "continue" should also be modified
to match.
Regards,
David Mathog
(e-mail address removed)