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.
🌐
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.
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
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
0
0
February 13, 2024
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
🌐
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: ... Note: The else block will NOT be executed if the loop is stopped by a break statement. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
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.
🌐
DataCamp
datacamp.com › tutorial › do-while-loop-python
Emulating a Do-While Loop in Python: A Comprehensive Guide for Beginners | DataCamp
January 31, 2024 - Python does not have a built-in "do-while" loop, but you can emulate its behavior.
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - However, translating the for loop ... ... A do-while loop is a control flow statement that executes its code block at least once, regardless of whether the loop condition is true or false....
Find elsewhere
🌐
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.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 39804 › do-while-loop-in-python
do-while loop in python [SOLVED] | DaniWeb
Python does not have a built-in do...while, but you can emulate "run at least once, then decide" cleanly. Use a boolean updated at the end of each iteration (avoid strings like "true"/"false", and prefer logical and over bitwise &): finished ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
Code given below uses a 'while' loop with the condition "True", which means that the loop will run infinitely until we break out of it using "break" keyword or some other logic. ... Note: It is suggested not to use this type of loop as it is a never-ending infinite loop where the condition is always true and we have to forcefully terminate the compiler. Python programming language allows to use one loop inside another loop which is called nested loop.
Published   1 month ago
🌐
Real Python
realpython.com › python-do-while
How Can You Emulate Do-While Loops in Python? – Real Python
August 2, 2022 - In this tutorial, you'll learn how to emulate do-while loops in Python. The most common technique to do this is to create an infinite while loop with a conditional statement that controls the loop and jumps out of it using a break statement.
🌐
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.
🌐
DataCamp
datacamp.com › tutorial › python-while-loop
Python While Loops Tutorial | DataCamp
June 25, 2020 - In the below example, you initially start with an error equal to 50.0. Next, you will write a while loop, in the condition part, we write error > 1, so that the while loop executes again as long as the error is above 1. Inside the while loop, you divide the error by four and update the error variable. When we run it the first time, we receive 12.5. Python will then go back to the condition of the while loop with the new error equal to 12.5.
🌐
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 - 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.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Python.org
discuss.python.org › python help
Need help understanding this while loop - Python Help - Discussions on Python.org
February 13, 2024 - 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 <= 5: print(current_number) current_number += 1 After just watching a video on Yo…
🌐
Server Academy
serveracademy.com › blog › python-while-loop-tutorial
Python While Loop Tutorial - Server Academy
Here’s a recap of key points: Basic Usage: Use a while loop to repeat a block of code as long as a condition is true. break: Use break to exit a while loop before the condition becomes false.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-while-loop
Python While Loop - GeeksforGeeks
When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements. Python Continue Statement returns the control to the beginning of the loop. ... # Prints all letters except 'e' and 's' i = 0 a = 'geeksforgeeks' while i < len(a): if a[i] == 'e' or a[i] == 's': i += 1 continue print(a[i]) i += 1
Published   December 23, 2025
🌐
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 :)