I think the assignment expressions of PEP 572 are being implemented partly because of this. It’s even one of the examples: https://www.python.org/dev/peps/pep-0572/#id22 Answer from Deleted User on reddit.com
🌐
Reddit
reddit.com › r/python › why not add do-while loops to python?
r/Python on Reddit: Why not add do-while loops to Python?
August 30, 2019 -

It continues to puzzle me why we have no do-while loop in Python.

In Python code, it's common to see the following pattern:

# ... some code ....
while condition:
    # ... same code copied here ...

This is of course prone to problems because of the "minor" code duplication.

Alternatively, you might see this better option:

def some_code():
    # ... some code ...

some_code()
while condition:
    some_code()

This involves creating a function even though IMHO it often serves to bloat the code unnecessarily.

And than there's this variation:

while True:
    # ... do something ...
    if not condition:
        break

IMHO, this approach, especially when the body of the loop is fairly large, fails to communicate the logical intent of the code in a clean manner.

Of course - all of these approaches do work. But IMHO a syntax which could clarify programmer's intent in the most precise and concise way is the following classical do-while:

do:
    # ... do something ...
    while some_condition

I know that years ago do-while style constructs have been proposed in PEPs and rejected.

But isn't it time we review the idea again? I think there is a considerable amount of code which could benefit from this.

Would love to hear your thoughts :)

Top answer
1 of 2
86

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.

2 of 2
9

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.

Discussions

python - How to emulate a do-while loop? - Stack Overflow
I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work: list_of_ints = [ 1, 2, 3 ] iterator = list_of_ints.__iter__() element = None More on stackoverflow.com
🌐 stackoverflow.com
We can use do - while loop in python .True or false .
We can use do - while loop in python .True or false . More on askfilo.com
🌐 askfilo.com
1
December 29, 2024
How do while not loops work
In general, recall the while loop syntax. while : Conceptually, coding aside, consider filling a glass of water. Is there space for more water in the glass? Okay, then pour some. Without going in detail of how this funky glass object is defined, hopefully the following makes sense as a python analog of that process. while glass.has_space(): #we're still filling the glass glass.add_water() #glass is now full of water Without writing out how this glass object works, let's say it has another method for returning a boolean (True/False) for whether or not it is currently full, instead of the previous example of whether or not it had space for more water. We can fill the glass in the same way, but we have to negate that statement using the not operator while not glass.is_full(): #we're still filling the glass glass.add_water() #glass is now full of water Going back to the initial syntax, which is still the same, the only thing we've done is changing the condition statement of the loop. Instead of checking that glass.has_space() evaluates to True, we are now checking that the expression not glass.is_full() evaluates to True. That is the same statement as evaluating that glass.is_full() evaluates to False, because the only thing the not operator does to a boolean is negating it, i.e. True becomes False and vice versa. Now looking at your code linked, the condition for looping is that not sequenceCorrect evaluates to True, which is equivalent to the statement that sequenceCorrect is False. I won't paste the code here, but we see on line 3 that sequenceCorrect starts its life being False, so upon entering the while loop we do indeed step into that block of code because at that time not sequenceCorrect is in fact True. Then the first thing we do on line 5 is reassign it to True. If the remaining lines don't change it back to False, this will stop the while loop from repeating, since in this current state not sequenceCorrect evaluates to False. So you can say that line 5 is defaulting the loop to not going to be repeated. The only way for the loop to be repeated is for lines 6-8 with the nested for loop and if statement to find some character in dna that is not in "actgn", upon which sequenceCorrect will be assigned to False and thus the statement in the while loop not sequenceCorrect would evaluate to True and thus the loop would repeat itself once more. Personally, I think this is just a pretty unclear way to achieve the goal of checking the validity of that input string. I would have defined it another way, but I can't see anything wrong with it really. If the not operation in the while loop statement is bothering you, you could equally have rewritten the code with a variable for instance named sequenceIncorrect and just flipped all assignments i.e. True's become False's and vice versa. More on reddit.com
🌐 r/learnpython
16
5
September 11, 2024
Write True or False: 1. We can use do-while loop in Python. 2. The continue statement breaks the loops one by one. 3. 4. To come out of the infinite loop, we can either close the program window or p Ctrl + C. A sequence is a succession of values bound together by a single name. 5. The while statement is the looping statement.
Solution For Write True or False: 1. We can use do-while loop in Python. 2. The continue statement breaks the loops one by one. 3. 4. To come out of t More on askfilo.com
🌐 askfilo.com
1
May 8, 2024
🌐
freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - If the number the user submits is negative, the loop will keep on running. If it is positive, it will stop. Python does not have built-in functionality to explicitly create a do while loop like other languages.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-do-while
Python Do While Loops - GeeksforGeeks
July 23, 2025 - In Python, we can simulate the ... condition is met. Do while loop is a type of control looping statement that can run any statement until the condition statement becomes false specified in the loop....
🌐
Real Python
realpython.com › python-do-while
How Can You Emulate Do-While Loops in Python? – Real Python
August 2, 2022 - If the loop condition is false from the start, then the body won’t run at all. Note: In this tutorial, you’ll refer to the condition that controls a while or do-while loop as the loop condition.
🌐
Filo
askfilo.com › cbse › smart solutions › we can use do - while loop in python .true or false .
We can use do - while loop in python .True or false .... | Filo
December 29, 2024 - Therefore, the statement is false. Understand that a 'do-while' loop executes the loop body at least once before checking the condition. Recognize that Python does not have a native 'do-while' loop structure.
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
With the break statement we can stop the loop even if the while condition is true:
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how do while not loops work
r/learnpython on Reddit: How do while not loops work
September 11, 2024 -

I was reading through this code and I'm just not getting how while loops work when not operator is also used.

https://pastebin.com/5mfBhQSb

I thought the not operator just inversed whatever the value was but I just can't see this code working if that's the case.

For example , the while not sequenceCorrect should turn the original value of False to True, so the loop should run while it's True. But why not just state while True then? And why declare sequenceCorrect = True again? Doesn't it just means while True, make it True? And so on.

The only way it makes sense to me is of the while not loop always means False (just like the while always means as long as it's True) even if the value is supposed be False and should be inverted to True.

So, is that the case? Can anyone explain why it works like that?

Top answer
1 of 8
7
In general, recall the while loop syntax. while : Conceptually, coding aside, consider filling a glass of water. Is there space for more water in the glass? Okay, then pour some. Without going in detail of how this funky glass object is defined, hopefully the following makes sense as a python analog of that process. while glass.has_space(): #we're still filling the glass glass.add_water() #glass is now full of water Without writing out how this glass object works, let's say it has another method for returning a boolean (True/False) for whether or not it is currently full, instead of the previous example of whether or not it had space for more water. We can fill the glass in the same way, but we have to negate that statement using the not operator while not glass.is_full(): #we're still filling the glass glass.add_water() #glass is now full of water Going back to the initial syntax, which is still the same, the only thing we've done is changing the condition statement of the loop. Instead of checking that glass.has_space() evaluates to True, we are now checking that the expression not glass.is_full() evaluates to True. That is the same statement as evaluating that glass.is_full() evaluates to False, because the only thing the not operator does to a boolean is negating it, i.e. True becomes False and vice versa. Now looking at your code linked, the condition for looping is that not sequenceCorrect evaluates to True, which is equivalent to the statement that sequenceCorrect is False. I won't paste the code here, but we see on line 3 that sequenceCorrect starts its life being False, so upon entering the while loop we do indeed step into that block of code because at that time not sequenceCorrect is in fact True. Then the first thing we do on line 5 is reassign it to True. If the remaining lines don't change it back to False, this will stop the while loop from repeating, since in this current state not sequenceCorrect evaluates to False. So you can say that line 5 is defaulting the loop to not going to be repeated. The only way for the loop to be repeated is for lines 6-8 with the nested for loop and if statement to find some character in dna that is not in "actgn", upon which sequenceCorrect will be assigned to False and thus the statement in the while loop not sequenceCorrect would evaluate to True and thus the loop would repeat itself once more. Personally, I think this is just a pretty unclear way to achieve the goal of checking the validity of that input string. I would have defined it another way, but I can't see anything wrong with it really. If the not operation in the while loop statement is bothering you, you could equally have rewritten the code with a variable for instance named sequenceIncorrect and just flipped all assignments i.e. True's become False's and vice versa.
2 of 8
2
The difference here is that by using not sequenceCorrect you are avoiding the use of the break keyword. It's cleaner and makes the loop terminate itself, rather than relying on a specific keyword and force terminate it. You could use while True, but that way is not descriptive enough and can potentially create and endless loop if you don't take care of the edge cases. Think about it: while not sequenceCorrect is telling you, without a single comment line, that the loop should run while sequenceCorrect is False, vs while True which the only thing that's telling you is that this loop will run endlessly for unknown reasons.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › do while loop in python
Do While Loop in Python | Emulate Do While Loop in Python(Example)
March 17, 2023 - Thus, in python, we can use a while loop with if/break/continue statements that are indented, but if we use do-while, it does not fit the indentation rule. Therefore we cannot use the do-while loop in python.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
DataCamp
datacamp.com › tutorial › do-while-loop-python
Emulating a Do-While Loop in Python: A Comprehensive Guide for Beginners | DataCamp
January 31, 2024 - To emulate a "do-while" loop in Python, you can use a while loop with a True condition and strategically place a break statement.
🌐
Coursera
coursera.org › tutorials › python-while-loop
How to Write and Use Python While Loops | Coursera
This loop will print the numbers ... loops continuously execute code for as long as the given condition is true. There is no do while loop in Python, but you can modify a while loop to achieve the same functionality...
🌐
CodeRivers
coderivers.org › blog › python-do-while-loop
Python Do-While Loop: A Comprehensive Guide - CodeRivers
January 29, 2025 - In contrast, a while loop in Python is a pre-test loop, where the condition is checked first, and if it's false, the loop body is never executed. The general syntax of a do-while loop in languages that support it looks like this: ... The key idea is that the code within the do block will run ...
🌐
Filo
askfilo.com › cbse › smart solutions › write true or false: 1. we can use do-while loop in python. 2.
Write True or False: 1. We can use do-while loop in Python. 2. ... | Filo
May 8, 2024 - False - Python does not have a built-in do-while loop structure. The continue statement breaks the loops one by one. False - The continue statement does not break loops; it skips the current iteration and continues with the next iteration of ...
🌐
Scaler
scaler.com › home › topics › python do while loop
Python Do While Loop - Scaler Topics
December 1, 2023 - The difference between a "Do While" Loop and a regular "While" loop is that a "Do While" Loop executes the block of code at least once, regardless of whether the condition is true or false.
🌐
Educative
educative.io › answers › how-to-emulate-a-do-while-loop-in-python
How to emulate a do-while loop in Python
Even though Python doesn’t explicitly have the do-while loop, we can emulate it. There are two scenarios in which a loop terminates: The loop condition is no longer true/false (depending on the type of loop).
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - Python while loops are compound statements with a header and a code block that runs until a given condition becomes false. The basic syntax of a while loop is shown below: ... In this syntax, condition is an expression that the loop evaluates for its truth value. If the condition is true, then the loop body runs.
🌐
Python
wiki.python.org › moin › WhileLoop
While loops - Python Wiki
If the condition is initially false, the loop body will not be executed at all. As the for loop in Python is so powerful, while is rarely used, except in cases where a user's input is required*, for example: n = raw_input("Please enter 'hello':") while n.strip() != 'hello': n = raw_input("Please ...
🌐
YoungWonks
youngwonks.com › blog › do-while-loop-python
What is a do while loop in Python? How do I create a while loop in Python? What is the difference between a for loop and a while loop in Python?
February 18, 2024 - This is different from a Python ... how you can write a do-while loop in Python: The while True statement creates an infinite loop, which will run indefinitely unless we stop it....