'Break' and other ways out of loops - (nf)
Steven M. Haflich
smh at mit-eddie.UUCP
Mon Aug 29 04:45:10 AEST 1983
(Sorry to join the discussion so late, but have just caught up on weeks
of news backlog...) One problem with execution-time flags for breaking
out of multiple loop levels is execution-time overhead. Sometimes it
matters, and there is no reason for generated code to waste time
setting and checking a flag when the proper code action, i.e. the
underlying machine-language goto, is known at compilation time. Multi-
level breaks and continues should indeed be available in the compiler.
Some years ago I spent a few hours implementing "break n" and "continue
n" in a (the?) V7 PDP11 C compiler, with n a compile-time value
defaulting to 1. It was not particularly difficult, as the parser
maintains a stack of generated labels for each flow-of-control loop. I
realized soon after that the extension was awkward and dangerous. It
was simply too difficult to keep track how many levels one wants to
exit. The fact that break counts levels of "for", "while", "do", and
"switch", whereas continue should *not* count "switch", further
complicates to programmer's task. I concluded that either one should
never use a break/continue n with n greater than 2 or 3, or else named
loops should be used. Indeed, I could think of no advantage of "break
n" over "break name".
Unfortunately, the implementation is not quite as trivial, although it
should be simple enough.
As the proposed changes are completely upward compatible, I would
welcome them to C if only they would spread widely enough to be
portable. This I see as the real block to such needed enhancements.
No, I no longer have the break n compiler mods, so don't ask...
Steve Haflich
genrad!mit-ems!smh
More information about the Comp.lang.c
mailing list