Python provides the for and while loop in order to iterate a given list, iterable object, or for conditions. Even it is not much popular as for loop while loop also used to for different cases. Popular programming languages like C, C#, and Java provide the do-while loop but Python does not provide the do-while loop. So if Python does not provide the do-while loop how can we emulate it?
Emulate The do-while Loop In Python
The do-while loop works like this. First, the do part of the do-while loop should be executed before the condition check. So we will set the while condition is True which will continue forever. But how can define the end condition for this do-while loop? We will use if conditional inside the while loop which will execute the break statement that will end the while loop.
a = 1 while True: print(a) a += 1 if (a>=10): break
The output will be like below.
1 2 3 4 5 6 7 8 9