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

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
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
How to write a loop with conditions
Hello, I have to write a loop that prompts a user for their age in a program that outputs an admission price for a cinema ticket (depending on the user’s age) I have just started writing basic loops like this one below, but I don’t even know where to start regarding creating loops where ... More on discuss.python.org
🌐 discuss.python.org
0
0
January 11, 2022
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
🌐
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
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.
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
In a for loop, the else clause is executed after the loop finishes its final iteration, that is, if no break occurred. In a while loop, it’s executed after the loop’s condition becomes false.
Find elsewhere
🌐
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 :)

🌐
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.
🌐
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....
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-while.html
While Loop
The if/break can be positioned anywhere in the loop vs. the while/test can only be at the top of the loop. Here is the previous example with a break added to leave the loop if the number is 6: i = 0 while i < 10: print(i) if i == 6: break i = i + 1 print('All done') 0 1 2 3 4 5 6 All done
🌐
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.
🌐
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…
🌐
Python.org
discuss.python.org › python help
How to write a loop with conditions - Python Help - Discussions on Python.org
January 11, 2022 - Hello, I have to write a loop that prompts a user for their age in a program that outputs an admission price for a cinema ticket (depending on the user’s age) I have just started writing basic loops like this one below,…
🌐
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.
🌐
Reddit
reddit.com › r/learnpython › is it weird that i never use while loops? it’s just not something i’ve felt like i needed, ever. what are some situations where using a while loop is necessary?
r/learnpython on Reddit: Is it weird that I NEVER use while loops? It’s just not something I’ve felt like I needed, ever. What are some situations where using a while loop is necessary?
September 29, 2022 -

I’ve written a lot of python code over the last few months, but I can’t think of a single time I’ve ever used a while loop. I mean, I use for loops pretty often, but not while loops.

Is this just down to personal preference, and I’m just using what I’m comfortable with? Or can you guys think of some situations where a while loop would be the easiest way to do things, but it’s possible to do it with a for loop? Maybe I’m using for loops in situations that I should be using while loops.

EDIT: Thanks for the suggestions. I found a few places in my code where a while loop makes sense.

First, I check if a folder has any files in it, and while it does, I delete the first one in the list:

useless_files = os.listdir("MGF_HR_Data") # I think these are files it creates when downloading the MGF data but they are extra and don't do anything 
while len(useless_files)>0: 
    os.remove(f"MGF_HR_Data/{useless_files[0]}") 
    useless_files = os.listdir("MGF_HR_Data") 
    print("Deleting useless file from MGF_HR_Data") 

I also used a while loop to check if the data has been downloaded, and if it hasn't prompt the user to download it and then press enter to check again. This way, the code doesn't break if the user forgot to download the file first:

# Check if you downloaded the file in Matlab already. If not, ask the user to download it and try again. 
while os.path.exists(file1)==False: 
    print(f"{file1} not found. Please open Matlab and run the following code to download the MGF data:" )
    print(f"download({wstime.year}, {wstime.month}, {wstime.day})")
    input("Press enter to try again after downloading the MGF data. \n") 

(Okay, I know this isn't the most efficient way to do this. There must be a way to use python to open matlab and run a matlab command, rather than asking the user to do it themselves. However, I don't know how to do that, so this works for now.)

🌐
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 ...
🌐
Hackr
hackr.io › home › articles › programming › python
Python Do While | Docs With Examples
February 12, 2025 - Python does not have a built-in do-while loop like some other languages (e.g., C, Java).
🌐
Alma Better
almabetter.com › bytes › tutorials › python › while-loop-in-python
While Loop in Python
October 2, 2024 - The Python While Loop 🔁 repeatedly executes a block of statements until a specified condition :thinking: becomes false.
🌐
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.