[net.lang.c] 'Break' and other ways out of loops

grunwald@uiuccsb.UUCP (08/27/83)

#R:ihuxq:-30200:uiuccsb:9000001:000:489
uiuccsb!grunwald    Aug 26 18:16:00 1983

   I've seen other languages which have a "label break", and it does make
it a lot easier. Using a "quisi-goto" is better, in my mind, then using a
goto. The limited form would tend to reduce the number of errors, I should
think.

The formats I've seen before (when properly Cified) are:

	/LABEL/ while ( Condition ) {
		    exit /LABEL/;
		}

		exit /LABEL/	-- this would cause an error -- no long in scope

					dirk grunwald
					university of illinois
					ihnp4 ! uiucdcs ! grunwald

smh@mit-eddie.UUCP (Steven M. Haflich) (08/28/83)

(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