Intermediate
40 mins
Teacher/Student led
+80 XP

Trace, Predict and Fix

Learn how to use trace tables to step through programs by hand, predicting what they will output before running them. Then trace a buggy program to locate the error and fix it so it produces the correct result.

Teacher Class Feed

Load previous activity

    1 - Start ~4 mins

    Illustration for StartLast time you wrote a plan as pseudo-code and a flow chart, then built it. Today you learn a new habit: step through a program by hand before you run it. You will fill a trace table, predict the output, then check. When the answer is wrong, you will find the bug and fix it.

    2 - Trace Tables and Predict-then-run ~5 mins

    Predict, then run

    A trace table is a simple grid. Each row is one instruction. You write the value of every variable after that instruction runs. By the last row, you know what the program will show, without running it yet.

    Worked example. Read this short program:

    1. Set score to 0
    2. Change score by 10
    3. Change score by 5
    4. Say score

    Filled trace table (columns: Step, Instruction, score):

    StepInstructionscore
    1Set score to 00
    2Change score by 1010
    3Change score by 515
    4Say score15 (says 15)

    So your prediction is 15. Only then do you run the program and compare. If the run matches, you understood it. If it does not, the mismatch tells you where to look.

    3 - Hands-on: Trace, Predict, Then Check ~11 mins

    Practise the predict-then-run habit on two short programs. For each one: draw a blank trace table with columns for Step, Instruction, each variable, and Notes, using the grid from the example as your model. Fill it by hand, write your prediction, then check.

    Program A (score): whole class first

    1. Set points to 3
    2. Change points by 4
    3. Change points by −2
    4. Say points

    Trace points after every step. What will it say?

    Check for Program A: your teacher runs it on the board. Tick a match, or circle the row that went wrong and fix that row. If you miss the board run, ask for the expected output and still tick or correct your table. Final output after check: _______

    Before Program B: a filled mini if-row

    Sometimes a step is a decision. The if-row still gets a line in the table, and you write which branch runs in the Notes column. Here is a short filled example to re-read if you need it:

    1. Set lives to 1
    2. If lives = 0, say "Game over", else say lives
    StepInstructionlivesNotes
    1Set lives to 11
    2If lives = 0 …1Condition false, so else runs; says 1

    Program B (lives and a decision): pairs

    1. Set lives to 2
    2. Change lives by −1
    3. If lives = 0, say "Game over", else say lives

    Your table can start like this (finish the blank cells):

    StepInstructionlivesNotes
    1Set lives to 22
    2Change lives by −1
    3If lives = 0 …Which branch runs?

    Which branch of the if runs? What will it say?

    Check for Program B: your teacher runs it on the board. Tick a match, or circle the row that went wrong and fix that row. If you miss the board run, ask for the expected output and still tick or correct your table. Final output after check: _______

    Done looks like:

    • A completed trace table for Program A with a written prediction
    • A completed trace table for Program B with a written prediction and a note on which branch ran
    • Both checked against the teacher Scratch runs, with any wrong row corrected
    Exploration

      4 - Your Turn: Find the Planted Bug ~14 mins

      Here is an unfamiliar program. It is supposed to count coins collected in a short round and finish on 5. It does not. Your job is to trace it, predict what it actually does, find the bug, fix it, then rebuild the fixed version in Scratch and watch it run.

      Buggy coin program (supposed to end on 5):

      1. Set coins to 5
      2. Change coins by 3
      3. Change coins by −2
      4. Set coins to 1
      5. Say coins

      Work in this order:

      1. Fill a trace table for the buggy program as written. What does it actually say?
      2. Write one sentence: which step breaks the intended path to 5, and why?
      3. Fix that step (change only what you must) so a fresh trace ends on 5. Change only one instruction; keep the program four or five steps; your second trace must end on 5.
      4. In pairs, rebuild your fixed version in Scratch (four or five blocks) and run it. Confirm it says 5 on screen. Tick your table if the live run matches.

      Hint: the final value must be 5, not just "closer". If your new trace ends on another number, adjust the same step again.

      Use the debugging routine you already know: read the symptom (wrong final value), find the step, form a hypothesis, test it with a new trace, then confirm with a live run.

      Must-do:

      • A trace of the buggy version with the real final value
      • One sentence naming the broken step
      • A fixed version of that step
      • Fixed version rebuilt and run in Scratch, saying 5 on screen

      Stretch if you have time:

      • A second full trace table for your fixed version that ends on 5 (paper check before or after the Scratch run)
      • A one-line comment above the fixed step explaining the mistake for another coder

      5 - Make Sense ~3 mins

      A trace table turns "I think it does this" into a step-by-step record you can check. Predicting before you run is how you catch bugs early: the mismatch between your table and the real output points at the broken step. That is testing and managing errors on purpose, not by guessing.

      123learn · Online learning platform

      Unlock the full learning experience

      You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.

      Hundreds of curriculum-aligned lessons
      Interactive activities in every lesson
      Printable resources & progress tracking
      Copyright Notice
      This lesson is copyright of Coding Ireland 2017 - 2025. Unauthorised use, copying or distribution is not allowed.
      🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more