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?
Introduction to Do While Loop in Python. The Do While Loop conditional statement is used for an exit level control flow of code implementation that ensures the code block is executed at least once before the control reaches the while condition.
There is no do... while loop because there is no nice way to define one that fits in the statement: indented block pattern used by every other Python compound statement. As such proposals to add such syntax have never reached agreement. and have the exact same effect as a C do { .. }
Python doesn't have a do-while loop construct. Why? Apparently, the core developers never found a good syntax for this type of loop. Probably, that's the reason why Guido van Rossum rejected PEP 315, which was an attempt to add do-while loops to the language.
while loop: In Python, while loops are used to execute a block of statements repeatedly until a given condition is satisfied. Then, the expression is checked again and, if it is still true, the body is executed again. This continues until the expression becomes false.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With