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

Steven D'Aprano steve at pearwood.info
Sun Apr 26 08:42:36 CEST 2009

On Sun, 26 Apr 2009 11:00:26 am Raymond Hettinger wrote:

> Am working on PEP 315 again, simplifying the proposal by focusing on
> a more standard do-while loop.
> The two motivating cases are:

A quick-and-dirty data point for you. Using Google code search, I found 
approx 80,000 hits for "lang:Pascal until" versus 129,000 hits 
for "lang:Pascal while". So the number of cases of test-at-top versus 
test-at-bottom loops are very roughly 3:2 in Pascal code indexed by 

(Disclaimer: both searches revealed a number of false positives. I've 
made no attempt to filter them out.)

For those who aren't familiar with Pascal, the test-at-top and 
test-at-bottom loops are spelled:

WHILE condition DO


UNTIL condition;

WHILE exits the loop when condition is False, and REPEAT...UNTIL exits 
when condition is True.

So I think the first question we need to deal with is, should we be 
discussing do...while or do...until? Is there a consensus on the sense 
of the condition test?

while True:  # do...while, exit when condition is false
    if not condition: break


while True:  # do...until, exit when condition is true
    if condition: break

Or do we doom this proposal by suggesting variant spellings that cover 
both cases?

> The challenge has been finding a syntax that fits well with the
> patterns in the rest of the language. It seems that every approach
> has it's own strengths and weaknesses.
> 1) One approach uses the standard do-while name but keeps usual
> python style formatting by putting the condition at the top instead
> of the bottom where it is in C and Java:

I'm not entirely comfortable with writing the test at the top of the 
loop, but having it executed at the bottom of the loop. But I'd prefer 
it to no do-loop at all.

Oh, I should ask... will whitespace around the ellipsis be optional?

> 2) Another approach is to put the test at the end.  Something like:
>     do:
>         j = _int(random() * n)
>     :while j in selected
>     do:
>         j = _int(random() * n)
>         while j in selected
> These seem syntactically weird to me and feel more like typos than
> real python code. I'm sure there are many ways to spell the last
> line, but in the two years since I first worked on the PEP, I haven't
> found any condition-at-the-end syntax that FeelsRight(tm).

I don't like either of those variants. I agree, they feel strange. The 
only variant that I find natural is the Pascal-like:

until condition

where the "until" is outdented to be level with the do. My vote goes for 
this, although I'm probably biased by my familiarity with Pascal. How 
does that feel to you?

I don't think I could live with

while condition

even though it isn't strictly ambiguous, nor does it prevent nesting a 
while-loop inside the do-loop:

    while flag:
while condition

But it looks disturbingly like an invalid line.

If outdenting is impossible, then I could live with it being indented:

    until condition  # or while if you prefer

but what happens if there is indented code following the until/while? 
Syntax error, or does it just never get executed?

My preferences, in order of most-liked to least-liked, with my score:

(1) +1
until condition # exit loop when condition is true

(2) +0.5
do...until condition: # exit loop when condition is true

(3) +0.5
do...while condition: # exit loop when condition is false

(4) +0
    until/while condition
    mystery_suite  # what happens here?

(5) -0
The status quo, no do-loop at all.

(6) -0.5
:while/until condition

(7) -1
while condition

Steven D'Aprano

More information about the Python-ideas mailing list