Breaking out of several nested loops (& ANSI C)

Jim Gillogly jim at randvax.UUCP
Fri Oct 12 01:18:33 AEST 1984


--------------
Breaking out of a nested loop with a "break label" instead of a GOTO is
supposed to be much better for program verification.  Rather than being an
arbitrary control transfer, it merely exits from a well-defined
environment.  At least that's what Bill Wulf told us when he invented it
for BLISS.

The only time I use GOTO's in C is to get out of these loops, and I write
a whole lot of C.

I heartily concur with David Dyer-Bennett's assertion that "break label"
is much better than "break n", having used and tried to debug with both
in BLISS.  I would add an outer loop now and then and forget that there
was an "exitloop 3" on the next screen.

For stand-alone completeness, we're talking about leaving nested loops as
follows:

     label:
	for (i = 0; i < n; i++)
	{       ...
		for (j = 0; j < m; j++)
		{       ...
			if (error)
				break label;
		}
	}

Jim Gillogly            {vortex, decvax}!randvax!jim



More information about the Comp.lang.c mailing list