From: martelli@cadlab.sublink.org (Alex Martelli)
Newsgroups: comp.arch,comp.lang.misc
Subject: Re: A challenge to the anti-goto
Date: 4 Nov 92 09:39:42 GMT
Organization: CAD.LAB S.p.A., Bologna, Italia

jhauser@pine.CS.Berkeley.EDU (John Hauser) writes:
	...[I'm just excerpting bits from the code:]...
:start:
:    g = ...;
:    switch ( g ) {
:        case 1:
:            n += 1;
:            goto start;
:        case 3:
	...
:        case 12:
:            goto start;
	...
:            if ( g != 0 ) goto start;
	...
:                if ( g & 3 != 2 ) goto start;
:                j = 7;
:                while ( ! ..bit.. ) {
:                    j += 1;
:                    g = ...;
:                    if ( ( 2<<( g - 1 )/j ) & 1 == 0 ) goto start;
:                }

:	When coded in C, Herman's algorithm cannot be efficiently
:	implemented without at least one label (`start'), and
:	corresponding `goto's.
:(I look forward to seeing this claim refuted!)

It seems to me that exactly ONE of the 5 "goto start" constructs may
give problems:  the last one, since it's embedded within another loop.
All others could be replaced easily by changing the "start:"  label into
a "for(;;) {", add a corresponding "break;}" at the end of the loop, and
changing all "goto start;" into "continue;"; so at least 80% of the
"corresponding `goto's" are removed (whether it is at all "beneficial"
to do so is another question, of course!).

Break-from-loop and iterate-loop from within another nested loop (and,
due to the 'overloaded' nature of the break keyword, break-from-loop
from within a switch!) are indeed a functional limitation of C, i.e.,
no can do without goto or some flag-variable.

:	If we've known about this problem for twenty years, why don't we
:	have more powerful `break' instructions in our existing languages??
:
:Beats me.

But we DO.  The hugely popular perl language allows one to label a loop
and use the label as an operand of the "next" (same as C "continue")
and "last" (same as C "break") keywords.  I seem to recall that PL/I
used a similar idea, and perhaps REXX, another very popular language.
Just to clarify, the above could be something like:

mainloop: for(;;) {
    ...
    next mainloop if ...some condition...;
    ...
    if(...other condition...) {
	next mainloop;
    }
    ...
    innerloop: while( ...whatever... ) {
	...
	next mainloop if ...yet further condition...;
    }
    ...
    last mainloop;
}

(perl has a further refinement, in that a loop may have a generalized
 "continue-block", but I don't think it's needed for anything except
 fully express the semantics of for(;;) in terms of while() - somebody
 is sure to correct me if I'm wrong!-).

I don't really see that anything beyond labeled loops is warranted in
the way of "more powerful `break' instructions".  By the way, isn't
this discussion by now totally out of charter for comp.arch and more
appropriate for comp.lang.{perl,misc}?  We're talking about little
more than syntactic sugar, after all, surely not deep architectural
questions!
-- 
Email: martelli@cadlab.sublink.org                   Phone: ++39 (51) 6130360
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: ++39 (51) 6130294