The answersDownload the PDF
Worksheet

F13.2 Trace tables

Exam preparation · GCSE · about 15 min

BugBotLab
NameClassDate

What this lesson is about

Being the computer: a row for every change, and the checks that catch a slip.

Questions 5 marks in all

  1. [1 mark]In a trace table, when do you write a new row?

    1. AEvery time a variable changes
    2. BOnce at the end
    3. COnly when something is printed
    4. DOnce for each variable
  2. [1 mark]What does this program print?

    total = 0
    n = 4
    while n > 0:
        total = total + n
        n = n - 1
    print(total)
  3. [1 mark]total = 0, n = 5. WHILE n > 1: total = total + n, n = n - 2. What is total at the end?

  4. [1 mark]A loop ends when its condition is false. What should the trace show for that check?

    1. AA row with the check written as false
    2. BNothing: the loop has ended
    3. CThe first row again
    4. DOnly the output
  5. [1 mark]What does 7 // 2 give?

The task: print the trace

Trace the algorithm in the comment by running it, printing one line per row in the form n=<n> total=<total> check=<true or false>, in the order the computer reaches them. Print the check as true while the loop keeps going and false on the row that ends it. Finish with output: <total>.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# total = 0
# n = 5
# WHILE n > 1
#     total = total + n
#     n = n - 2
# ENDWHILE
# OUTPUT total
total = 0
n = 5

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/f13-2-trace-tables/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Change the loop to n >= 1. Which extra row appears?
  2. Trace it by hand first, then run it. Did your table match?
  3. Write a trace for a for loop over range(3), showing the loop variable each time.