Exam preparation · GCSE · about 15 min
Being the computer: a row for every change, and the checks that catch a slip.
[1 mark]In a trace table, when do you write a new row?
[1 mark]What does this program print?
total = 0
n = 4
while n > 0:
total = total + n
n = n - 1
print(total)[1 mark]total = 0, n = 5. WHILE n > 1: total = total + n, n = n - 2. What is total at the end?
[1 mark]A loop ends when its condition is false. What should the trace show for that check?
[1 mark]What does 7 // 2 give?
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.
n >= 1. Which extra row appears?for loop over range(3), showing the loop variable each time.