[Python-Dev] Wishlist: dowhile

Nick Coghlan ncoghlan at gmail.com
Mon Jun 13 14:07:38 CEST 2005


Raymond Hettinger wrote:
> [BJörn Lindqvist] 
> 
>>I would like to have do-while's like this:
>>
>>do:
>>    <body>
>>    until <cond>
>>
>>But I'm sure that has problems too.
> 
> 
> That looks nice to me.

And this could easily be extended to allow code both before and after 
the 'until', giving a fully general loop:

   do:
       <body-1-or-more-times>
       until <cond>
       <body-0-or-more-times>
   else:
       <on-natural-loop-exit>

In fact, this would simply be giving "looks like executable 
pseudocode" syntactic sugar for the current 'do-until' workarounds:

   while 1:
     <body-1-or-more-times>
     if <cond>:
         <on-natural-loop-exit>
         break
     <body-0-or-more-times>

And:

   _exit = False:
   while not _exit:
       <body-1-or-more-times>
       _exit = <cond>
       <body-0-or-more-times>
   else:
       <on-natural-loop-exit>

The 'until' is less hidden than the 'yield' that turns a function into 
a generator, and its presence is obviously signalled by the preceding 
'do'. Its also less hidden than the 'if'/'break' construct in the 
infinite loop workaround, and about as hidden as the exit flag in the 
other style of workaround (although the 'until' can be more easily 
picked out by a syntax highlighter).

Cheers,
Nick.

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


More information about the Python-Dev mailing list