Tim Rentsch said:
Certainly not.
This analogy doesn't hold up. It happens that functions have
names, but we don't give a function name in a 'return' statement,
and we don't have to look for a name (any name) to see which
block is exited upon seeing a 'return'.
Hypothetically, if the proposed named break feature were extended
to cover returns from nested functions (which, yes, don't exist in
standard C), then it would make sense to allow a return statement
to return from a named function rather than from the nearest
enclosing one. The syntax might be something like
return value from name;
or perhaps
return value _From name;
where "name" is a function name, defaulting to the innermost enclosing
function if it's not specified.
I'm not advocating such a feature, merely stating that it could be
defined consistently.
Similarly, in a code
fragment such as
XYZZY:
while(condition){
...
if(something) break;
...
}
the 'break' is exiting a named statement, but the presence of a
name, or what name is used, is completely incidental to how break
works, and what we have to do to understand it in the context of
the function body. So it is also with return.
In C as it's currently defined, yes, the break always refers to the
innermost enclosing loop or switch statement. What we're discussing
is a hypothetical change (or extension) to the language, in which
a loop or switch statement can be named and a break statement
may optionally refer to an enclosing loop or switch statement by
its name.
There's precedent for this in Ada, for example:
Outer: loop
Inner: loop
-- ...
end loop Inner;
end loop Outer;
Inside the inner loop, you can write any of:
exit; -- Ada's spelling of "break"
exit Inner; -- exits the loop named "Inner"
exit Outer; -- exits the loop named "Outer"
The names "Inner" and "Outer" refer to the entire respective loop
constructs, not to a point in the code at the top of a loop.
(There's a different syntax for labels used as the targets of
goto statements.)
If such a feature were to be added to C, it could either re-use
the existing syntax for goto labels (Perl does this), or invent
a new syntax used only to name loops and switch statements (and
perhaps blocks).
I'm certainly aware that loops don't have names that are relevant
to break statements *in C as it's currently defined*.