How to convert a Python for loop to while loop?

C++Server Side ProgrammingProgramming

Unlike while loop, for loop in Python doesn't need a counting variable to keep count of number of iterations. Hence, to convert a for loop into equivalent while loop, this fact must be taken into consideration.

Following is a simple for loop that traverses over a range

for x in range(5):
     print (x)

To convert into a while loop, we initialize a counting variable to 0 before the loop begins and increment it by 1 in every iteration as long as it is less than 5

while x<5:
     print (x)
Published on 19-Feb-2018 23:45:14