Emulate a Do-While Loop in Python



Python doesn't have an equivalent of do-while loop as in C/C++ or Java. The essence of do-while loop is that the looping condition is verified at the end of looping body. This feature can be emulated by following Python code −

Example

condition=True
x=0
while condition==True:
     x=x+1
     print (x)
     if x>=5: condition=False

Output

The output is as follows −

1
2
3
4
5
Updated on: 2020-06-19T11:55:40+05:30

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements