Ontario Grade 9 Mathematics · MTH1W C2.3
C2.3 · Predict and modify the outcome of code
Have you ever wondered what a computer program is actually doing when it runs? Every program — whether it controls a video game, calculates a…
Ontario Grade 9 Mathematics
Algebra
Ontario MTH1W — Strand C: Coding, Expectation C2.3
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 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
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.
- 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
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.
- 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
base = 40
tax = base * 0.10
total = base + tax
print(total)
Tracing gives: tax = , and total = . If the target output is 48, you need total to be 4 more. One way: change base to 44 — but let's check: tax = , total = . That overshoots. Another way: change line 3 to total = base + tax + 4 — this directly adds 4 and gives total = . That works. There is often more than one valid modification; what matters is that your change is deliberate and you can explain it.
- 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
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.
- 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) | Calculation | Value of points after this iteration |
|---|---|---|
| Start (before loop) | — | 0 |
| 1 | 4 | |
| 2 | 8 | |
| 3 | 12 | |
| 4 | 16 | |
| 5 | 20 |
Worked example
Trace, Predict, and Modify a Loop Program
points = 0
for i from 1 to 5:
points = points + 4
print(points)
- Step 1 — Identify the variables and their starting valuesBefore 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.
- Step 2 — Trace each iteration of the loopThe 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.
- Step 3 — Continue the trace for all iterationsWe keep adding 4 each time the loop body runs.
- 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. ✓
- 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.
- Step 6 — Write the modified lineChange the loop body from 'points = points + 4' to 'points = points + 6'. Everything else stays the same.
- Step 7 — Re-trace the modified code to confirmWe 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. ✓
Common mistakes and how to avoid them
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
n = 10
n = n - 3
n = n * 2
print(n)
- 7
- 14
- 17
- 20
Show answer and explanation
Question 2
- 20
- 22
- 25
- 18
Show answer and explanation
Question 3
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?
- Change score = 45 to score = 50
- Change 'pass' to 'fail' inside the if branch
- Change 'fail' to 'pass' inside the else branch
- Add a second print statement after the loop
Show answer and explanation
Question 4
- 18
- 21
- 36
- 9
Show answer and explanation
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
- C2.2 · Create code to represent relationships and solve problems
- C3.1 · Compare rates of change in linear and non-linear graphs
- C1.1 · Explore the development and use of an algebraic concept
- C1.2 · Create expressions that generalize relationships
- C1.3 · Compare expressions and identify equivalent forms
- C1.4 · Simplify algebraic expressions
Tutor support for C2.3 · Predict and modify the outcome of code
Working on C2.3 · Predict and modify the outcome of code in MTH1W? 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 math 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.
برای مبحث C2.3 · پیشبینی و تغییر خروجی کد در درس MTH1W، سؤال و تلاش خود را همراه بیاورید. در جلسه میتوانید یک ورودی کوچک را گامبهگام دنبال کنید، خروجی مورد انتظار و واقعی را مقایسه کنید و یک حالت مرزی را بیازمایید. راهنمای معلم ریاضی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقهای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی میشود.
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.