You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Trace tables are used to track how variables change as an algorithm executes. Completing trace tables is a common exam skill in OCR J277 Paper 2, typically worth 3-6 marks. This lesson teaches you how to trace through code systematically and avoid common errors.
A trace table is a table with a column for each variable and each output. Each row represents one step of the algorithm (usually one line of code or one iteration of a loop). By filling in the table, you can see exactly how the algorithm behaves.
OCR Exam Tip: Cross out each line of code as you trace through it (on the question paper) to keep track of where you are. For loops, use a tally to count iterations.
x = 1
total = 0
for i = 1 to 4
total = total + x
x = x * 2
next i
print(total)
| Step | i | x | total | Output |
|---|---|---|---|---|
| Init | — | 1 | 0 | |
| i=1 | 1 | 1 | 1 | |
| 1 | 2 | 1 | ||
| i=2 | 2 | 2 | 3 | |
| 2 | 4 | 3 | ||
| i=3 | 3 | 4 | 7 | |
| 3 | 8 | 7 | ||
| i=4 | 4 | 8 | 15 | |
| 4 | 16 | 15 | ||
| End | 15 |
The output is 15.
count = 0
total = 0
num = input("Enter a number (0 to stop): ")
while num != 0
total = total + num
count = count + 1
num = input("Enter a number (0 to stop): ")
endwhile
print(total / count)
If the user enters: 10, 20, 30, 0
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.