[Python-Dev] PEP 351 - do while
Ron Adam
rrr at ronadam.com
Sun Oct 1 12:20:07 CEST 2006
Nick Coghlan wrote:
> 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 don't think this has been suggested yet.)
while <enter_condition>, <exit_condition>:
<body>
This would be a do-loop.
while 1, <exit_condition>:
<body>
In situations where you want to enter a loop on one condition and exit on a
second condition:
if value1:
value2 = True
while value2:
<body>
Would be ...
while value1, value2:
<body>
I've used that pattern on more than a few occasions.
A single condition while would be the same as...
while <condition>, <condition>: # same entry and exit condition
<body>
So do just as we do now...
while <condition>: # same entry and exit condition
<body>
> 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).
Putting both the entry and exit conditions at the top is easier to read.
The end of the first loop is also the beginning of all the following loops, so
having the exit_condition at the top doesn't really put anything out of order.
If the exit_condition is not evaluated until the top of the second loop, the
names it uses do not need to be pre defined, they can just be assigned in the loop.
Ron
More information about the Python-Dev
mailing list