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

New to python and I need some help with loops.
Loops allow you to repeat a block of code over and over again. There are two main types of loops in Python - for loops and while loops. For loops are used when you know how many times you want to repeat the code. # This will print the numbers 0 to 4 for i in range(5): print(i) The range(5) part creates numbers from 0 to 4. The for loop will go through each of those numbers, storing them in the variable i, and print them out. While loops are used when you don't know exactly how many times you need to repeat the code. You keep looping as long as some condition is true. # This will print numbers as long as x is less than 5 x = 0 while x < 5: print(x) x = x + 1 This starts x at 0, then keeps looping as long as x is less than 5. Each time, it prints x and then adds 1 to x. Once x reaches 5, the loop stops. More on reddit.com
🌐 r/learnpython
12
6
August 31, 2023
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
January 11, 2022
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
Hi, Usually in Python we can avoid the i = 0 … i += 1 paradigm that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always found a more “pythonic” replacement for code containing i = 0 … i += 1. ... More on discuss.python.org
🌐 discuss.python.org
4
August 10, 2022
How to properly do 'game loop' in python?
"Standard" loop is: running = True while running: process_events() # process input and other stuff update() # update all objects that need to be updated, e.g. position changes, physics, all that other stuff draw() #render things on screen running = False it set in first part where input is processed, the loop exits and the game ends. It's advised not to have other loops inside this one, I mean, while loops, or entire thing will freeze, obviously. events and update can be merged, but note that input should be captured before any movement happens. Also there's no such thing as "actions" or "choices", everything is done exactly the same every frame, the only difference is what happens depending on what is in the game and so on. More on reddit.com
🌐 r/learnpython
2
2
December 2, 2017
🌐
freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - There are cases where you would want your code to run at least one time, and that is where do while loops come in handy. 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. If it is positive, it will stop. Python does not have built-in functionality to explicitly create a do while loop like other languages.
🌐
Python 101
python101.pythonlibrary.org › chapter5_loops.html
Chapter 5 - Loops — Python 101 1.0 documentation
We are going to loop over a range, but we want to print out only the even numbers. To do this, we want to use a conditional statement instead of using the range’s step parameter. Here’s one way you could do this: >>> for number in range(10): if number % 2 == 0: print(number) 0 2 4 6 8 · You’re probably wondering what’s going on here. What’s up with the percent sign? In Python, the % is called a modulus operator.
🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other ...
🌐
Reddit
reddit.com › r/learnpython › new to python and i need some help with loops.
r/learnpython on Reddit: New to python and I need some help with loops.
August 31, 2023 -

For the past 2 months I have been doing the Codecademy Python 3 course and last month I went into loops and functions and I was incredibly overwhelmed just like I was in Java. Functions are easy nothing special, at least yet, but loops are made me take that month break.

I know the theory of loops. I think I know what commands should be used where and when but for the love of god I can not write a simple loop, even after countless errors. Could anyone point me in the right direction? I really dont want to quit another language for the same reason.

Edit: a user pointed out that I need to elaborate on "simple loops". For example if I had a list and wanted to print a sentence as many times as the length of the list I know I would use len and range and have the print statement inside the loop but I can't implement it

Top answer
1 of 5
2
Loops allow you to repeat a block of code over and over again. There are two main types of loops in Python - for loops and while loops. For loops are used when you know how many times you want to repeat the code. # This will print the numbers 0 to 4 for i in range(5): print(i) The range(5) part creates numbers from 0 to 4. The for loop will go through each of those numbers, storing them in the variable i, and print them out. While loops are used when you don't know exactly how many times you need to repeat the code. You keep looping as long as some condition is true. # This will print numbers as long as x is less than 5 x = 0 while x < 5: print(x) x = x + 1 This starts x at 0, then keeps looping as long as x is less than 5. Each time, it prints x and then adds 1 to x. Once x reaches 5, the loop stops.
2 of 5
2
There are 3 kinds of loops that i can come up with that doesnt include itertools. 1: you have a loop that continues for x amount of iterations by being specific on how many iterations. This loop doesnt let you do anything with the values themself since they are all ints. Example: for i in range(5): (this will do the block of code 5 times starting from 0 and excluding the 5 and i will only be the integers 0 to 4) 2: you have a loop that will do the same as the first loop but this time its through all values in what you are looping through unless you specify on which values you want to loop through using indecies [start, stop, skip] if its a list or a string that is. Example: for my_value in my_list: (this will go through all the values in our list since we didnt specify what values to go through) 3: a condition based loop that will continue until the condition is met. Example of similar principal as the first loop: while i < 5: i+=1 (this will do the code block until i isnt less than 5, if you dont add to i it will never break since then i can never become not less than 5) So if you want to look through the actual values in something you have go with (2), if you want something to go a set number of times go with (1), if you want something to go until you tell it to stop do (3). If you want to know how to write better loops you should look into best practices for that, it doesnt mean like do tasks but just what to think about when making a loop and other things, there is ofc a bunch of mixed thoughts in what is the best and what not but being the best doesnt really mean anything, if it works it works. For your problem you dont need a loop, you already know len() so if you do print("hello")*len(my_list) it will print the msg as many times as the lenght of the list, but you can ofc use loops too, you can have that as excercise by knowing (1) and (3) and len and range.
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.
🌐
Python
wiki.python.org › moin › ForLoop
ForLoop
In Python, these are heavily used whenever someone has a list of lists - an iterable object within an iterable object. for x in range(1, 11): for y in range(1, 11): print('%d * %d = %d' % (x, y, x*y)) ... Like the while loop, the for loop can be made to exit before the given object is finished. This is done using the break statement, which will immediately drop out of the loop and continue execution at the first statement after the block.
🌐
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,…
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
In most such cases, however, it ... see Looping Techniques. A strange thing happens if you just print a range: ... In many ways the object returned by range() behaves as if it is a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really ...
🌐
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.
🌐
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.
🌐
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.
🌐
Dataquest
dataquest.io › blog › python-for-loop-tutorial
Python for Loop: A Beginner's Tutorial – Dataquest
March 10, 2025 - To create a Python for loop, you start by defining an iteration variable and the iterable object you want to loop through. The iteration variable temporarily holds each item from the iterable during each loop.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-for-loop-example
Python for loop | DigitalOcean
March 14, 2024 - words= ["Apple", "Banana", "Car", "Dolphin" ] for word in words: #This loop is fetching word from the list print ("The following lines will print each letters of "+word) for letter in word: #This loop is fetching letter for the word print (letter) print("") #This print is used to print a blank line ... Python range is one of the built-in functions.
🌐
Learn Python
learnpython.org › en › Loops
Loops - Learn Python - Free Interactive Python Tutorial
Loop through and print out all even numbers from the numbers list in the same order they are received. Don't print any numbers that come after 237 in the sequence.
🌐
Python.org
discuss.python.org › ideas
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
August 10, 2022 - Hi, Usually in Python we can avoid the i = 0 … i += 1 paradigm that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always found a more “pythonic” replacement for code containing i = 0 … i += 1. There is an exception with this code: an infinite loop with a counter: i = 0 while True: ... if breaking_condition: break i += 1 Proposal: could we accept that range() without any parameter ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
If we want a block of code to execute infinite number of times then we can use the while loop in Python to do so.
Published   1 month ago
🌐
Coursera
coursera.org › tutorials › for-loop-python
How to Use For Loops in Python: Step by Step | Coursera
Write your loop statements in an indented block. The indentation lets Python know which statements are inside the loop and which statements are outside the loop. ... 1 2 # Write a for loop that prints the numbers from 1 to 10, inclusive. # (Hint: There's more than one way to do this)