Breaking out of several nested loops (& ANSI C)

Cesar Quiroz quiroz at rochester.UUCP
Fri Oct 12 02:05:02 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 think the proposed syntax is not that bad.  First of all, it doesn't 
introduce a new usage in C, labels are already valid where proposed.
Now, the idea requires a simple extension to the semantics of the language,
namely: That 'break <label>' works only for breakable statements! This,
of course, means two things: 

1._ That <label> refers to a 'breakable' statement,
2._ That the break occurs inside the range of that statement.

You can add a field to the 'label' entries in your symbol table (say
'breakable').  You set this flag appropriately as soon as you recognize what
type of statement your label is adorning. (Minor difficulties can arise
if you allow <label> [<label> ..] <statement>, because you have to fix not
just one label, but this shouldn't be a concern here). Then, if you see

	break foo;     (as per the example)

you can determine that the usage is valid. On the other hand, 'break baz;' 
is nonsense, can be easily shown to be that, and can be handled properly
(it 'breaks' your whole program :-)). It  should go without saying, but
the compiler shouldn't scream "syntax error", because 'break <label>' would
be a syntactically valid construction, but an error message like
"cannot break from ..." or "unbreakable ..." (but not "stainless ..." :-))
Additional remarks concerning the competence of the perpetrator are usually
considered unnecessary or in bad taste.

There is also the (unrelated?) proposal of adding the same label at the
end of a compound statement.  Sound as it is, retrofitting it into a 
language seems too painful in terms of the expected benefits.


Cesar Augusto  Quiroz Gonzalez

Department of Computer Science     {allegra|seismo}!rochester!quiroz
University of Rochester            or
Rochester,  NY 14627               quiroz at ROCHESTER



More information about the Comp.lang.c mailing list