[Python-Dev] PEP 351 - do while
rrr at ronadam.com
Sun Oct 1 12:20:07 CEST 2006
Nick Coghlan wrote:
> Hans Polak wrote:
>> 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:
>> <setup code>
>> while <condition>
(I don't think this has been suggested yet.)
while <enter_condition>, <exit_condition>:
This would be a do-loop.
while 1, <exit_condition>:
In situations where you want to enter a loop on one condition and exit on a
value2 = True
Would be ...
while value1, value2:
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
So do just as we do now...
while <condition>: # same entry and exit 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).
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.
More information about the Python-Dev