N3/N4  ·  SDD  ·  Software Design & Development

Reading and Explaining Code

National 3 & National 4 Computing Science Lesson SDD6 of 10 Approx 55 min
Learning intentions
  • Read a short Python program and work out what it does, without running it
  • Trace variable values through a program line by line
  • Identify which constructs (sequence, selection, iteration) a program uses
Success criteria
  • I can predict the output of a program by tracing it
  • I can complete a trace table showing how a variable's value changes
  • I can explain, in plain English, what a piece of code does and why
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.

1. What does x = 5 followed by print(x) output?

2. If age = 12, what does if age >= 13: evaluate to?

3. True or false: you can work out what a program will do just by reading it, without running it.

Key vocabulary

Tracing
Following code by hand, line by line, to work out what it does without running it.
Trace table
A table used while tracing, with one column per variable, showing how each value changes.
Sequence
Instructions that run one after another, in the order they're written.
Selection
A construct (if / elif / else) that chooses which instructions run based on a condition.
Iteration
A construct (for / while) that repeats a block of instructions.
Predict
To state what a program's output will be before it is run.

Reading and Explaining Code

Why reading code matters

So far, most lessons have focused on writing code. But being able to read code — working out what a program does just by looking at it — is just as important a skill, and it's exactly what SQA assessment expects pupils to demonstrate. A program combines the three basic constructs covered so far: sequence (SDD1–SDD2), selection (SDD3), and iteration (SDD4–SDD5). Reading code well means recognising which of these is happening at each point, and following the values stored in variables as the program runs.

Tracing line by line

To trace a program, go through it one line at a time, exactly in the order Python would run it, and keep track of what changes. For sequence, this is simple — just follow the lines in order. For selection, work out whether the condition is true or false, and only follow the branch that matches. For iteration, the same lines get followed multiple times, so keep track of how many passes have happened and what has changed each time.

Trace tables

A trace table is a simple tool for tracing: draw a column for each variable in the program, then add a new row every time a variable's value changes, filling in the new value. This is especially useful for loops, where a variable might change many times before the program finishes.

Line executedcounttotal
count = 00
total = 000
total = total + count (pass 1)00
count = count + 1 (pass 1)10
total = total + count (pass 2)11

Explaining code in plain English

Explaining what code does is different from just describing each line. A good explanation says what the program achieves overall and why, not just a line-by-line translation. For example, rather than "it sets total to 0, then it loops and adds count to total," a stronger explanation says "this program adds up a series of numbers using a running total, built inside a loop."

Worked examples

Example 1 — tracing sequence and selection

Code: score = 7 then if score >= 5: /   print("Pass") / else: /   print("Fail")

1
score is set to 7.
2
The condition score >= 5 is checked: 7 >= 5 is True.
3
Because the condition is True, only the if branch runs: Pass is printed. The else branch is skipped entirely.
Example 2 — tracing a loop with a trace table

Code: total = 0 then for number in range(1, 4): /   total = total + number, then print(total)

Passnumbertotal
start0
111
223
336
1
The loop runs 3 times, since range(1, 4) produces 1, 2, 3.
2
total builds up: 0 → 1 → 3 → 6.
3
Final output: 6.
Example 3 — explaining purpose, not just lines

Code: attempts = 0 then password = input("Enter password: ") then while password != "abc123": /   attempts = attempts + 1 /   password = input("Try again: "), then print("Correct after", attempts, "attempts")

1
Weak explanation (avoid this): "It sets attempts to 0, asks for a password, loops while it's wrong, adds 1 to attempts, asks again, then prints attempts."
2
Strong explanation (aim for this): "This program repeatedly asks the user for a password until they enter the correct one, counting how many incorrect attempts were made along the way."
Now you try

Code: total = 0 then for n in range(2, 5): /   total = total + n, then if total > 8: /   print("High") / else: /   print("Low")

  1. Complete a trace table for n and total.
  2. What is printed at the end?

Trace: n takes 2, 3, 4. total goes 0 → 2 → 5 → 9.

Output: 9 > 8 is True, so "High" is printed.

Common mistakes
Skipping lines when tracing. Every line that runs must be followed in order — missing one line, especially inside a loop, gives the wrong final value.
Forgetting a loop repeats the same lines multiple times. Each pass needs to be traced separately in the trace table, not just once.
Explaining code line-by-line instead of explaining its purpose. A strong explanation says what the program is for, not just what each line says.
Assuming both branches of an if/else run. Only one branch ever runs per pass — tracing must follow the condition to know which one.
Tip

N4 pupils: when asked to explain code, always name the construct being used (sequence, selection, or iteration) as part of your answer — this shows the assessor you can identify constructs, not just describe behaviour.

Task Set

Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are practical tasks in IDLE.

1. x = 3 then y = x + 4 then print(y). What is printed? TYPE 1

2. mark = 4 then if mark >= 5: print("Pass") else: print("Fail"). What is printed? TYPE 1

3. total = 0 then for n in range(1, 4): total = total + n. What is total after the loop? TYPE 1

4. A program contains an if / elif / else chain and a for loop. Which constructs does it use? TYPE 1

5. count = 1 then while count < 4: count = count + 1 then print(count). What is printed? TYPE 1

6. Trace this code and state what is printed: x = 2 / y = 0 / for i in range(3): /   y = y + x / print(y). Show your trace table. TYPE 2 N3

i takes 0, 1, 2 (3 passes). y goes 0 → 2 → 4 → 6. The program prints 6.

7. Explain, in your own words and without describing every line, what this program does: total = 0 / count = 0 / number = input("Enter a number, or 'done' to finish: ") / while number != "done": /   total = total + int(number) /   count = count + 1 /   number = input("Enter a number, or 'done' to finish: ") / print(total / count) TYPE 2 N4

This program repeatedly asks the user to enter numbers, adding each one to a running total and counting how many were entered, until the user types "done." It then calculates and prints the average of all the numbers entered, by dividing the total by the count.

8. Practical: In IDLE, create a new file called sdd6_tracing.py. Without running it first, predict on paper what this program will print, then type it in, run it, and check your prediction: total = 0 / for i in range(1, 5): /   total = total + i / print(total). Record your prediction and the actual output. TYPE 3

Prediction: i takes 1, 2, 3, 4. total = 1+2+3+4 = 10. Actual output when run: 10 — matches the prediction.

9. Practical: In sdd6_tracing.py, add comments above each line of your program from Q8 explaining what that line does, then add one more comment at the very top explaining the program's overall purpose in one sentence. Paste your commented code below. TYPE 3

# This program adds up the numbers 1 to 4 using a running total
# set up the running total
total = 0
# repeat once for each number 1 to 4
for i in range(1, 5):
    # add the current number to the running total
    total = total + i
# display the final total
print(total)
Teacher notes — Shift+T to hide

Suggested timing: 55 minutes. Warm up 5 min; notes 15 min; examples 15 min; now you try 5 min; task set 15 min.

Key misconception to address: pupils narrating code line-by-line instead of explaining its purpose — model both a weak and strong explanation on the board, as in Worked Example 3.

Live demo suggestion: build a trace table together as a class on the board for Example 2 before pupils attempt Now You Try independently.

Extension idea (S5 / N5-bridging, non-assessed): briefly show a trace table for nested selection inside a loop as a look-ahead — keep clearly separate from the assessed task set.

Assessment standards covered: N3 O2.1; N4 O1.1, O1.2, O1.3.