Is there a reason why the do-while (repeat-until) loop is not native implemented in py? | SoloLearn: Learn to code for FREE!

+9

Is there a reason why the do-while (repeat-until) loop is not native implemented in py?

Which is the best way to fill in for it?

5/13/2018 8:59:34 PM

Dario

4 Answers

New Answer

+18

Python has not a Do-While-Loop. 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. I was confused a little bit, because I had in mind that there would be something like this, but it emerged that this was in JavaScript. If you want to do a do-while-loop in Python, you have to make the following workaround: while True: do_something() if condition(): break https://stackoverflow.com/questions/37281553/why-there-is-no-do-while-loop-in-python

+13

I usually initialize the loop as infinite and break if the condition is not met. Serves the same after the first cycle.

+5

Thanks to all of you for the Giga-fast answer.

+5

@ Dario You are welcome! :-)