While loop#
Overview#
The while
loop allows the execution of code statements during each iteration while certain condition remains true.
while condition:
# Code to be executed while the condition is true
Caution
The condition to stop the while loop should be carefully thought and the condition eventually becomes false, otherwise you can get stuck in the infinite loop
Examples#
Force the termination using
break
# Example 2
count = 0
while True:
print(count)
count += 1
if count >= 5:
break
0
1
2
3
4