Breaking out of several nested loops (& ANSI C)

Radford Neal radford at calgary.UUCP
Thu Oct 11 05:17:26 AEST 1984


I think labelling loops and allowing one to break out of several by
using that label is NOT a good idea for the ANSI standard. 

What does this really get you? Nothing as far as I can see. Breaking 
out of nested loops with a goto is not at all obscure. The only advantage
of a multi-level exit from a structured programming point of view is
that it guarantees that only forward branches are made, presumably
increasing readability. The proposal manages to avoid this advantage,
however, by using the same syntax for a label used as the object of a
goto as for a label used in a break. If I came across a label at the beginning
of a while loop, my first impression would be that someone RESTARTS the
loop by jumping there.

If you really want to do such things, the best construct is the "situation
case" proposed by C. T. Zahn. This works like this:

      notice-situation <situation1>, <situation2>, ...
         ...
         <code, including statements like "situation-occurs <situation>">
         ...
      case <situation1>: <code>
      case <situation2>: <code>
      ...
      end

(This isn't a proposal for a C-oriented syntax.) 

In the block of code at the beginning, statements can occur indicating
that a particular situation has occured (possibly in deeply nested 
contexts). After this block are a number of (optional) sections of code
that get jumped to when the corresponding situations arise. After that
section of code is done, the statement following all this gets done.

This gets you multi-level exits and more as well, in a potentially more
understandable form.

REALLY though, none of this is neccessary. I find that single level exits
are quite convenient 95% of the time, and gotos work fine for the rest.
I really thought this was all settled five years ago during the great
structured programming debate. Using a goto statement once every few
thousand lines of code is perfectly reasonable.

Furthermore, this is just the sort of thing which should NOT go in a
standard, as it would render all existing compiler non-standards-conforming
for no good reason, reducing the probability that the standard will
be taken seriously. Anybody who went out and wrote lots of code using the
new construct thinking it was portable because it was "standard" would
be fooling themselves.

      Radford Neal
      Dept. of Computer Science
      University of Calgary



More information about the Comp.lang.c mailing list