How to implement (Emulate) do-while like Loop in Python?

Table of Contents

Since there is no do-while loop in python like in C / C++ programming language. The key features of a do-while loop is body of the loop always executes at least once even if the initial condition is FALSE.

do-while loop is very handy when we need to execute body of loop at least once. So, In this article we will discuss about how to emulate do-while like feature in python programming language.

Emulating do-while Loop in Python


condition = True

while condition:
	# body of loop here 
	condition = loop_test_condition

do-while Example


count = 1

condition = True

while condition:
    print('Welcome to python - %d' % count)
    count = count + 1
    condition = count <= 5

do while output

Welcome to python - 1
Welcome to python - 2
Welcome to python - 3
Welcome to python - 4
Welcome to python - 5