How to emulate a do-while loop in Python?

PythonServer Side ProgrammingProgramming

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
raja
Published on 17-Feb-2018 13:34:02
Advertisements