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
How to learn for loop, do while loop and functions?
It seems like you have started learning python from the data analysis point of view, using a Jupyter notebook for data research and processing. Python can also be used as a programming language to create applications and scripts, you will find functions loops and other structures there more. I'd recommend trying to learn python from that point of view, hopefully that will help you complete the picture and understand it all better. Try this free online book: https://automatetheboringstuff.com/#toc More on reddit.com
🌐 r/learnpython
5
3
March 20, 2023
While loop help

You're very close, you just have line 13 indented one to many times.

def choosedoor():
door = ''
while door != '1' and door != '2':
print ("Which door do you choose? (1 or 2)")
door = input()
return door
More on reddit.com
🌐 r/learnpython
6
8
February 18, 2018
While loop not breaking.

I don't see exactly what's wrong, but maybe it's because you've only given us the gist. However if you're looking for breaking on a specific condition, that's exactly what the while loop condition is for. I'd try something like this:

marker = None
while marker not in ['X','O']:
marker = input("Select Marker 'X' or 'O' ").upper()
if marker == 'X':
...
More on reddit.com
🌐 r/learnpython
4
2
September 20, 2017
🌐
W3Schools
w3schools.com › PYTHON › python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Python Bootcamp Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
🌐
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.
🌐
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.
Find elsewhere
🌐
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-do-while
How Can You Emulate Do-While Loops in Python? – Real Python
August 2, 2022 - For example, if the loop condition implies costly operations and the loop must run n times (n ≥ 1), then the condition will run n times in a do-while loop. In contrast, a regular while loop will run the costly condition n + 1 times. Python doesn’t have a do-while loop construct.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-while.html
While Loop
While Operation: Check the boolean test expression, if it is True, run all the "body" lines inside the loop from top to bottom. Then loop back to the top, check the test again, and so on. When the test is False, exit the loop, running continues on the first line after the body lines. Here is a while loop to print the numbers 0, 1, 2, ... 9 (there are easier ways to do this, but here we're just trying to show the parts of the loop).
🌐
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
🌐
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).
🌐
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....
🌐
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.
🌐
iO Flood
ioflood.com › blog › python-do-while
Python Do-While Loop: Ultimate How-To Guide
June 7, 2024 - In Python, you can use an else statement with a while loop. The else block executes after the while loop finishes, but not if the loop is exited with a break statement. Here’s an example with a do-while 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 ...
🌐
Reddit
reddit.com › r/learnpython › how to learn for loop, do while loop and functions?
r/learnpython on Reddit: How to learn for loop, do while loop and functions?
March 20, 2023 -

I work as an Analyst, have some experience with Python, like using groupby, filter conditions, joins, windows function, rank function to basically get the business logic to code. I see people who are efficient in coding tend to write their code as a single function rather than going through each step one at a time like cells in jupyter notebook. I want to learn how can I be better at writing loops, conditions and functions together as a whole. Like for example my coworker built a function where he declared a empty list outside the function, use it within, used counters and basically ran a groupby with agg to include the function fetching the desired output as a dataframe. How will I be able to work on such complex functions using loops and other parameters?

🌐
Upgrad
upgrad.com › home › tutorials › software & tech › python’s do-while loop
Python’s do-while Loop
November 18, 2024 - The do-while loop can be compared to a situation where you implement an action and later determine whether to continue based on the result of that step. It provides a means to iteratively execute a set of instructions while a specific condition holds true, but it guarantees the execution of the code block at least once. ... In Python programming language, there is no specific built-in do-while loop ...
🌐
Python Tutorial
pythontutorial.net › home › python basics › python do…while loop statement emulation
Python do while loop Emulation
March 31, 2025 - Unfortunately, Python doesn’t support the do...while loop. However, you can use the while loop and a break statement to emulate the do...while loop statement. First, specify the condition as True in the while loop like this:
🌐
Python Central
pythoncentral.io › python-do-while-loop
Python Do While Loop: Step by Step Walkthrough | Python Central
March 18, 2025 - Here is how you can write a Python code for the same example written in C language earlier: x = 0 while True: print(f"Iteration: {x}") x += 1 if x >= 5: break · Let us break down this code for you to understand better. The loop executes at least once, because the condition is "True" initially.