[Python-ideas] Updating PEP 315: do-while loops

Ron Adam rrr at ronadam.com
Mon Apr 27 07:09:35 CEST 2009



Arnaud Delobelle wrote:
> 
> On 26 Apr 2009, at 21:20, Larry Hastings wrote:
> 
>>
>> Anyway, this is one reason why I like the "do:" statement I proposed.  
>> (The other one--the one where I also proposed "continue if" and "break 
>> if".  But given your reaction to those let's ignore 'em for now.)  
>> This isn't *pure* sugar--it's a mildly novel flow control construct 
>> for structured programming.  Certainly Python doesn't have anything 
>> like it right now; "while True:" isn't an exact match (and "while 
>> False:" is equivalent to "if 0:").  My "do:" would allow spelling 
>> do/while as:
>>
>>   do:
>>       something()
>>       if condition: continue
>>
>> I think that's a reasonable do/while, and so economical: we only added 
>> one new keyword.  This form of "do:" has other uses too, as per my 
>> previous email.
>>
>> Not that I expect this syntax to take the world by storm.
> 
> It's funny because I had something like this in mind when I myself 
> suggested 'break if ...' and 'continue if ...' a little while ago 
> (although I would go for a 'loop:' that doesn't need to be continue'd 
> explicitely).  I had come to the point of view that the restrictions of 
> the looping construct had in fact somehow liberated it and made it more 
> versatile by making
> 
>    if condition: break
> 
> and
> 
>    if condition: continue
> 
> common control flow constructs inside a while loop, that in particular 
> encompass the do ... while / repeat ... until constructs, etc seen in 
> other languages (I may have expressed myself better in the original 
> thread).  My motivation for 'break/continue if ...' was that it would 
> make those constructs stand out more (and could be hilighted clearly as 
> such in editors) - I was a bit surprised when I was told it would make 
> code less readable and that it was a slippery slope, but then I have 
> lots of ideas, most of which don't turn out to be very good :)


I think it would help it to stand out.

     while 1:
        ...
        if a == 2: b = finished
        if b == 3: breakit()
        if c == 4: break
        if d == 5: baker = name
        if e == 6: steak = done

     while 1:
        ...
        if a == 2: b = finished
        if b == 3: breakit()
        break if c == 4
        if d == 5: baker = name
        if e == 6: steak = done

Ron



More information about the Python-ideas mailing list