M&C4:  Infinite Loops

The Scratch examples included programs with infinite loops. Using an infinite loop is quite common in Scratch when visualizing activities until the user decides to stop them. But we don’t want to write Python programs with infinite while loops!  Unintended infinite loops happen for various reasons. Every programmer creates an infinite loop once in a while.

Once your students start writing while loops, make sure they know how to terminate an infinite loop! How to terminate an infinite loop and how graceful the termination is depends on the programming environment used. In Canopy, infinite loops can often be terminated by using Run -> Interrupt Kernel or by using  Run -> Restart Kernel. However, this may not work all the time. If it does not, one may unfortunately have to kill Canopy. For the programs below having an infinite loop, consider  using PythonTutor. PythonTutor will terminate after a fixed number of steps and let you know that it suspects an infinite loop. This also allow your students to trace the execution.

The following videos describe and illustrate common situations causing infinite loops. They include:

  1.  Omitting or changing the statement that advances an index needed for termination. Quite common is changing the indentation and thus placing a statement outside the loop.  Print all integers from 1 to n (with a bug causing an infinite loop):
n = input('Please input an integer n: ')
i = 1
while (i <= n):
   # i = i +1  Without this statement, the loop will not stop
  1. Having a Boolean condition that is always true.  The intent of the code below is to terminate once i reaches 100.  However, variable i never does have the value of 100 and we have an infinite loop.
i = 1

while i != 100:
    print i
    i = i + 7 

The condition in the while loop should have been “while i < 100”.

Good advice is to think carefully how a while loop will terminate and what situation happen for different values used in the loop.

Infinite Loops Part I  (Video Length: 5:09)

Infinite Loops Part II  (Video Length: 7:01)

An infinite loop is often caused by a while loop. Placing print statements in an infinite loop may be of limited use as the program may just print and print, most likely faster than one can read.  Train your students to take a close look at the condition in the while loop and to check that it tests what they want it to test. Run the program with other parameters.  Change the values of the variables and run the program with simple values.

Programs can run correctly in some cases, but fail in others.  If a student does not see what is causing the infinite loop, add a condition that executes the loop only for a fixed number of times and print the variables changing inside the loop. Now it is easier to trace the execution. This may result in seeing which code segment is not making progress as expected.

An infinite loop can also be caused by a mistake in the logic of the loop. In this case, make students step back and rethink the approach taken and what the loop/program is actually supposed to do.

In summary, ask students to check that

  • the loop variable changes during the execution of the body
  • the loop variable is not modified in a way with unintended consequences
  • boundary conditions are executed properly.