[Python-Dev] PEP 315 - do while

Hans Polak Hans.Polak at capgemini.com
Tue Oct 3 16:14:12 CEST 2006

I'm against infinite loops -something religious :), which explains the call
for the do loop.

The issue about the parser is over my head, but the thought had occurred to
me. Now, it would not affect while loops inside do loops, wouldn't it?


-----Original Message-----
From: Nick Coghlan [mailto:ncoghlan at gmail.com] 
Sent: martes, 03 de octubre de 2006 15:51
To: Fuzzyman
Cc: Hans Polak; python-dev at python.org
Subject: Re: [Python-Dev] PEP 315 - do while

Fuzzyman wrote:
> Nick Coghlan wrote:
>> In my example, the 3 sections (<setup code>, <loop body> and <loop
>> code> are all optional. A basic do-while loop would look like this:
>>       do:
>>           <setup code>
>>           while <condition>
>> (That is, <setup code> is still repeated each time around the loop - it's

>> called that because it is run before the loop evaluated condition is
> +1
> This looks good.

I'm pretty sure it was proposed by someone else a long time ago - I was 
surprised to find it wasn't mentioned in PEP 315.

That said, Guido's observation on PEP 315 from earlier this year holds for
me too:

  "I kind of like it but it doesn't strike me as super important" [1]

> The current idiom works fine, but looks unnatural :
> while True:
>     if <condition>:
>        break

There's the rationale for the PEP in a whole 5 lines counting whitespace ;)

> Would a 'while' outside of a 'do' block (but without the colon) then be
> a syntax error ?
> 'do:' would just be syntactic sugar for 'while True:' I guess.

That's the slight issue I still have with the idea - you could end up with 
multiple ways of spelling some of the basic loop forms, such as these 3 
flavours of infinite loop:

       pass # Is there an implicit 'while True' at the end of the loop body?

       while True

   while True:

The other issue I have is that I'm not yet 100% certain it's implementable 
with Python's parser and grammar. I *think* changing the definition of the 
while statement from:

while_stmt ::=
               "while" expression ":" suite
                 ["else" ":" suite]

while_stmt ::=
               "while" expression [":" suite
                 ["else" ":" suite]]

And adding a new AST node and a new type of compiler frame block "DO_LOOP" 
would do the trick (the compilation of a while statement without a trailing 
colon would then check that it was in a DO_LOOP block and raise an error if



Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient,  you are not authorized to read, print, retain, copy, disseminate,  distribute, or use this message or any part thereof. If you receive this  message in error, please notify the sender immediately and delete all  copies of this message.

More information about the Python-Dev mailing list