Emulating do-while Loops in Python

Introduction

A do-while loop is like a while loop, except that the condition for terminating the loop is checked at the end rather than at the beginning of the loop. Consequently, the loop’s block is guaranteed to execute at least once.

Many programming languages have their own syntax for the do-while loop, typically involving the keyword “do” at the beginning of the loop, followed by a code block and a while statement at the end. Python, however, does not have syntax for do-while loops. That’s perfectly fine because do-while loop functionality can be created from the while loop, and it’s not a construct that’s frequently used anyway, especially in comparison to the for loop.

Creating a do-while Loop in Python From while Syntax

If we were to have a do-while loop in Python, its syntax might look something like the following:

do:
  code-block
while condition

The above is not real Python syntax! With this fictional syntax, presumably the code in code-block executes once, then condition is checked at the end of each iteration. If condition is true, code-block is executed again.

Instead of doing that, to emulate the do-while logic, we can use a while loop with a True condition. The actual loop terminating condition is checked with an if at the very last statement within the loop, which executes a break to exit the loop.

while True:
    code-block
    if not condition:
        break

The above code will execute the exact same way as the preceding fictional do-while. Line 1 has a while True which will loop forever unless the break statement on line 4 executes. And it will execute when condition is not true as indicated by the if statement on line 3.

The not operator is there to show that the condition in the emulated do-while is the opposite of that in the conceptual do-while. That’s because it makes the loop exit using the break in the former, and keeps the loop going in the latter.

Example: Emulating a do-while Loop

This example recreates the name game, which is a silly song for any name that uses a formula. It was written by Shirley Ellis in 1965.

The name_game() function in the example, prints out the song given a name. The song’s formula and how this function works are not relevant for learning about the do-while loop. The important code starts on line 13.

Example:

def name_game(name):
    first_letter = name[0].lower()
    vowels = ('a', 'e', 'i', 'o', 'u')
    truncated_name = name if first_letter in vowels else name[1:]
    b_char = '' if first_letter == 'b' else 'b'
    f_char = '' if first_letter == 'f' else 'f'
    m_char = '' if first_letter == 'm' else 'm'
   
    print(f"\n{name}, {name}, bo-{b_char}{truncated_name}")
    print(f"Banana-fana fo-{f_char}{truncated_name}")
    print(f"Fee-fi-mo-{m_char}{truncated_name}")
    print(f"{name}!\n")

    
while True:
    name = input("Enter a name for the name game: ")
    name_game(name)
    go_again = input("Should we go again? (enter yes to continue)").lower()
    
    if go_again != 'yes':
        break

Output:

Enter a name for the name game: > Python

Python, Python, bo-bython
Banana-fana fo-fython
Fee-fi-mo-mython
Python!

Should we go again? (enter yes to continue) > no

The code in the loop plays the name game on lines 16 and 17. One line 18, the user is asked whether he or she would like to go again. If the answer is not ‘yes’ the loop exits using the break on line 21. Note that the if statement does not use the not operator, but the condition is still the opposite of what it would have been in a do-while loop. In the do-while, it would have been go_again == 'yes', flipping the not-equal != operator to equal ==.

Summary & Reference for Emulating do-while Loops in Python

A do-while loop is a loop in which the condition for terminating is checked at the end and consequently, the loop’s block is guaranteed to execute at least once.


Python does not have native syntax for do-while loops.


Do-while loops can be emulated in Python using a while loop with a True condition, and an if statement that checks the loop’s terminating condition and executes a break to exit the loop.

while True:
    code-block
    if not condition:
        break