Gradient

Courses

∇\nabla∇The maths behind AI0/72
π\piπMaths0/84
⋆\star⋆AI for everyone0/10
Γ\GammaΓGame theory0/49
py\texttt{py}pyPython for AI0/15
y^\hat{y}y^​Build your own AI0/15
All courses

Tuition

My tuitionOlympiad trackCompetitions

Practice

ProjectsAsk a questionTrain on a GPUExplorationsMaths StoriesUnsolved problemsPlaygroundScience labsMock testsOlympiad

Reference

GlossaryBlogDiscoveries
Competitions › SOF ICSO › unit 3 of 16

Tracing a program

SOF ICSO · Computer Science · about 7 minutes

“What is the output?” questions are solved by tracing: keep a small table of every variable and update it line by line.

Run the program one step at a time and watch the variables.

In this lesson

  1. Be the computer
  2. Loops
  3. Conditions
  4. Worked examples
  5. Where marks are lost
  6. Check yourself

1Be the computer

To trace a program, make a table with a column for every variable and a row for every step. Run each line exactly as written, updating the table.

Computers do not guess: x = x + 1 means ‘take the old x, add 1, store it back’.

2Loops

A for loop from 1 to 5 runs 5 times. A while loop runs as long as its condition is true, and checks it before each run. Count the runs carefully: off-by-one mistakes are the most common error.

Nested loops multiply: an outer loop of 3 and an inner loop of 4 run the inner body 12 times.

3Conditions

If–else picks one path. Check comparison signs carefully: < is not ≤.

Integer division and remainder (// and % in Python) are common: 17 // 5 = 3 and 17 % 5 = 2.

Worked examples

  1. Example 1s = 0; for i in 1 to 4: s = s + i * i. What is s?

    1. i = 1: s = 1.
    2. i = 2: s = 5.
    3. i = 3: s = 14.
    4. i = 4: s = 30.

    Answer: 30

  2. Example 2n = 20; c = 0; while n > 1: n = n // 2; c = c + 1. What is c?

    1. n: 20 → 10 → 5 → 2 → 1.
    2. That is 4 halvings.

    Answer: 4

See it move

Where marks are lost

  • Running a loop one time too many or too few.
  • Using a value before it is updated in the same line.
  • Forgetting that integer division drops the remainder.

Exam tips

  • A loop “for i in 1 to 5” runs five times.
  • Update the table after every line, not in your head.
  • Watch for = (store) versus == (compare).

Practice questions

  1. x = 2; repeat 3 times: x = x × 2. What is x?

  2. total = 0; for i in 1 to 4: total = total + i. Output total?

  3. a = 5; b = 3; a = a + b; b = a − b. What is b?

More worked examples

Try each one first, then open the working.

  1. Fill the trace table. What value replaces “?”?

    total = 0for i in range(2, 8):total = total + i * 3itotal263?427542660781
    Show the working

    Start: Each row adds i × k to the total from the row above.

    total after i = 3: 6 + 9 = 15.

    Answer: 15

  2. Trace the flowchart. What number is printed?

    Starts = 0, n = 12n > 0?yess = s + nn = n − 1noprint sEnd
    Show the working

    Start: Write down s and n after every pass round the loop.

    s takes the values 12 + 11 + 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 78.

    Answer: 78

  3. How many times is print called? for i in range(5): for j in range(i, 8): print(i, j)

    Show the working

    Start: For each i, the inner loop runs b − i times.

    The inner loop runs 8 + 7 + 6 + 5 + 4 = 30 times.

    Answer: 30

  4. Fill the trace table. What value replaces “?”?

    total = 0for i in range(1, 7):total = total + i * 3itotal13293184?545663
    Show the working

    Start: Each row adds i × k to the total from the row above.

    total after i = 4: 3 + 6 + 9 + 12 = 30.

    Answer: 30

Practise more

New questions every time, at exam level

Fill the trace table. What value replaces “?”?

total = 0for i in range(3, 7):total = total + i * 4itotal3124285?672

Sit a timed SOF ICSO mock test

Logic gatesNext: Sorting algorithms