Table of contents
1.
Introduction
2.
What is the do-while loop in Python?
3.
Syntax of Python Do-While
4.
Example of Do While Loop
4.1.
Python
5.
Frequently Asked Questions
5.1.
Can a do-while loop have multiple conditions?
5.2.
Is it possible to have nested do-while loops in Python?
5.3.
When should I use a do-while loop instead of a regular while loop?
6.
Conclusion
Last Updated: Aug 12, 2025
Easy

Do While Loop in Python

Author Gaurav Gandhi
0 upvote

Introduction

A loop is a basic concept in programming that allows you to repeat a block of code multiple times. In Python, there are two main types of loops: the for loop and the while loop. However, there's another variation of the while loop called the "do while" loop in python.

Do While Loop in Python

In this article, we'll discuss what the do-while loop is, how it works, and when to use it in your Python programs. We will learn the syntax of the do-while loop with a clear example to help you understand its functionality.

What is the do-while loop in Python?

The “Python do-while” is a control flow statement that executes a block of code at least once before checking the loop condition. Unlike a regular while loop, which checks the condition first and then executes the code block if the condition is true, the do-while loop always runs the code block first and then checks the condition afterward. If the condition is still true after the first iteration, the code block is executed again. This process continues until the condition becomes false.

Syntax of Python Do-While

Here's the general syntax for a do-while loop in Python:

while True:
    # code block
    # ...
    if condition:
        break


In this syntax, the while True statement creates an infinite loop that will keep executing the code block until the break statement is encountered. The if statement inside the loop checks the condition, and if it's true, the break statement is executed, causing the loop to terminate.

It's important to note that Python doesn't have a built-in do-while loop like some other programming languages. However, we can simulate its behavior using a combination of while True and break statements as shown above.

Example of Do While Loop

Suppose we want to prompt the user to enter a positive number and keep asking until a positive number is provided. 

Here's how we can implement this using a do-while loop:

  • Python

Python

while True:

   number = int(input("Enter a positive number: "))

   if number > 0:

       break

   print("Invalid input! Please enter a positive number.")

print("You entered:", number)
You can also try this code with Online Python Compiler
Run Code


In this example, we start with a while True loop that will keep running until the break statement is encountered. Inside the loop, we prompt the user to enter a positive number using the input() function and convert the input to an integer using int().

We then check if the entered number is greater than 0 using an if statement. If the condition is true, we execute the break statement, which terminates the loop. If the condition is false, we print an error message indicating that the input is invalid and the loop continues to the next iteration.

After the loop ends, we print the positive number entered by the user.

Here's an example run of the program:

Enter a positive number: -5
Invalid input! Please enter a positive number.
Enter a positive number: 0
Invalid input! Please enter a positive number.
Enter a positive number: 10
You entered: 10


As you can see, the loop keeps prompting the user until a positive number is entered, demonstrating the behavior of a do while loop.

Frequently Asked Questions

Can a do-while loop have multiple conditions?

Yes, you can use logical operators like and or to combine multiple conditions in the if statement inside the loop.

Is it possible to have nested do-while loops in Python?

Yes, you can nest do while loops inside each other, just like you can with regular while loops or for loops.

When should I use a do-while loop instead of a regular while loop?

Use a do-while loop when you want the code block to execute at least once, regardless of the initial condition. Use a regular while loop when you want to check the condition before the first iteration.

Conclusion

In this article, we learned about the do-while loop in Python. We discussed the concept, syntax, and how it differs from a regular while loop. We also looked at an example that showed how to use a do-while loop to repeatedly prompt the user for input until a certain condition is met. 

Live masterclass