[Python-Dev] PEP 351 - do while

Nick Coghlan ncoghlan at gmail.com
Sun Oct 1 06:18:11 CEST 2006


Hans Polak wrote:
> Hi,
> 
>  
> 
> Just an opinion, but many uses of the ‘while true loop’ are instances of 
> a ‘do loop’. I appreciate the language layout question, so I’ll give you 
> an alternative:
> 
>  
> 
> do:
> 
>             <body>
> 
>             <setup code>
> 
>             while <condition>
> 

I believe you meant to write PEP 315 in the subject line :)

To fully account for loop else clauses, this suggestion would probably need to 
be modified to look something like this:

Basic while loop:

     <setup code>
     while <condition>:
         <loop body>
         <setup code>
     else:
         <loop completion code>


Using break to avoid code duplication:

     while True:
         <setup code>
         if not <condition>:
             <loop completion code>
             break
         <loop body>

Current version of PEP 315:

     do:
         <setup code>
     while <condition>:
         <loop body>
     else:
         <loop completion code>

This suggestion:

     do:
         <setup code>
         while <condition>
         <loop body>
     else:
         <loop completion code>

I personally like that style, and if the compiler can dig through a function 
looking for yield statements to identify generators, it should be able to dig 
through a do-loop looking for the termination condition.

As I recall, the main objection to this style was that it could hide the loop 
termination condition, but that isn't actually mentioned in the PEP (and in 
the typical do-while case, the loop condition will still be clearly visible at 
the end of the loop body).

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list