Study Guide

Trace Tables and Dry Runs

How to follow a program by hand, one line at a time, and record every change in a trace table: variables, outputs and user prompts, loops with selection inside them, working out what an algorithm is for, and finding the line that is wrong.

Lesson 5.7 Algorithms and Programming

For: Cambridge IGCSE Computer Science learners, or Key Stage 3 learners who have met loops and selection (about ages 13 to 14)

Stage: Think

A program with no computer

A fictional arcade gives each player 10 tickets. Every game costs 3 tickets, and the machine tells the player how many tickets are left after each game. Here is its program, printed on a sheet of paper. There is no computer in the room.

Tickets ← 10
Played ← 0
WHILE Tickets >= 3 DO
  Tickets ← Tickets - 3
  Played ← Played + 1
  OUTPUT Tickets
ENDWHILE
OUTPUT "Games: ", Played

Think it through

You have a pencil and the sheet, and you want to know exactly what this program would output. How would you find out? What would you write down as you went?

Show a model answer

Follow the program the way the computer does: one line at a time, in order, and write down the value of each variable every time it changes, together with anything that is output. The variables are what the computer would be holding in memory, so writing them down is the only way to keep track.

Stage: Predict

Writing the steps down

Think it through

Before tracing, predict: how many games does the arcade program count, and what is the last number of tickets it outputs inside the loop?

Show what happens

Three games are counted. The tickets go 10, then 7, 4 and 1, and the loop stops when Tickets is 1, because 1 is less than 3. The last number output inside the loop is 1.

Following a program by hand, line by line, is called a dry run. The table you write while you do it is a trace table: a column for each variable, a column for the output, and a row for each step or pass, so that you can see how the values change. Trace the arcade program. Each row is one pass of the loop; the test in the second column is made at the top of the pass, before anything changes.

Following the arcade program
RowTickets >= 3 ?TicketsPlayedOUTPUT
Before the loop-100
Pass 1
Pass 2
Pass 3
Pass 4

    Show the completed table
    RowTickets >= 3 ?TicketsPlayedOUTPUT
    Before the loop-100
    Pass 1Yes717
    Pass 2Yes424
    Pass 3Yes131
    Pass 4NoGames: 3

    Leave a cell empty when its variable does not change on that row.

    Check your understanding

    In the Pass 4 row, the Tickets and Played cells are empty. Why?
    Show the answer

    Answer: B. The loop body did not run on that row, so neither variable changed.

    A trace table records changes. The test that ends the loop is a row of its own, even though nothing inside the loop runs.

    Each new value is worked out from the row above, not from the value you expect later. On Pass 2, Tickets ← Tickets - 3 uses the 7 from Pass 1. It is tempting to jump straight to the final answer; the trace is only trustworthy if every row follows from the one before it.

    Do not confuse: Dry run and trace table

    What is it?Dry run
    The activity: following an algorithm by hand, line by line, with chosen values, without a computer.Working through the arcade program with a pencil.
    What is it?Trace table
    The record: the table that shows each variable, each output and each prompt as the dry run goes on.The table with Tickets, Played and OUTPUT columns.

    You do a dry run; you write a trace table while you do it. Many people use the two names for the same piece of work.

    Stage: Discover

    Prompts and typed values

    A fictional quiz app asks four questions. A correct answer, “A”, scores 2 points. The trap answer “X” takes away 1 point. Any other answer scores nothing. The program’s test for “X” is nested inside the ELSE of the test for “A”.

    Score ← 0
    FOR Question ← 1 TO 4
      OUTPUT "Answer to question ", Question
      INPUT Answer
      IF Answer = "A"
        THEN
          Score ← Score + 2
        ELSE
          IF Answer = "X"
            THEN
              Score ← Score - 1
          ENDIF
      ENDIF
    NEXT Question
    OUTPUT "Score: ", Score

    Think it through

    A player types A, B, X and A for the four questions. Predict the final score. Which of the four answers uses the inner IF?

    Show what happens

    The score is 3. The first A gives 2, B gives nothing, X takes 1 off, and the last A adds 2: 2 + 0 - 1 + 2 = 3. The inner IF is reached by B and X, the two answers that are not A.

    The trace table has to show what the user sees (the prompt, which is an OUTPUT line) and what the user types (an INPUT). Both are columns, so the table still shows every step.

    Following the quiz app: one row for each question
    RowOUTPUTAnswer typedScore
    Before the loop0
    Question 1Answer to question 1A
    Question 2Answer to question 2B
    Question 3Answer to question 3X
    Question 4Answer to question 4A
    After the loop

      Show the completed table
      RowOUTPUTAnswer typedScore
      Before the loop0
      Question 1Answer to question 1A2
      Question 2Answer to question 2B2
      Question 3Answer to question 3X1
      Question 4Answer to question 4A3
      After the loopScore: 3

      The prompt rows are given; each prompt is the OUTPUT line the loop runs before every INPUT.

      Check your understanding

      Where does the text “Answer to question 2” belong in a trace table?
      Show the answer

      Answer: A. In the OUTPUT column, on the row for question 2, because it is shown to the user.

      A trace table covers every variable, every output and every prompt, in the order they happen.

      Stage: Explain

      Tracing a loop with a choice inside it

      Think it through

      Look at the program in the worked example below before reading the trace. What do you think EvenTotal and BigEvens each count?

      Show what happens

      EvenTotal adds up the even numbers from 1 to 8. BigEvens counts the even numbers that are also bigger than 5.

      EvenTotal ← 0
      BigEvens ← 0
      FOR Number ← 1 TO 8
        IF Number MOD 2 = 0
          THEN
            EvenTotal ← EvenTotal + Number
            IF Number > 5
              THEN
                BigEvens ← BigEvens + 1
            ENDIF
        ENDIF
      NEXT Number
      OUTPUT "Even total: ", EvenTotal
      OUTPUT "Big evens: ", BigEvens
      1. Choose the columns. The variables that change: Number, EvenTotal and BigEvens. The outer test, Number MOD 2 = 0, and the inner test, Number > 5, are worth a column each, because each decides whether lines run.
      2. Work in order, one pass at a time. For Number = 1: the outer test is false (1 MOD 2 is 1), so everything inside is skipped, and the inner test is not even reached. EvenTotal and BigEvens stay at 0.
      3. Look at an even number. For Number = 2: the outer test is true (2 MOD 2 is 0), so EvenTotal becomes 0 + 2 = 2. The inner test, 2 > 5, is false, so BigEvens stays 0.
      4. Reach the inner IF’s true case. Number = 6: the outer test is true, EvenTotal becomes 6 + 6 = 12 (the 6 from adding 2 and 4 before it), and 6 > 5 is true, so BigEvens becomes 1.
      5. Finish. Number = 8 gives EvenTotal 20 and BigEvens 2. The program outputs Even total: 20 and Big evens: 2.

      The whole trace, one line for each Number:

      • 1: outer test false, inner test not reached. EvenTotal 0, BigEvens 0.
      • 2: outer true, inner false (2 is not above 5). EvenTotal 2, BigEvens 0.
      • 3: outer false. EvenTotal 2, BigEvens 0.
      • 4: outer true, inner false. EvenTotal 6, BigEvens 0.
      • 5: outer false. EvenTotal 6, BigEvens 0.
      • 6: outer true, inner true. EvenTotal 12, BigEvens 1.
      • 7: outer false. EvenTotal 12, BigEvens 1.
      • 8: outer true, inner true. EvenTotal 20, BigEvens 2.

      A greenhouse (fictional) records five midday temperatures. Days of 15 degrees or more are “mild or hot”, and of those, days of 25 or more are “hot”. Now do a trace yourself, with the two tests in columns.

      Hot ← 0
      Mild ← 0
      FOR Day ← 1 TO 5
        INPUT Temp
        IF Temp >= 15
          THEN
            IF Temp >= 25
              THEN
                Hot ← Hot + 1
              ELSE
                Mild ← Mild + 1
            ENDIF
        ENDIF
      NEXT Day
      OUTPUT "Hot days: ", Hot
      OUTPUT "Mild days: ", Mild
      Following the greenhouse program for the temperatures 12, 27, 18, 25 and 31
      DayTemp typedTemp >= 15 ?Temp >= 25 ?HotMild
      Before the loop00
      112
      227
      318
      425
      531

        Show the completed table
        DayTemp typedTemp >= 15 ?Temp >= 25 ?HotMild
        Before the loop00
        112NoNot reached00
        227YesYes10
        318YesNo11
        425YesYes21
        531YesYes31

        The program outputs Hot days: 3 and Mild days: 1.

        Check your understanding

        For the temperature 18, which statement about the program is true?
        Show the answer

        Answer: C. Both tests are made: the outer is true and the inner is false, so Mild goes up by 1.

        An inner IF is only reached when the outer one is true.

        Stage: Discover

        What is this algorithm for?

        Looking at the lines alone does not always tell you what a program is for. A trace shows what it does with real numbers, and the pattern in the answers gives the purpose away.

        INPUT Number
        Result ← 0
        FOR Step ← 1 TO Number
          Result ← Result + Number
        NEXT Step
        OUTPUT Result

        Think it through

        Without tracing yet, what do you think this program does with the number typed? Make a guess, then trace it for the number 3.

        Show a model answer

        Each pass adds Number to Result, and there are Number passes. For 3: 3 + 3 + 3 = 9. So it multiplies the number by itself, but the guess is only trustworthy once it is checked with more than one value.

        Following the program for the number 3
        StepResult
        Before the loop0
        1
        2
        3

          Show the completed table
          StepResult
          Before the loop0
          13
          26
          39

          The program outputs 9. For the number 4 the same trace ends at 16, and for 5 at 25.

          Check your understanding

          The program outputs 9 for 3, 16 for 4 and 25 for 5. What is the algorithm for?
          Show the answer

          Answer: D. It squares the number typed, by repeated addition.

          A trace with two or three well-chosen values usually shows the purpose of an algorithm. Say what it is for in one plain sentence, without describing the lines.

          Stage: Apply

          Finding the line that is wrong

          A fictional school intends this program to count the pupils who passed. A mark of 50 or more is a pass. It is tested with three marks, 62, 50 and 38, and the right answer is 2. It outputs 1.

          Passes ← 0
          FOR Pupil ← 1 TO 3
            INPUT Score
            IF Score > 50
              THEN
                Passes ← Passes + 1
            ENDIF
          NEXT Pupil
          OUTPUT "Passes: ", Passes
          Tracing the faulty program for the marks 62, 50 and 38
          PupilScore typedScore > 50 ?Passes
          Before the loop0
          162
          250
          338

            Show the completed table
            PupilScore typedScore > 50 ?Passes
            Before the loop0
            162Yes1
            250No1
            338No1

            The program outputs Passes: 1, but the expected answer is 2.

            Trace what the lines say, not what the programmer meant. A reader who already knows the program is meant to count 50 as a pass will be tempted to write “Yes” for the mark 50. The trace only finds the mistake if every test is worked out honestly from the line as it is written.

            Apply it

            Compare the trace with the expected answer. On which row do the program and the intention first disagree, and what is the fix?
            Show the answer

            Answer: B. Row 2: 50 should count as a pass, so the test should be Score >= 50.

            The row where the trace first goes wrong points to the line to fix. Boundary values are the best test data for comparisons, because that is where > and >= differ.

            Stage: Apply

            Write it, then trace it

            A fictional fitness app asks for the number of steps on each of 4 days. It outputs the total steps, and the number of days on which the goal of 8000 steps or more was reached.

            Think it through

            Write the pseudocode, including a prompt before each input. Then say which columns a trace table for it needs, and fill in the table for the days 9000, 7500, 8000 and 12000.

            Show a model answer
            Total ← 0
            GoalDays ← 0
            FOR Day ← 1 TO 4
              OUTPUT "Steps for day ", Day
              INPUT Steps
              Total ← Total + Steps
              IF Steps >= 8000
                THEN
                  GoalDays ← GoalDays + 1
              ENDIF
            NEXT Day
            OUTPUT "Total steps: ", Total
            OUTPUT "Goal days: ", GoalDays

            The columns are Day, OUTPUT, Steps, Total and GoalDays.

            RowOUTPUTStepsTotalGoalDays
            Before the loop00
            Day 1Steps for day 1900090001
            Day 2Steps for day 27500165001
            Day 3Steps for day 38000245002
            Day 4Steps for day 412000365003
            After the loopTotal steps: 36500
            After the loopGoal days: 3

            The 8000-step day counts because the test is >=. A FOR loop is used because there are exactly four days.

            Go deeper Letting the computer print the trace
            tickets = 10
            played = 0
            while tickets >= 3:
                tickets = tickets - 3
                played = played + 1
                print("Tickets", tickets, "Played", played)
            print("Games:", played)
            • Adding a print that shows the variables on each pass is a quick way to see what a program really does: it prints one row of a trace table per pass. This program prints Tickets 7 Played 1, Tickets 4 Played 2, Tickets 1 Played 3 and then Games: 3.
            • A dry run is still the skill to practise. In an exam there is no computer, and a hand trace also shows what the program ought to do before you run it.
            • A debugger is a tool that lets you pause a real program and look at the variables line by line, which is a trace table made by the computer.

            Stage: Consolidate

            Show what you know

            Think it through

            Explain to a friend why a trace table is useful even when you have a computer that can run the program. Give two different jobs it does.

            Show a model answer

            A good answer gives two jobs from: it shows what the program does at every step, so you can see where a variable goes wrong, not only that the final answer is wrong; it shows what an unfamiliar algorithm is for by running it on small values; it finds the line at fault by showing the first row that disagrees with what was intended; and it checks your design before you write any code.

            Apply it

            A program reads three marks, outputs the prompt “Mark?” before each, totals them and then outputs the average. Which set of columns gives a trace table that records everything the syllabus asks for?
            Show the answer

            Answer: A. Mark, Total and OUTPUT, with the prompt in the OUTPUT column.

            A trace table has a column for each variable that changes and for the output, with typed values and prompts included.

            • A dry run is following an algorithm by hand, with chosen data. A trace table is the record: a column for each variable that changes, one for the output (prompts included) and a row for each step.
            • Work from the row above. Write a value only when it changes, and leave the cell empty otherwise.
            • For a loop with an IF inside, show the test in a column. An inner IF is only reached when the outer one is true.
            • To find an algorithm’s purpose, trace it for two or three simple values and describe the pattern in the outputs.
            • To find an error, trace what the lines say. The first row that disagrees with the intention shows the line to fix; boundary values are the most revealing test data.

            Sources and scope

            • What this lesson is. The seventh lesson of the Algorithms and Programming module, written for this site. It covers point 7.7 of Cambridge IGCSE Computer Science 0478 (2026 to 2028, version 6), with the use of a trace to find an algorithm’s purpose (7.3) and an error (7.8). Its curriculum links are provisional and imply no review or endorsement by any examination board.
            • Original material. The arcade, the quiz app, the greenhouse, the school marks and the fitness app are fictional, written for this lesson. The pseudocode follows the syllabus’s pseudocode guide, and every trace and output was worked out by hand and checked by running an equivalent Python program.
            • Simplified on purpose. Programs are short, with small numbers. Choosing test data (normal, abnormal, extreme and boundary) is mentioned only for the boundary value; it is not taught in full here. Flowcharts are not traced.
            • Left to other lessons. Tracing string handling, subroutines, arrays and sorting is practised in Lessons 8 to 11; validation, verification and test data have lessons of their own.