Reputation: 19181
Is there a good reason why there isn't a do while flow control statement in python?
Why do people have to write while
and break
explicitly?
Upvotes: 16
Views: 2675
Reputation:
Python adds features only when they significantly simplify some code.
while True:
...
if not cond: break
is not less simple than a do-while loop, for which there is no obvious natural python syntax anyway.
do:
...
while cond
(Looks weird)
or this?
do:
...
while cond
(The while looks like a regular while statement)
Upvotes: 2
Reputation: 28934
It has been proposed in PEP 315 but hasn't been implemented because nobody has come up with a syntax that's clearer than the while True
with an inner if-break
.
Upvotes: 11
Reputation: 64414
Probably because Guido didn't think it was necessary. There are a bunch of different flow-control statements you could support, but most of them are variants of each other. Frankly, I've found the do-while statement to be one of the less useful ones.
Upvotes: 10