Robert Johns | 12 Feb, 2025
Fact checked by Jim Markus

Python Do While | Docs With Examples

Python does not have a built-in do-while loop like some other languages (e.g., C, Java). However, you can simulate the behavior of a do-while loop using a while loop.

What is a Do-While Loop?

A do-while loop ensures that a block of code runs at least once before checking the condition. The typical structure in other languages looks like this:

// Example in C
int x = 0;
do {
    printf("x is: %d\n", x);
    x++;
} while (x < 5);

This guarantees the loop executes at least once, even if the condition is False at the start.

Simulating a Do-While Loop in Python

In Python, you can achieve similar behavior using a while loop with a break condition inside:

x = 0
while True:
    print("x is:", x)
    x += 1
    if x >= 5:
        break

Explanation:

  • The while True: loop runs indefinitely.
  • The body of the loop executes at least once.
  • The break statement exits the loop when the condition is met.

Another Approach: Using while with a Flag

Another way to simulate a do-while loop in Python is by initializing a flag:

x = 0
do_continue = True

while do_continue:
    print("x is:", x)
    x += 1
    do_continue = x < 5

Explanation:

  • The do_continue variable ensures the loop runs at least once.
  • The loop condition is updated at the end of each iteration.

Common Use Cases for a Do-While Pattern

  • User Input Validation: Ensuring valid user input before proceeding.
  • Menu-Driven Programs: Running a program loop until the user chooses to exit.
  • Processing Data Streams: Handling input until a stopping condition is met.

Example: User Input Validation

while True:
    num = input("Enter a positive number: ")
    if num.isdigit() and int(num) > 0:
        break
    print("Invalid input, try again.")
print("You entered:", num)

Key Takeaways

  • Python does not have a do-while loop but can simulate it with while True and break.
  • Use a flag variable to manage loop execution when needed.
  • This pattern is useful for user input validation, menus, and data processing in Python projects.

Practice Exercise

Here's a simple challenge, open up your Python editor and try to write a Python script that keeps asking for a password until the correct one is entered:

correct_password = "python123"
while True:
    password = input("Enter password: ")
    if password == correct_password:
        print("Access granted.")
        break
    print("Incorrect password, try again.")

Wrapping Up

Although Python lacks a do-while loop, simulating its behavior is easy using while True with break. Understanding this pattern will help you handle scenarios where at least one execution is necessary before checking conditions. Happy coding!

By Robert Johns

Technical Editor for Hackr.io | 15+ Years in Python, Java, SQL, C++, C#, JavaScript, Ruby, PHP, .NET, MATLAB, HTML & CSS, and more... 10+ Years in Networking, Cloud, APIs, Linux | 5+ Years in Data Science | 2x PhDs in Structural & Blast Engineering

View all post by the author

Subscribe to our Newsletter for Articles, News, & Jobs.

I accept the Terms and Conditions.

Disclosure: Hackr.io is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.

In this article

Learn More

Please login to leave comments