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 do while loop the statement runs at least once no matter whether the condition is false or true. ... In this example, we are going to print multiple of 2 using the do while loop.
🌐
freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - For example, when you're writing a program that takes in input from users you may ask for only a positive number. The code will run at least once. If the number the user submits is negative, the loop will keep on running.
🌐
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
🌐
Real Python
realpython.com › python-do-while
How Can You Emulate Do-While Loops in Python? – Real Python
August 2, 2022 - The main difference is that in this case, you’re using a regular while loop because Python doesn’t have do … while loops. In this Python implementation, when the user guesses the secret number, the else clause runs, breaking the loop. The final line of code prints the successful guess message. Using an infinite loop and a break statement like you did in the example above is the most widely used approach for emulating a do-while loop in Python.
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - If you come from languages like C, C++, Java, or JavaScript, then you may be wondering where Python’s do-while loop is. The bad news is that Python doesn’t have one. The good news is that you can emulate it using a while loop with a break statement. Consider the following example, which takes user input in a loop:
🌐
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. Here's an example:
🌐
DataCamp
datacamp.com › tutorial › python-while-loop
Python While Loops Tutorial | DataCamp
June 25, 2020 - In the below example, you will create the variable offset with an initial value of 8. Then you will write a while loop that keeps running as long as the offset is not equal to 0. ... You will print out the sentence "correcting...". Next, decrease the value of offset by 1. You can do this with ...
Find elsewhere
🌐
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 which is not in python it can be done by the above syntax using while loop with break/if /continue statements. In this, if the condition is true, then while statements are executed, if not true, another condition is checked ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Coursera
coursera.org › tutorials › python-while-loop
How to Write and Use Python While Loops | Coursera
In this example, the while loop iterates over the numbers 1 through 10. The if statement checks whether the current value of i is even. If it is, the continue statement is executed, causing the loop to skip the current iteration and move on ...
🌐
Server Academy
serveracademy.com › blog › python-while-loop-tutorial
Python While Loop Tutorial - Server Academy
In such cases, you can use the break statement to terminate the loop immediately. number = 1 while number <= 10: print(number) if number == 5: break number += 1 ... Here, the loop is set to iterate until number is greater than 10, but when number ...
🌐
Texas Instruments
education.ti.com › en › resources › computer-science-foundations › do-while-loops
Python coding: Do While Loops | Texas Instruments
Note that this loop structure is slightly different than Do While loop structures that appear in other programming languages. Python doesn’t have a built-in Do While loop structure, but the behavior of a Do While loop can be modeled with programs that use While, If and break commands as we’re exploring here.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 39804 › do-while-loop-in-python
do-while loop in python [SOLVED] | DaniWeb
Use a boolean updated at the end of each iteration (avoid strings like "true"/"false", and prefer logical and over bitwise &): finished = False while not finished: # loop body # ...
🌐
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 - Otherwise, the loop continues. For example, if we want to ask the user to enter a number between 1 and 10 and repeat the question until the user enters a valid number, we can use a do-while loop like this:
🌐
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Examples)
In the above example, we have used a while loop to print the numbers from 1 to 3. The loop runs as long as the condition number <= 3 is True. ... The while loop evaluates condition, which is a boolean expression. If the condition is True, body of while loop is executed.
🌐
Coderwall
coderwall.com › p › q_rd1q › emulate-do-while-loop-in-python
Emulate do-while loop in Python (Example)
October 10, 2024 - Though Python doesn't have it explicitly, we can surely emulate it. ... We can write the equivalent for the do-while in the above C program using a while loop, in Python as follows:
🌐
Scaler
scaler.com › home › topics › python do while loop
Python Do While Loop - Scaler Topics
December 1, 2023 - Let’s see one more example of printing the first five multiples of 2 using both implementations. ... The explanation for these two programs is similar to the code samples above. ... Since there is not an in-built do while loop in python, we implemented the do-while Loop in Python using for-loop and while-loop, which are already present in the language.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-emulate-a-do-while-loop-in-python
How to Emulate a Do-while loop in Python? - GeeksforGeeks
July 23, 2025 - The loop continues until the index exceeds the length of the list, at which point the flag is set to False, and the loop terminates. ... # Example list of strings strings_list = ["geeksforgeeks", "C++", "Java", "Python", "C", "MachineLearning"] print("Strings in the list:") # Using a while loop with a flag variable flag = True index = 0 while flag: # Execute the code block at least once current_string = strings_list[index] print(current_string) # Increment the index index += 1 # Set the flag to False if the loop should terminate if index >= len(strings_list): flag = False # Code block outside the loop print("Loop has exited.")
🌐
Python Tutorial
pythontutorial.net › home › python basics › python do…while loop statement emulation
Python do while loop Emulation
March 31, 2025 - The following program uses a while ... attempt input_number = int(input(f'Enter a number between {MIN} and {MAX}:')) attempt += 1 if input_number > secret_number: print('It should be smaller.') elif input_number < secret_number: print('It should be bigger.') else: print(f'Bingo!...