[net.lang.c] Intelligibility of multibreak loops

ggs@ulysses.UUCP (08/25/83)

I think 'chongo's example of how one can survive without
the multi-level break is a demonstration of the need for
it.  Consider a slightly more complicated problem: you
want to get the next line if the line is bad, you want to
abandon the whole thing if the file is bad.  If you use
'break label' and 'continue label', you get the following:


	/* read lines of text ... */

readloop:
	for (; some-code; some-code) {
		...

		/* character in line processing */

		while (some-code) {
			...

			/* case in char in a line */

			switch (some-code) {
			...
			case BADLINE:
				...
				continue readloop;	/* get the next line */
				break;
			...
			case BADFILE:
				...
				break readloop;		/* stop reading */
			...
			}
		}
	}

I don't even want to think about how to write that using
convoluted flag testing, much less read it.
-- 

Griff Smith	Bell Labs, Murray Hill
Phone:		(201) 582-7736
Internet:	ggs@ulysses.uucp
UUCP:		ulysses!ggs

mark@cbosgd.UUCP (08/26/83)

Among programming languages people, multi level breaks and continues
are generally considered a "good thing".  When coupled with a decent
for loop (such as the one in C) and a return statement, the almost
make goto's completely obsolete.  There are two remaining situations
I run across every blue moon that are left:

(1) Error handling.  There are lots of hairy and ugly schemes to deal
with sudden errors.  Software events, such as PL/1 on-units and Ada
exceptions, are one very complex approach.  Non local goto's ala Pascal
are another.  For my money, the setjmp/longjmp mechanism provided by
the C library (not the language!) is prefectly adequate for this one,
although one would like better diagnostics when you mess up.

(2) Reduction to another case.  This is a familiar concept in Mathematics,
where you handle one case by doing some simplification and then saying
"it reduces to this other case".  The same thing is useful in programming.
Within a switch, you want to say, in effect, "go to the label marking the
code to handle the following switch value".  (Note this is a generalization
of the "goto default" situation mentioned earlier.)