How to 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: 19-Jun-2020

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements