Skip to main content

Python do while Loop

The do...while loops are used when you have a block of code which you want to repeat at least one time and ** until a defined condition is no longer met**.

Syntax

Python does not support the do...while loop.

However, you can use the while loop and a break statement to emulate the do...while loop statement.

while True:
<statement>
<statement>
...

if condition
break

Example

For example, read numbers until 0 is entered:

while True:
num = int(input("Enter a number: "))
print(f"The number entered is {num}")
if num == 0:
print(f"The end.")
break

Output

Enter a number: 10
The number entered is 10
Enter a number: 3
The number entered is 3
Enter a number: 0
The number entered is 0
The end.

Table of Contents