DoAssignment study guide

B3.3 · Trace a program to find an error

Learn to trace a program to find an error through clear examples and targeted practice.

Ontario Grade 10 Computer Science

Introduction to Programming

ICS2O study topic B3.3: follow instructions, record what happens, and compare it with the intended result

A program can produce a result that does not match what it was meant to do. Tracing helps you find where the difference begins. Choose specific input, follow each written instruction in order, record important values, and compare the result with the program’s purpose. Input is information a program receives; process is the work its instructions perform; output is information it displays or produces. The examples use Python 3. Classroom languages can differ. Code is shown as text for study and has not been executed.

What you will learn

  • Explain tracing as following a program step by step for stated input.
  • Identify a program’s input, process, and output.
  • Record values and decisions to locate an error.
  • Trace a proposed correction with the same input.

State the purpose and identify input, process, and output

Before tracing, describe what the program is supposed to do in plain language. Identify its input, process, and output. Then choose the input to trace and state the expected output. This gives you a clear result to compare with the behaviour of the written code.
Suppose a program should add two item prices. The prices are its input, addition is its process, and the displayed total is its output. For prices 5 and 2, the expected total is 7. If following the written instructions leads to 2, the traced result does not match the program’s purpose.
Keep the intended purpose separate from the code. Do not assume that a line does what its author meant. First find what the written instructions actually do for the selected input.
  • State the intended behaviour before following the code.
  • Use the input given in the problem or scenario.
  • Compare the traced output with the expected output.

Follow instructions and record changes

Read the program from top to bottom, following each instruction in order. When an instruction gives a value to a name, record that value. If a later instruction changes the value, record the new value as well. A trace table can make this record easier to follow; include only the values and decisions needed for the trace.
A decision asks a true-or-false question. Substitute the exact input into the condition and follow the branch that matches the answer. Pay particular attention to boundary values. If a stated rule includes age 12, trace age 12 exactly rather than guessing whether the condition includes it.
For a loop, check its condition, follow the instructions inside it when the condition is true, record any changes, and check the condition again. Continue until it is false. Include that final check: it tells you whether the loop repeats or stops.
  • Record values when they are assigned or changed.
  • Evaluate each decision using the exact input.
  • For a loop, include the update and the condition check that ends it.

Use the trace as evidence

When you find a mismatch, identify the instruction that causes it and use the trace as evidence. Saying only “the answer is wrong” does not show where the problem occurs. For example, you could explain that the total is assigned only the second price, so the program displays 2 rather than the expected 7.
After locating the error, suggest a small change that matches the stated purpose. Trace the changed code separately with the same input. This checks the correction for that example. One trace shows what happens for the input you used; it does not establish what happens for every possible input.
Keep the trace of the original code distinct from the trace of the correction. First describe what the code as written does. Then explain the change and follow the changed instruction.
  • Name the instruction responsible for the mismatch.
  • Support your explanation with a recorded value or decision.
  • Trace a proposed correction separately from the original code.

A repeatable tracing routine

Use a consistent routine: state the program’s purpose; identify its input and expected output; follow the written instructions in order; record values and decisions; compare the traced result with the expected result; and, if needed, identify and check a correction.
Do not silently fix a line while tracing the original program. Doing so can make you describe what you think the code should do instead of finding the error in what it says. A careful trace is a record of the program’s behaviour for a particular input.
For practice, take a short program from class, write down its stated purpose, and trace one specific input. Before suggesting a correction, point to the value or decision in your trace that first differs from what the purpose requires.
  • Trace the original code before changing it.
  • Keep the expected output distinct from the traced output.
  • Explain a mismatch using evidence from the trace.

Trace of the reminder loop as written

Counter at conditionCondition counter <= 4Reminder displayedCounter after increase
1TrueYes2
2TrueYes3
3TrueYes4
4TrueYes5
5FalseNo; loop stops—

Worked example

A total leaves out the first price

A snack program should add two prices. With inputs 5 and 2, it should display 7. Trace the code, find the error, and check a correction.
Trace the price total
Input prices: 5 and 2first_price = 5second_price = 2total = second_price; di

Follow the input values through the assignments to the displayed total.

Python 3

first_price = 5
second_price = 2
total = second_price
print(total)
  1. Set the purpose
    The inputs are 5 and 2. The process should add both prices, and the expected output is 7. The final instruction displays the value named total.
  2. Follow the assignments
    The first assignment gives first_price the value 5, and the second gives second_price the value 2. The next assignment copies second_price into total. It does not add the first price, so total becomes 2 and that is what the program displays.
  3. Locate and check a correction
    The assignment to total is the error because it uses only the second price. A correction is to assign the sum of first_price and second_price to total. Following that changed instruction on paper gives 7 for these inputs; this is a trace, not a claim that the code was run.
Answer: The original code displays 2 instead of the expected 7. The error is the assignment that copies only second_price. A correction is total = first_price + second_price.
Check: The corrected assignment uses both input values, giving a traced total of 7.

Worked example

A decision misses the boundary age

A practice program should display “Child entry” for ages 12 and under, and “Other entry” for ages above 12. Trace the code for age 12 and find the error.
Trace the age decision
YesNoInput age: 12age < 12?Display Child entryDisplay Other entry

At age 12, the written condition is false. Yes and No show the two possible branches.

Python 3

age = 12
if age < 12:
    print("Child entry")
else:
    print("Other entry")
  1. Identify the expected output
    The input is age 12. The stated rule includes age 12 among ages 12 and under, so the expected output is “Child entry.”
  2. Evaluate the written condition
    The code asks whether age is less than 12. At age 12, that condition is false, so the program follows the else branch and displays “Other entry.”
  3. Check a correction
    The condition leaves out the boundary age. Changing it to age <= 12 includes 12. For the same input, the condition is true and the program follows the child-entry branch.
Answer: For age 12, the original code displays “Other entry,” but the expected output is “Child entry.” The condition should include equality: age <= 12.
Check: The original test excludes 12 because 12 is not less than 12. The changed test includes it.

Worked example

A reminder loop repeats too many times

A reminder program should display “Check your notes” three times. Trace the code, locate the error, and check a correction.
Trace the reminder loop
YesRepeatNoSet counter to 1counter <= 4?Display reminderIncrease counter by 1End

The Yes path displays the reminder and updates the counter before returning to the decision. No ends the loop.

Python 3

counter = 1
while counter <= 4:
    print("Check your notes")
    counter = counter + 1
  1. Trace each repetition
    The counter starts at 1. The condition counter <= 4 is true at 1, 2, 3, and 4. At each of those values, the program displays the reminder and increases the counter by 1. The message is therefore displayed four times.
  2. Check why the loop stops
    After the display at counter 4, the counter increases to 5. The condition is then false, so the loop stops. The extra display happens because the condition allowed the counter value 4.
  3. Check a correction
    Changing the condition to counter < 4 allows displays at counter values 1, 2, and 3. After the counter increases to 4, the condition is false, so the loop ends without a fourth display.
Answer: The original loop displays the reminder four times rather than three. A correction is to use counter < 4 as the condition.
Check: With the changed condition, the message displays at 1, 2, and 3; after the increase to 4, the loop stops.

Common mistakes and how to avoid them

Tracing what you think the code should say instead of what it actually says.
Correction: Follow the written instructions in order first. Suggest a correction only after locating the mismatch.
Skipping a value change when a name is assigned a new value.
Correction: Record important values after assignments so you can see what later instructions use.
Assuming a boundary value is included without testing it.
Correction: Substitute the exact boundary input into the condition and decide whether it is true or false.
Stopping a loop trace as soon as the expected number of displays has occurred.
Correction: Record the update and check the condition again to see whether the loop repeats or stops.

Lesson summary

  • Tracing means following code step by step with specific input.
  • Identify input, process, and output before comparing behaviour with purpose.
  • Record changing values and evaluate decisions as written.
  • Trace each loop repetition, including the final condition check.
  • Use a traced value or decision as evidence when explaining an error.

Check your understanding

Question 1

A program should display “Adult” for ages 18 and older. It checks whether age > 18. What happens for age 18?
  1. It displays “Adult” because the condition includes 18.
  2. The condition is false, so it follows the other branch; the test excludes 18.
  3. It displays both messages.
  4. The code cannot be traced without running it.
Show answer and explanation
The condition is false, so it follows the other branch; the test excludes 18.
At age 18, age > 18 is false. The written condition excludes the boundary age.

Question 2

A loop starts at counter 1, displays a message while counter <= 3, and increases the counter by 1 after each display. How many times does it display the message?
  1. Two times
  2. Three times
  3. Four times
  4. It does not stop.
Show answer and explanation
Three times
The condition is true at counter values 1, 2, and 3. After the increase to 4, it is false.

Question 3

After stating a program’s purpose, what is a useful next step in a trace?
  1. Change the line that looks unusual.
  2. Use the stated input and follow the instructions in order.
  3. Assume the output and work backwards.
  4. Skip value changes and inspect only the final line.
Show answer and explanation
Use the stated input and follow the instructions in order.
Following the instructions in order with stated input provides evidence of the program’s behaviour.

Key terms

Trace
Follow a program’s instructions step by step with specific input and record what happens.
Input
Information a program receives.
Process
The work carried out by a program’s instructions.
Output
Information a program displays or produces.
Condition
A true-or-false question in a program that affects which instruction follows.
Trace table
A table for recording important values and decisions while following a program.

Continue through ICS2O

View the complete ICS2O Ontario Grade 10 Computer Science curriculum and lessons

Tutor support for B3.3 · Trace a program to find an error

Working on B3.3 · Trace a program to find an error in ICS2O? Bring the question and your own attempt. In a live session, you can trace a small input step by step, compare expected and actual output, and test an edge case. Ask about the first step that does not make sense, then try a similar question yourself.

Explore coding tutoring · Request an eligible free 30-minute introduction. Your request is matched by subject and language; a tutor must accept before a time is confirmed.

برای مبحث B3.3 · ردیابی برنامه برای یافتن خطا در درس ICS2O، سؤال و تلاش خود را همراه بیاورید. در جلسه می‌توانید یک ورودی کوچک را گام‌به‌گام دنبال کنید، خروجی مورد انتظار و واقعی را مقایسه کنید و یک حالت مرزی را بیازمایید. راهنمای معلم برنامه‌نویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقه‌ای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی می‌شود.

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 10 Computer Science (ICS2O), expectation B3.3. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question