DoAssignment study guide

C2.3 · Predict and modify the outcome of code

Learn to predict and modify the outcome of code through clear examples and targeted practice.

Ontario Grade 9 Mathematics

Algebra

Ontario MTH1W — Strand C: Coding, Expectation C2.3

Have you ever wondered what a computer program is actually doing when it runs? Every program — whether it controls a video game, calculates a school mark, or runs a streaming app — is a set of instructions that a computer follows in order. In this lesson you will learn to read short blocks of code, predict what they will produce, and make targeted changes so the code produces a different result. You do not need to write full programs from scratch. Instead, you will act like a detective: read the code carefully, track what each instruction does to the data, and reason about the outcome. This skill is called tracing code, and it is one of the most important habits a programmer — or anyone who works with technology — can develop.

What you will learn

  • Trace through a short block of code step by step and predict what it will output.
  • Explain how changing a variable's starting value or an operator changes the output.
  • Identify and fix a specific line of code so that the program produces a desired result.
  • Recognise how a loop repeats a set of instructions and predict the final value of a variable after the loop ends.

What Is Code and How Does It Run?

Code is a list of instructions written in a programming language. The computer reads those instructions from top to bottom, one line at a time, and carries them out in that exact order. This order is called sequential execution.
A variable is a named storage location that holds a value. Think of a variable as a labelled box. When you write x = 5, you are telling the computer to create a box called x and place the number 5 inside it. Later instructions can look inside the box, change what is stored there, or use the value in a calculation.
An operator is a symbol that tells the computer to perform an action. You already know arithmetic operators from Grade 8: + means add, - means subtract, * means multiply, and / means divide. In code, these work exactly the same way. The = sign in code means assign — put this value into this variable — not 'equals' in the equation sense you use in algebra.
An output statement tells the computer to display a value. In the pseudocode used in this lesson, print(...) means 'show this value on the screen.' Pseudocode is a way of writing code in plain English-like steps so you can focus on the logic without worrying about the exact rules of one specific programming language.
  • Code runs line by line, top to bottom.
  • A variable stores a value that can be read or changed.
  • = in code means assign a value, not 'is equal to' in the algebra sense.
  • print(...) displays a value as output.
  • Arithmetic operators +, -, *, / work the same as in Grade 8 math.

Tracing Code: Predicting the Output

Tracing means reading each line of code and keeping a running record of what value every variable holds at that moment. You do this by hand, updating the value of each variable each time the code changes it. The final value that gets printed is your predicted output.
Here is a concrete situation before any code. Imagine a store starts the day with 20 items in stock. During the morning, 8 items are sold. Then 15 new items arrive. How many items are in stock at the end? You work through it step by step: start with 20, subtract 8 to get 12, then add 15 to get 27. That step-by-step thinking is exactly what tracing code means.
Now look at the same situation written as pseudocode:

stock = 20
stock = stock - 8
stock = stock + 15
print(stock)

Line 1 puts 20 into stock. Line 2 reads the current value of stock (which is 20), subtracts 8, and stores the result (12) back into stock. Line 3 reads stock (now 12), adds 15, and stores 27 back into stock. Line 4 prints 27. Notice that each assignment line replaces the old value with a new one.
A trace table is a helpful tool. You make one column for each variable and one row for each line of code. After reading a line, you update the column for any variable that changed. The last row shows the final state of all variables, and you can read off the output directly.
  • Tracing means tracking the value of every variable, line by line.
  • Each assignment replaces the variable's old value with a new one.
  • A trace table organises this tracking in rows and columns.
  • The printed value is whatever the variable holds when print() is reached.

How Loops Repeat Instructions

A loop is a section of code that repeats a group of instructions a set number of times. Instead of writing the same line five times, you write it once inside a loop and tell the computer how many times to repeat it.
A counting loop uses a counter variable that starts at a value, increases by 1 each time the loop runs, and stops when it reaches a limit. Here is the structure in pseudocode:

for i from 1 to 4:
total = total + 3

This means: run the indented instruction for i = 1, then i = 2, then i = 3, then i = 4 — four times in total. The indented line is called the body of the loop.
To trace a loop, you treat each pass through the body as a separate row in your trace table. For example, if total starts at 0 and the loop adds 3 each time it runs four times, after the loop total holds 0+3+3+3+3=120 + 3 + 3 + 3 + 3 = 12. You can also express this with multiplication: 4×3=124 \times 3 = 12. Both give the same answer — the multiplication is just a quick way to check your trace.
The number of times a loop runs is called the iteration count. Changing the start value, the end value, or the amount added each time will change the final output. This is key to modifying code, which you will practise in the worked example.
final total = starting value + (iterations × amount added per iteration)
  • A loop repeats a block of instructions a fixed number of times.
  • The counter variable tracks which pass (iteration) is currently running.
  • Trace a loop by updating variables once per iteration.
  • Changing the loop's start, end, or body changes the final output.

Modifying Code to Get a Target Output

Predicting output is only half the skill. The other half is working backwards: given a target output, which line of code must change, and how?
The process has three steps. First, trace the original code to find what it currently outputs. Second, compare that output to the target. Third, identify which variable or operator, if changed, would close the gap between the current output and the target.
For example, suppose a piece of code calculates a total charge like this:

base = 40
tax = base * 0.10
total = base + tax
print(total)

Tracing gives: tax = 40×0.10=440 \times 0.10 = 4, and total = 40+4=4440 + 4 = 44. If the target output is 48, you need total to be 4 more. One way: change base to 44 — but let's check: tax = 44×0.10=4.444 \times 0.10 = 4.4, total = 44+4.4=48.444 + 4.4 = 48.4. That overshoots. Another way: change line 3 to total = base + tax + 4 — this directly adds 4 and gives total = 40+4+4=4840 + 4 + 4 = 48. That works. There is often more than one valid modification; what matters is that your change is deliberate and you can explain it.
Always re-trace the modified code from the top to confirm it produces the target output before accepting your answer. This re-tracing step catches errors that seem logical but have a small arithmetic slip.
target output=current output+required adjustment\text{target output} = \text{current output} + \text{required adjustment}
  • To modify code, first trace the original to find current output.
  • Compare current output to the target and find the difference.
  • Change one variable value or one operator deliberately to close the gap.
  • Re-trace the modified code to confirm it reaches the target output.

Conditional Statements: Code That Makes Decisions

A conditional statement tells the computer to run a block of code only if a certain condition is true. The most common form is if / else. If the condition is true, the first block runs; otherwise (else), the second block runs.
Here is an example:

score = 72
if score >= 50:
result = 'pass'
else:
result = 'fail'
print(result)

The condition is score >= 50. Since 72 is greater than or equal to 50, the condition is true, so result is set to 'pass', and 'pass' is printed.
When you trace code with a conditional, you must evaluate the condition first. Ask: is the condition true or false right now? Then follow only the branch that matches. The other branch is skipped entirely.
To modify a conditional so it produces a different output, you can change the value stored in the variable being tested, change the threshold in the condition, or change what gets assigned inside one of the branches. Each change has a different effect, so trace carefully after each modification.
  • A conditional runs a block of code only when its condition is true.
  • Trace a conditional by evaluating the condition first, then follow the matching branch.
  • The branch that does not match is skipped — its lines do not run.
  • Modifying the variable, the threshold, or the branch body each changes the output differently.

Trace Table for the Original Loop (points = points + 4, 5 iterations)

Iteration (i)CalculationValue of points after this iteration
Start (before loop)—0
10+40 + 44
24+44 + 48
38+48 + 412
412+412 + 416
516+416 + 420

Worked example

Trace, Predict, and Modify a Loop Program

Read the pseudocode below. (a) Predict what value is printed. (b) Change exactly one line so that the printed value is 30 instead.

points = 0
for i from 1 to 5:
points = points + 4
print(points)
  1. Step 1 — Identify the variables and their starting values
    Before the loop begins, points is assigned the value 0. The counter i will take the values 1, 2, 3, 4, 5 as the loop runs. We are not told to print i, so we only need to track points.
    points=0 (before loop starts)\text{points} = 0 \text{ (before loop starts)}
  2. Step 2 — Trace each iteration of the loop
    The loop body is 'points = points + 4'. This runs once for each value of i from 1 to 5, so it runs 5 times. Each time, 4 is added to whatever points currently holds.
    After iteration 1: 0+4=4\text{After iteration 1: } 0 + 4 = 4
  3. Step 3 — Continue the trace for all iterations
    We keep adding 4 each time the loop body runs.
    4+4=8    (i=2),8+4=12    (i=3),12+4=16    (i=4),16+4=20    (i=5)4 + 4 = 8 \;\; (i=2), \quad 8 + 4 = 12 \;\; (i=3), \quad 12 + 4 = 16 \;\; (i=4), \quad 16 + 4 = 20 \;\; (i=5)
  4. Step 4 — State the predicted output (Part a)
    After the loop finishes, points holds 20. The next line prints points, so the predicted output is 20. We can verify with the formula: starting value + (iterations × amount added) = 0 + (5 × 4) = 20. ✓
    0+(5×4)=200 + (5 \times 4) = 20
  5. Step 5 — Find the required adjustment (Part b)
    The target output is 30. The current output is 20. The difference is 30 − 20 = 10. We need the loop to add 10 more in total across its 5 iterations. Since the loop runs 5 times, each iteration must add 2 more than it currently does: 4 + 2 = 6.
    30−20=10,10÷5=2,4+2=630 - 20 = 10, \quad 10 \div 5 = 2, \quad 4 + 2 = 6
  6. Step 6 — Write the modified line
    Change the loop body from 'points = points + 4' to 'points = points + 6'. Everything else stays the same.
    points=points+6\text{points} = \text{points} + 6
  7. Step 7 — Re-trace the modified code to confirm
    We trace the modified loop: the body adds 6 each time, and it runs 5 times. Using the formula: 0 + (5 × 6) = 30. The printed value is now 30. ✓
    0+(5×6)=300 + (5 \times 6) = 30
Answer: (a) The original code prints 20. (b) Change 'points = points + 4' to 'points = points + 6'. The modified code prints 30.
Check: Re-trace from the top: points starts at 0. Loop runs 5 times, adding 6 each time: 0 → 6 → 12 → 18 → 24 → 30. print(points) outputs 30. ✓

Common mistakes and how to avoid them

Reading = in code as a mathematical equation. Students write 'x = x + 1 is impossible because a number can't equal itself plus one.'
Correction: In code, = means assign. 'x = x + 1' means: take the current value of x, add 1, and store that new value back into x. It is an instruction, not a statement of equality.
Forgetting to update the variable after each loop iteration. Students use the starting value for every iteration instead of the updated value.
Correction: After each iteration, the variable holds its new value. Always use the most recently updated value when tracing the next iteration. A trace table helps prevent this error.
Counting the number of loop iterations incorrectly. For example, thinking 'for i from 1 to 5' runs 4 times instead of 5.
Correction: Count the values i actually takes: 1, 2, 3, 4, 5 — that is 5 values, so 5 iterations. When in doubt, list every value of i explicitly to count them.
In a conditional, running both branches instead of only the one that matches the condition.
Correction: Only one branch ever runs for a given condition. Evaluate the condition first — true or false — then follow only the matching branch and skip the other entirely.
Changing a line of code to reach a target output without re-tracing the full program to verify.
Correction: Always re-trace the entire program after making a change. An adjustment that seems correct by reasoning alone can still contain an arithmetic slip that the re-trace will catch.

Lesson summary

  • Code runs line by line from top to bottom; each instruction is carried out in order.
  • A variable stores a single value at a time; an assignment line (=) replaces the old value with a new one.
  • Tracing code means tracking the value of every variable after each line runs; a trace table organises this work.
  • A loop repeats its body a fixed number of times; the final variable value equals the starting value plus the number of iterations multiplied by the amount changed each iteration.
  • A conditional (if / else) evaluates a condition and runs only the matching branch; the other branch is skipped.
  • To modify code for a target output: trace the original, find the gap, change one element deliberately, then re-trace to confirm.

Check your understanding

Question 1

What does the following code print?

n = 10
n = n - 3
n = n * 2
print(n)
  1. 7
  2. 14
  3. 17
  4. 20
Show answer and explanation
14
Line 1: n = 10. Line 2: n = 10 − 3 = 7. Line 3: n = 7 × 2 = 14. Line 4 prints 14.

Question 2

A loop runs 4 times and adds 5 to a variable called total each time. total starts at 2. What is printed after the loop?
  1. 20
  2. 22
  3. 25
  4. 18
Show answer and explanation
22
Using the formula: final total = starting value + (iterations × amount added) = 2 + (4 × 5) = 2 + 20 = 22.

Question 3

Study this code:

score = 45
if score >= 50:
grade = 'pass'
else:
grade = 'fail'
print(grade)

A student wants the code to print 'pass' without changing the condition or the branch bodies. Which change achieves this?
  1. Change score = 45 to score = 50
  2. Change 'pass' to 'fail' inside the if branch
  3. Change 'fail' to 'pass' inside the else branch
  4. Add a second print statement after the loop
Show answer and explanation
Change score = 45 to score = 50
The condition is score >= 50. Setting score = 50 makes the condition true (50 >= 50), so the if branch runs and grade is set to 'pass'. The other options either don't change the branch that runs or alter the branch bodies, which the question forbids.

Question 4

A program currently prints 18. A student changes one line so that a loop runs 6 times instead of 3 times, but keeps the loop body the same (adding 3 each iteration, starting from 0). What does the modified program now print?
  1. 18
  2. 21
  3. 36
  4. 9
Show answer and explanation
36
Original: 0 + (3 × 3) = 9 — but we are told the original prints 18, which means starting value may differ. Re-reading: start = 0, body adds 3, 6 iterations → 0 + (6 × 3) = 18. Wait — let's use the question's own numbers consistently. With 6 iterations and +3 each time from 0: 0 + (6 × 3) = 18. With 3 iterations and +3 each time from 0: 0 + (3 × 3) = 9. The question states the program currently prints 18 with 3 iterations, so the amount added must be 6 per iteration (0 + 3 × 6 = 18). Changing to 6 iterations: 0 + (6 × 6) = 36.

Key terms

Variable
A named storage location in a program that holds one value at a time. The value can be read or replaced by the program.
Assignment
An instruction that stores a value into a variable. Written with = in code; it means 'put this value here,' not 'is equal to.'
Sequential execution
The way a computer runs code: one line at a time, from top to bottom, in the written order.
Operator
A symbol that tells the computer to perform an action, such as + (add), - (subtract), * (multiply), or / (divide).
Tracing
The process of reading each line of code by hand and recording the current value of every variable to predict the program's output.
Trace table
A hand-drawn table used when tracing code. Each column represents one variable; each row represents the state of all variables after one line of code runs.
Loop
A section of code that repeats a block of instructions a set number of times. A counting loop uses a counter variable to track how many times the block has run.
Conditional statement
An instruction that checks whether a condition is true or false and runs one block of code if true and a different block if false. Written as if / else in pseudocode.

Continue through MTH1W

View the complete MTH1W Ontario Grade 9 Mathematics curriculum and lessons

Use the free MTH1W test and exam review guide

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 9 Mathematics (MTH1W), expectation C2.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