Loop-and-a-half (Re: Curious assignment behaviour)

Ian Parker parker at gol.com
Fri Oct 12 18:57:30 EDT 2001


In article <Xns913851934E5D9michaelrcpcouk at 194.238.50.13>, Michael
Abbott <michael at rcp.co.uk> writes
>Donn Cave <donn at u.washington.edu> wrote in 
>news:9q4it9$tio$1 at nntp6.u.washington.edu:
>
>> I sort of regret wasting bandwidth on this purely academic question,
>> but it's a matter of truly venerable tradition that C's loop is a
>> bit shy of perfection.  The idea as I understand it, is that there
>> are essentially two control points in a loop, the entry, and the
>> conditional repeat.  C conflates those two in one, because both
>> entry and the conditional repeat have to be at the "top".
>
>Perhaps it's worth recalling at this point the syntax used by Algol68 for a 
>loop.  This was really quite pretty and possibly slightly relevant:
>
>       WHILE
>               statements;
>               expression
>       DO
>               statements
>       OD
>
>Of course, in C this needs to be translated to:
>
>       while(1)
>       {
>               statements;
>               if (!expression)  break;
>               statements;
>       }
>
>and in Python we have to do exactly the same:
>
>       while 1:
>               statements:
>               if not expression:  break
>               statements
>
><Shrug>  It's not pretty, but I'd rather worry about having a conditional 
>expression (off topic; we've already beaten that to death and not quite 
>managed a PEP out of it...)


In fact, IIRC, it was prettier and possibly more relevant:

        for i by j to k while t do x od

where i,j,m,t,x are expressions (which could be one or more statements),
and the phrases "for i", "by j", "to k", "while  t" are optional, with
reasonable defaults.

So you could briefer forms:

        for i to k do  statements  od

or 
        while t do  statements  od

or the infinite loop:

        do  statements  od


-- 
Ian Parker



More information about the Python-list mailing list