Breaking out of several nested loops (& ANSI C)

Roger Hayes rogerh at arizona.UUCP
Thu Oct 11 05:04:46 AEST 1984


Tony Hansens' proposal was to allow labels on loops, like
	foo: while (x)
	{
		...
		.{.
			break foo;
		.}.
	}

I like the idea of named loops.  This syntax is very bad (sorry, Mr. 
Hansen).  Is foo a legal label?  Can one say "goto foo"?  Is this fragment
legal?  (If so, what does it mean?)

baz: 	i += j;
	break baz;

I am not pleased with any of the alternatives I have thought of for syntax.
The problem is to give a name to the loop body, without creating an ambiguity
with any other legal construct, and hopefully by using something that will
seem familiar to present C users.  It would be nice to avoid making another 
keyword.

I have thought of, and rejected:
	while (x) foo: { ... } foo	
	while (x) loop foo { ... } foo
	while (x) foo:: { ... } foo

I like the first best, but it does create two kinds of labels (on regular 
statements and on compound statements) with the same syntax, but different
semantics.  I put trailing labels (which must match the name of the loop)
on all of them, because I want a marker to see where the break will jump
to, without counting levels of braces.

	Roger Hayes
	University of Arizona
	Dept. of Computer Science

PS: I am strongly in favor of more-than-6-character-names in the C standard.
In addition to all the reasons advanced so far, the C language is tied pretty
closly to UN*X, and UN*X is headed towards flexnames.  Let the inconvenience 
be on the past (6-chars) not on the future.



More information about the Comp.lang.c mailing list