Showing results for do while in python
Search instead for do while en python

Python does not have a built-in do-while loop, but you can emulate its behavior using a while True loop combined with a break statement. This structure ensures the code block executes at least once before checking the condition, mimicking the post-test nature of a do-while loop found in languages like C or Java.

To implement this, initialize an infinite loop and place the condition check inside the body:

while True:
    # Code block executes first
    user_input = input("Enter a number: ")
    if user_input.isdigit():
        break  # Exit if condition is met

Key differences from standard while loops:

  • Execution Order: A do-while loop checks the condition after execution, guaranteeing the body runs once, whereas a standard while loop checks before and might never run if the condition is initially false.

  • Control Flow: The emulation relies on break to exit the while True loop when the desired condition is met, effectively creating an "exit-controlled" loop.

  • Use Cases: This pattern is particularly useful for menu-driven programs or input validation where the system must present options or request data at least once before verifying validity.

I am not sure what you are trying to do. You can implement a do-while loop like this:

while True:
  stuff()
  if fail_condition:
    break

Or:

stuff()
while not fail_condition:
  stuff()

What are you doing trying to use a do while loop to print the stuff in the list? Why not just use:

for i in l:
  print i
print "done"

Update:

So do you have a list of lines? And you want to keep iterating through it? How about:

for s in l: 
  while True: 
    stuff() 
    # use a "break" instead of s = i.next()

Does that seem like something close to what you would want? With your code example, it would be:

for s in some_list:
  while True:
    if state is STATE_CODE:
      if "//" in s:
        tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
        state = STATE_COMMENT
      else :
        tokens.add( TOKEN_CODE, s )
    if state is STATE_COMMENT:
      if "//" in s:
        tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
        break # get next s
      else:
        state = STATE_CODE
        # re-evaluate same line
        # continues automatically
Answer from Tom on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-do-while
Python Do While Loops - GeeksforGeeks
July 23, 2025 - In Python, we can simulate the behavior of a do-while loop using a while loop with a condition that is initially True and then break out of the loop when the desired condition is met.
Discussions

As a beginner how do I understand while loops?
It’s just a loop which continues running until the condition becomes false. More on reddit.com
🌐 r/learnpython
76
36
April 10, 2025
Need help understanding this while loop
I’m new to while loops, and I don’t quite get what’s going on in this code from my book: current_number = 1 while current_number More on discuss.python.org
🌐 discuss.python.org
19
0
February 13, 2024
Do while loop in python
I am trying to implement the queue data structure in Python. I want to make the program function according to user-input. Like, the user should be able to decide if they want to enqueue or dequeue elements, or display elements in the queue. I was wondering if there is an alternative to the do-while ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
January 26, 2021
Why not add do-while loops to Python?
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 More on reddit.com
🌐 r/Python
22
3
August 30, 2019
🌐
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.
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
🌐
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Examples)
In Python, we use the while loop to repeat a block of code until a certain condition is met.
Find elsewhere
🌐
Texas Instruments
education.ti.com › en › resources › computer-science-foundations › do-while-loops
Python coding: Do While Loops | Texas Instruments
Explore Do While loops in Python on the TI-Nspire™ CX II graphing calculator. Do While loops are a form of post-test loop that always run at least one time.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-construct-while-loops-in-python-3
How To Construct While Loops in Python 3 | DigitalOcean
August 20, 2021 - If you’re unfamiliar with this package, you can learn more about generating random numbers from the Python docs. ... Next, we’ll assign a random integer to the variable number, and keep it in the range of 1 through 25 (inclusive), in the hope that it does not make the game too difficult. ... At this point, we can get into our while loop, first initializing a variable and then creating the loop.
🌐
Coursera
coursera.org › tutorials › python-while-loop
How to Write and Use Python While Loops | Coursera
A do while loop is a variant of the while loop that checks the condition at the end of the loop rather than the beginning. Even if the condition is never true, the statements inside the loop body will be executed at least once. do while loops ...
🌐
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 - A do-while loop is a loop that executes a block of code at least once, then checks a boolean condition which results in a true or false to decide whether to repeat the block of code or not.
🌐
Study.com
study.com › computer science courses › computer science 113: programming in python
While Loops in Python | Definition, Syntax & Examples - Lesson | Study.com
July 24, 2024 - It is ideal for situations where ... in Python is a reserved word which creates a while loop using the syntax: while condition: do_stuff....
🌐
Quora
quora.com › How-do-I-use-for-and-while-loops-in-Python
How do I use for and while loops in Python?
Because Python supports only one form of “conditional loop” — with the while at the top (tested before the first execution of its indented suite of statements) — because there is no “do … until” or “do … while” (bottom tested) loop, it’s common to see code using the pattern: while True: …
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
For a more detailed explanation and additional examples, you can look into PEP 636 which is written in a tutorial format. We can create a function that writes the Fibonacci series to an arbitrary boundary: >>> def fib(n): # write Fibonacci series less than n ... """Print a Fibonacci series less than n.""" ... a, b = 0, 1 ... while a < n: ...
🌐
Python documentation
docs.python.org › 3 › tutorial › introduction.html
3. An Informal Introduction to Python — Python 3.14.3 documentation
>>> a, b = 0, 1 >>> while a < 1000: ... print(a, end=',') ... a, b = b, a+b ... 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987, ... Since ** has higher precedence than -, -3**2 will be interpreted as -(3**2) and thus result in -9. To avoid this and get 9, you can use (-3)**2. ... Unlike other languages, special characters such as \n have the same meaning with both single ('...') and double ("...") quotes.
🌐
Sololearn
sololearn.com › en › Discuss › 2754843 › does-python-have-dowhile-loop
Does python have do-while loop? | Sololearn: Learn to code for FREE!
April 14, 2021 - Python doesn't have a built-in `do-while` loop like some other programming languages, such as C or Java. However, you can achieve similar functionality using a `while` loop in combination with a conditional check at the end.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 39804 › do-while-loop-in-python
do-while loop in python [SOLVED] | DaniWeb
Editors note: For Python code on ... · vegaseat 1,735 DaniWeb's Hypocrite Team Colleague 20 Years Ago · No, there is no "do ... while" loop in Python....
🌐
Scaler
scaler.com › home › topics › python do while loop
Python Do While Loop - Scaler Topics
December 1, 2023 - Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied, and when the condition becomes false, the line immediately after the Loop in the program is executed.
🌐
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 :)