4) a. While Loop

Another kind of loop that is commonly used in programming is a while loop. It allows to repeat certain action as long as a given condition is true. Take a look at this example.

This is a simple implementation of a countdown where with each iteration we check the value of i which acts like a counter.

It is important to not forget to change the value of i. Unlike the range loop which has clearly defined number of iterations, a while can go forever if you let it.

1
2
3
4
while True:
    print("Can't stop, won't stop.")

# This will run endlessly.

The only way to stop this script is to terminate Python all together so you should always look out for these infinite loops.