Trusted answers to developer questions

How to emulate a do-while loop in Python

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

At times, we encounter situations that require us to use the do-while loop in Python. A do-while loop is important because it is a post-test loop, meaning that it checks the condition only after executing the code in the loop block.

Even though Python doesn’t explicitly have the do-while loop, we can emulate it.

General syntax

svg viewer

Emulating a do-while loop in Python

There are two scenarios in which a loop terminates:

  • The loop condition is no longer true/false (depending on the type of loop).
  • A break statement is executed from within the code in the loop body.

If the condition is to be checked after executing the loop block, we can bring the condition inside the loop block, set the loop condition to be always true, and break the loop once the condition is met inside ​the code block.

Have a look at the code,for printing the first 3 integers,below:

i = 1
while True:
print(i)
i = i + 1
if(i > 3):
break

The do-while loop in C++ and the emulation of the same loop in Python result in the same output. In the Python emulation, i is being printed before the condition is checked, just ​like in the do-while loop in C++.

RELATED TAGS

do-while
loop
pyhton
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?