[Python-ideas] Immemorial desire for "do-while"-like construction

Steven D'Aprano steve at pearwood.info
Thu Jul 16 01:59:39 CEST 2009


On Thu, 16 Jul 2009 08:50:06 am Jan Kaliszewski wrote:
> Hello,
>
> The issue has been coming back repeatedly:

And not very long ago at that.

Please have a read of this thread:

http://mail.python.org/pipermail/python-ideas/2009-April/004306.html

and (if I may be so immodest to link to my own post) especially this one 
where I discuss various possible ways of spelling the loop:

http://mail.python.org/pipermail/python-ideas/2009-April/004326.html


> Maybe here the simplest approach can lead us to the solution? I.e.:
>
> repeat while CONDITION:
>        SOME
>        ACTIONS
>        HERE
>        ...

(1) I dislike that it puts the test at the top of the loop even though 
it is not tested until the bottom.

(2) I dislike that it has two keywords back-to-back.

(3) I prefer to swap the sense of the test. Rather than end the loop 
when CONDITION becomes false, I'd prefer the Pascal semantics of ending 
the loop when CONDITION becomes true. Using your proposed syntax, that 
would become:

repeat until CONDITION:
    block

Now we can discard the redundant double keyword and just write:

until CONDITION:
    block

which is still funny because the test is at the top of the block 
although it's not tested until the bottom.


> In human language it'd mean: do ACTIONS once unconditionally, then
> *repeat* them while the CONDITION is true.
>
> Additional advantage is that probably for many people associate
> 'repeat' with such construction (from experience with other
> languages).

Other people associate 'do' with such a construction.


I suspect we are doomed to keep re-visiting this question until 
Python5K, because (1) there are so many ways of spelling a do...until 
loop and the differences are (almost entirely) just aesthetic; and (2) 
there is no overwhelming advantage to do...until compared to a while 
loop with an explicit break.



-- 
Steven D'Aprano



More information about the Python-ideas mailing list