AlgoCademy
Lesson
Code

Infinite for loops in {lang}


Remember the infinite while loop? Well, there is also such a thing called the infinite for loop.

We use for loops to iterate over sequences like strings and lists. We can run into problems when we manipulate a sequence while iterating on it.

For example, if we append elements to a list while iterating on it:

fruits = ["banana", "orange"]
for fruit in fruits:
  fruits.append("kivi")

Every time we enter this loop, we add a kivi item to the end of the list that we are iterating through.

As a result, we never make it to the end of the list. It keeps growing forever!

This is an infinite for loop. You can imagine that as programmers, we want to make sure we never write infinite loops as they make our program run forever and completely unusable.


Assignment
Follow the Coding Tutorial and let's write some while loops.


Hint
Look at the examples above if you get stuck.