Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
The for loop
The while loop
10. Infinite loops
Nested loops
break and continue


Perfect! The while loop may seem easy to write, but it's also very easy to create a so-called infinite loop. For instance, when you forget to increment the value of counter:

counter = 1
while counter <= 10:
  print('Current value:', counter)

In the program above, counter will always be equal to 1, so the condition in the while loop will be always True. This means, in turn, that the program will try to print Current value: 1 forever. Your program will run forever, and you will have to force it to quit. Our platform automatically kills long-running programs, but in real life you have to manually force the program to quit.

When you create a loop, always think about its termination condition to avoid situations like the one above.


Take a look at the template code. Its aim is to print all odd numbers below 100. However, something is wrong with the code, and we end up with an endless loop. Fix the code.

Stuck? Here's a hint!

The termination condition is wrong. We only check whether current_value is different than 100. However, if we increase the value by 2 each time, current_value will be equal to 99 at some point, and it will then "jump over" the number 100 to the value of 101.