Unit 3: Programming · Repetition

Article

Tracing a loop

8 min read

The exam gives you a loop and asks, "What is displayed?" You cannot guess it from the shape of the code. You have to be the computer: run the code by hand, one step at a time, and write down every value. This is called tracing, and it is the most useful programming skill on the exam.

Five steps for tracing a loop

  1. Write the name of every variable as a column in a table.
  2. Fill in the starting values from the lines before the loop.
  3. Before each pass, check the condition (for REPEAT UNTIL, stop if it is true). For REPEAT n TIMES, count the passes.
  4. Run the body line by line, writing the new value of any variable that changes.
  5. When the loop ends, carry on with the lines after it. Write down everything DISPLAY shows, in order.

A trace table has one row per pass. If two rows are exactly the same, the loop will never end.

Here is a loop with an IF inside it. It counts the even numbers (n MOD 2 = 0 means "n divided by 2 leaves remainder 0"):

AP pseudocode
count ← 0
n ← 1
REPEAT UNTIL(n > 6)
{
    IF(n MOD 2 = 0)
    {
        count ← count + 1
    }
    n ← n + 1
}
DISPLAY(count)
ChecknStop? (n > 6)n MOD 2 = 0?count after the passn after the pass
11falsefalse02
22falsetrue13
33falsefalse14
44falsetrue25
55falsefalse26
66falsetrue37
77true

It displays 3. The Python version has the same table:

Runs in your browser

Check your understanding

What is displayed when this code runs?

AP pseudocode
n ← 1
REPEAT UNTIL(n > 5)
{
    IF(n MOD 2 = 1)
    {
        DISPLAY(n)
    }
    n ← n + 1
}

A loop inside a loop

The body of a loop can hold another loop. For every one pass of the outer loop, the inner loop runs all of its passes.

AP pseudocode
REPEAT 2 TIMES
{
    REPEAT 3 TIMES
    {
        DISPLAY("*")
    }
    DISPLAY("-")
}

The outer loop runs 2 times. Each time it shows three stars (the inner loop) and then a dash. So the output is * * * - * * * -. The star is displayed 2 × 3 = 6 times. The dash is displayed 2 times.

Runs in your browser

(end=" " tells Python to put a space after the value instead of starting a new line, so it looks like DISPLAY.)

Off-by-one errors

An off-by-one error is a loop that runs one pass too many or one too few. It is the most common loop bug, and the exam builds questions on it. Look at these two loops. They are the same except for the condition:

AP pseudocode
n ← 1
REPEAT UNTIL(n = 5)
{
    DISPLAY(n)
    n ← n + 1
}

This shows 1 2 3 4. When n becomes 5, the loop stops before showing it. It runs 4 passes. With REPEAT UNTIL(n > 5) it would show 1 2 3 4 5, which is 5 passes.

The order of the lines matters too. Both of these stop when n > 3:

DISPLAY firstAdd first
DISPLAY(n) then n ← n + 1n ← n + 1 then DISPLAY(n)
shows 1 2 3 (from n ← 1)shows 2 3 4 (from n ← 1)

To count passes: from a first value to a last value, both included, there are last − first + 1 passes. From 1 to 5 there are 5 passes, from 3 to 7 there are 5 passes, not 4. Python's range(1, 5) gives only 4 numbers (it leaves out the 5), so range(1, 6) is the one for 1 to 5.

Check your understanding

What is displayed when this code runs?

AP pseudocode
n ← 1
REPEAT UNTIL(n = 4)
{
    DISPLAY(n)
    n ← n + 1
}

Worked example: a mixed trace

What is displayed?

AP pseudocode
a ← 20
steps ← 0
REPEAT UNTIL(a ≤ 3)
{
    a ← a - 6
    steps ← steps + 1
}
DISPLAY(steps)
DISPLAY(a)
CheckaStop? (a ≤ 3)a aftersteps after
120false141
214false82
38false23
42true

The loop ran 3 passes. It displays 3 2. Notice that a is 2, below the stopping value 3, because the check only happens before a pass.

Common mistakes

  • Not writing the table. Doing it in your head is how values get mixed up. Write it down.
  • Tracing the last check as a pass. The check that says "stop" does not run the body.
  • Reading DISPLAY as if it ran once. If it is inside the loop, it shows a value on every pass.
  • Forgetting the starting values. The table starts with the lines before the loop.
  • Mixing up the inner and outer loops. The inner loop runs fully each time the outer loop runs once.

On the exam

Questions give the code and ask for the output, for the final value of a variable, or for how many times a line runs. A good answer comes from a table, not from a feeling. If you have a few seconds left, trace again with the smallest case, which is often 0 or 1 passes, to check the edges.

Key words

Tracing: running code by hand and recording each variable's value.

Trace table: the table of those values, one row per pass.

Nested loop: a loop inside the body of another loop.

Off-by-one error: a loop that runs one pass too many or too few.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.