Skip to content
ExamSpine

Lesson 3.2

Every loop needs an update; find the infinite loop

Part 3: RepetitionReading, about 4 min2 exercises in this lesson

Every loop needs an update

By the end of this you will be able to spot a loop that never stops, find the line it is missing, and check that each turn moves the loop toward stopping.

A while loop stops only when its condition becomes false. The condition is made of variables, so something inside the loop body has to change one of those variables. That change is the loop's update. With no update, the condition gives the same answer on every check.

A loop with no update

This program is meant to repeat each word until the user types stop. Do not run it: it has a bug.

word = input()
while word != "stop":
    print("Got: " + word)
print("Done")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    String word = scanner.nextLine();
    while (!word.equals("stop")) {
        System.out.println("Got: " + word);
    }
    System.out.println("Done");
}

Say the user types hello. The condition is true, so the body prints Got: hello. Then the condition is checked again. Nothing in the body changed word, so it is still hello, the condition is still true, and the body prints Got: hello again. This never ends. Done is never printed, and the program never reads a second line, so typing stop now does not help.

This is an infinite loop (an endless loop). The code is valid, so there is no syntax error. It is a logic error: the program does the wrong thing. The sign is a program that seems stuck, or prints the same line again and again.

Notice one more thing. If the very first word is stop, this program works: the body never runs, and Done prints. A test that types only stop would pass. An endless loop can hide behind a test that never enters the loop.

How to find the missing update

Ask three questions about any loop.

  1. Which variables does the condition use? Here, only word.
  2. Does the body change at least one of them? Here, no. That is the bug.
  3. Does the change move the loop toward making the condition false?

The fix is the read at the end of the body, the last step of the read-check-read pattern from the last lesson. Each new read gives the user a chance to type stop.

word = input()
while word != "stop":
    print("Got: " + word)
    word = input()
print("Done")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    String word = scanner.nextLine();
    while (!word.equals("stop")) {
        System.out.println("Got: " + word);
        word = scanner.nextLine();
    }
    System.out.println("Done");
}

An update that goes the wrong way

Having an update is not enough. Question 3 matters too. This countdown is meant to print 10, 9, 8 and so on down to 1. Do not run it either.

n = 10
while n > 0:
    print(n)
    n = n + 1
print("Lift off")
public static void main(String[] args) {
    int n = 10;
    while (n > 0) {
        System.out.println(n);
        n = n + 1;
    }
    System.out.println("Lift off");
}

The body does change n, so question 2 passes. But n goes 10, 11, 12, 13, moving away from 0. The condition n > 0 stays true turn after turn, so instead of stopping after 10 turns the loop keeps printing bigger and bigger numbers, far past 10. An update that goes the wrong way is a bug, just like a missing update.

To check the direction, look at the condition and ask which way the variable must move to make it false. Here n > 0 becomes false only when n gets small enough, so n must go down. The fix is n = n - 1:

n = 10
while n > 0:
    print(n)
    n = n - 1
print("Lift off")
public static void main(String[] args) {
    int n = 10;
    while (n > 0) {
        System.out.println(n);
        n = n - 1;
    }
    System.out.println("Lift off");
}

Now n goes 10, 9, and so on down to 0, the condition becomes false, and Lift off prints.

The rule

Every loop body needs an update that changes a variable in the condition, and each update must move the loop toward stopping. When you write a loop, write the update as soon as you write the condition, before you fill in the rest of the body.

Now try it yourself on the questions below.

Exercise 3.2.13.2.1 · Answer itFind infinite loop

A student wrote this program to print every number from 1 to 5:

n = 1
while n <= 5:
    print(n)
int n = 1;
while (n <= 5) {
    System.out.println(n);
}

(a) State what happens when the program runs. [1]

(b) State what is missing from the loop body that causes this problem. [1]

(c) Write the one line that should be added, and state where it goes. [1]

[3]

Exercise 3.2.23.2.2 · Answer itLoop update rule

A while loop checks a condition before each repetition.

(a) Explain why the loop body must contain a statement that can eventually make the condition false. [1]

(b) State what kind of error occurs if no such statement exists. [1]

[2]