50

Why doesn't Python have a 'do while' loop like many other programming language, such as C?

Example : In the C we have do while loop as below :

do {
   statement(s);
} while( condition );
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Bahubali Patil
  • 1,127
  • 1
  • 16
  • 23
  • 6
    Status *Rejected*: https://www.python.org/dev/peps/pep-0315/ – Martijn Pieters May 17 '16 at 16:20
  • 3
    Possible duplicate of [Emulate a do-while loop in Python?](http://stackoverflow.com/questions/743164/emulate-a-do-while-loop-in-python) – miradulo May 17 '16 at 16:20
  • 1
    Re the hold notice: The link provided by Martijn doesn't look very opinion-based: "Subsequent efforts to revive the PEP in April 2009 did not meet with success because no syntax emerged that could compete..." – JS. May 17 '16 at 16:24
  • 2
    @DonkeyKong: how to work around it does not answer the question as to why there is no such syntax though. – Martijn Pieters May 17 '16 at 16:32
  • @MartijnPieters Meh, yeah, I suppose. I just feel like the answers in that question provide the same reason as the rejected PEP - no syntax emerged that could compete with the `while True` form. You have a point though - they don't mention the first paragraph of your answer. – miradulo May 17 '16 at 16:36

2 Answers2

84

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.

Nor is there really any need to have such a construct, not when you can just do:

while True:
    # statement(s)
    if not condition:
        break

and have the exact same effect as a C do { .. } while condition loop.

See PEP 315 -- Enhanced While Loop:

Rejected [...] because no syntax emerged that could compete with the following form:

    while True:
        <setup code>
        if not <condition>:
            break
        <loop body>

A syntax alternative to the one proposed in the PEP was found for a basic do-while loop but it gained little support because the condition was at the top:

    do ... while <cond>:
        <loop body>

or, as Guido van Rossum put it:

Please reject the PEP. More variations along these lines won't make the language more elegant or easier to learn. They'd just save a few hasty folks some typing while making others who have to read/maintain their code wonder what it means.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 3
    "Nor is there really any need to have such a construct, not when you can just do:" If you do that, and you then call "continue" somewhere in the while loop before the condition check, then it will have an effect very very different from doing the same thing in other languages that have a do-while loop, since the continue statement would mean that you never check the condition. A real do-while loop would allow you to call continue and then have the loop end if the condition is false.. I love python, but the two things I really miss are do-while and real for i=0; i++, i – Vinzent Jan 18 '21 at 19:55
  • 2
    @Vinzent then use a `try:`...`finally:` construct and put the condition in the `finally` block. Any `continue` statement in the `try` block will still trigger the ` finally ` block and so the condition is still checked. I created a [quick demo](https://repl.it/@mjpieters/dowhilecontinue). – Martijn Pieters Jan 18 '21 at 20:38
  • @Vinzent that said: I have never, ever needed to do that. In general, I usually encapsulate more complex loop requirements in iterators instead. – Martijn Pieters Jan 18 '21 at 20:40
  • 1
    Didn't think of the "try-finally" structure you proposed, that's one way to do it.. there are of course multiple ways that it can be accomplished, one of them being iterators, which is also occasionally what i end up resorting to. but to me it just feels really chunky and awkward, in most simple situations. It is not that there are things that you cannot do in python because it lacks the do-while structure, it is just that the do ---> if condition: continue ---> while condition: .. structure makes a lot of code much more compact, neat and readable. in my opinion. – Vinzent Jan 18 '21 at 20:59
  • @Vinzent no one made that argument; “doesn’t make the language easier to learn” is not the same as “makes the language harder to learn”. Also, there is wider context that Guido made that comment in, addressing arguments made in the discussions. Discussions that stretch back at least 4 years, and never went anywhere. – Martijn Pieters Jan 18 '21 at 21:27
8

Because everyone is looking at it wrong. You don't want DO ... WHILE you want DO ... UNTIL.

If the intitial condition is true, a WHILE loop is probably what you want. The alternative is not a REPEAT ... WHILE loop, it's a REPEAT ... UNTIL loop. The initial condition starts out false, and then the loop repeats until it's true.

The obvious syntax would be

repeat until (false condition):
  code
  code

But for some reason this flies over everyone's heads.

Caleb Fuller
  • 209
  • 2
  • 5