Breaking out of several nested loops (& ANSI C)

Cesar Quiroz quiroz at rochester.UUCP
Fri Oct 12 09:52:55 AEST 1984


Minimal editing done in quotes below!_ 
<From a posting by  Charlie Martin  (...mcnc!duke!crm)>

> I may be dense, but I simply cannot see what makes
> 	...
> 		break label ;
> 
> any more  structured'' than
> 	...
> 		goto end ;
> 	...
> end :	;
> 

I, for one, will not say that one of the constructs is more
''structured'' that the other. Nor I couldn't care less. For
the 'structured'/'unstructured' debate usually moves real fast
into religious beliefs.

What makes me decide for the 'break' style over the 'goto' style
is a set of other considerations.  To start with,  you may agree
(so I hope) that the few goto's that are really harmless (or 
unavoidable) are the ones that take you out from a loop/procedure
or some other flow of control commitment into safe code again. Like
the one above, which is probably less dangerous than adding flags
to check how to get out. (My typing errors increase exponentially 
with the number of declarations).

However, if those are really the only goto's we really want, why bother
implementing the whole power of an all purpose 'goto'? To take your own
words on this matter:

> In addition, the construct above has the advantage that it
> looks like it is doing what it is really doing -- that is, the
> break label'' version says that it is breaking a loop labellled
> label'', which label is at the beginning, but it is effectively
> GOING to the END.
> 

Yes, 'break' (or 'exit' or whatever reason may suggest) says what
it is doing. And it can do only what it says, no more no less. In
addition, 'break' is NOT going to the end. It's going beyond :-)

> Don Knuth wrote an article that was pretty good on Structured
> Programming With GOTO's that covered all off this sort of
> thing, by the way -- I can dig up a formal reference if needed.
> 

Amen brother. Every side seems to have a Holy Book. (And yes, the 
article is really good -- my copy appears in Raymond Yeh's collection
"Current Trends in Programming Methodology", Vol.1, Prentice Hall (1977),
but there is a footnote that says that the copyright is by ACM (1974)). But
again, best arguments don't use religion.


Cesar Augusto  Quiroz Gonzalez {allegra|seismo}!rochester!quiroz



More information about the Comp.lang.c mailing list