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

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
๐ŸŒ
Python
python.org
Welcome to Python.org
The official home of the Python Programming Language
Discussions

What does : do in python
A line that ends with a : signals that a nesting level [edit: or "code block"] begins, meaning the following line (plus potentially more) will be indented. Typical cases are if condition: nested elif condition: nested else: nested for item in items: nested while condition: nested def function(args): nested class ClassName: nested try: nested except Exception: nested More on reddit.com
๐ŸŒ r/learnpython
13
5
May 3, 2024
What can I use Python for?
What are some cool or useful things you can use Python for? anything you can describe in pseudo-code that is grounded in reality. as long as it doesn't requires direct use of any magic/miraculous expectations.. then you can probably use python to work with it. i know it's very generic ..but that is the power of python. More on reddit.com
๐ŸŒ r/learnpython
110
144
July 27, 2023
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-do-while
Python Do While Loops - GeeksforGeeks
July 23, 2025 - Python loops only include for loop and while loop but we can modify the while loop to work as do while as in any other languages such as C++ and Java.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_operators.asp
Python Operators
Python Operators Arithmetic Operators Assignment Operators Ternary Operator Comparison Operators Logical Operators Identity Operators Membership Operators Bitwise Operators Operator Precedence Code Challenge Python Lists
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_intro.asp
Introduction to Python
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_conditions.asp
Python If Statement
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
๐ŸŒ
Python
python.org โ€บ about โ€บ gettingstarted
Python For Beginners | Python.org
Before getting started, you may want to find out which IDEs and text editors are tailored to make Python editing easy, browse the list of introductory books, or look at code samples that you might find helpful. There is a list of tutorials suitable for experienced programmers on the BeginnersGuide/Tutorials page. There is also a list of resources in other languages which might be useful if English is not your first language. The online documentation is your first port of call for definitive information.
๐ŸŒ
AWS
aws.amazon.com โ€บ what is cloud computing? โ€บ cloud computing concepts hub โ€บ developer tools โ€บ what is python?
What is Python? - Python Language Explained - AWS
4 days ago - Python is a programming language that is widely used in web applications, software development, data science, and machine learning (ML). Developers use Python because it is efficient and easy to learn and can run on many different platforms. Python software is free to download, integrates well ...
๐ŸŒ
Real Python
realpython.com โ€บ python-do-while
How Can You Emulate Do-While Loops in Python? โ€“ Real Python
August 2, 2022 - If you came to Python from a language ... loop construct. A do-while loop is a common control flow statement that executes its code block at least once, regardless of whether the loop condition is true or false....
๐ŸŒ
Real Python
realpython.com โ€บ what-can-i-do-with-python
What Can I Do With Python? โ€“ Real Python
October 22, 2025 - Learn how Python builds software, powers AI, automates tasks, and drives robotics. Discover tools and projects to guide your programming journey.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what does : do in python
r/learnpython on Reddit: What does : do in python
May 3, 2024 - You can think of : as a way of letting Python know you're about to start a code block that follows your current statement (such as what to do if a condition is fulfilled, as in your example).
๐ŸŒ
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 โ€บ ideas
Implement 'do' to simplify coding - Ideas - Discussions on Python.org
September 25, 2024 - What is โ€˜doโ€™ about? Well, itโ€™s a type of loop which instead of checking its condition at the beginning, it is checked at the end of the loop. The code is executed first and the condition is checked at the end of each loop.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial
The Python Tutorial โ€” Python 3.14.6 documentation
Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Pythonโ€™s elegant syntax an...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ what-can-i-do-with-python
What Can I Do With Python? - GeeksforGeeks
July 15, 2025 - Pygame, Python can be used to code a variety of arcade games, adventure games, and puzzle games that you can deploy within a few days. Classics like hangman, tic-tac-toe, ping-pong, and more are all doable with your newly acquired programming skills.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_for_loops.asp
Python For Loops
Python Examples Python Compiler ... Bootcamp Python Training ... 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 object-orientated programming languages. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc. ... The for loop does not require ...
๐ŸŒ
Pydoit
pydoit.org
pydoit โ€” Python Task Runner & Automation Tool
Run shell commands, call Python functions, or mix both in a single task. ... In practice: A game developer uses doit to automate code generation, cross-compilation, and resource generation โ€” simplifying cumbersome command line calls and skipping tasks already done.