Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.2, Edexcel 1CP2 6.2.2 · about 15 min
range, the loop variable, and repeating a known number of times.
[1 mark]What does this program print?
for i in range(4):
print(i)0 1 2 3
range(4) is 0, 1, 2, 3: four numbers, starting at 0 and never reaching 4.
[1 mark]What does this program print?
for n in range(1, 4):
print(n, n * n)1 1 2 4 3 9
range(1, 4) starts at 1 and stops before 4.
[1 mark]What does this print? total = 0 then for i in range(5): total = total + i, then print(total)
[1 mark]A square has four sides. Which loop drives it?
for i in range(4): with forward and turn_right(30, angle=90) insidefor i in range(3): with forward and turn_right(30, angle=90) insidefor i in range(90): with forward insidefor i in 4: with forward and turn_right(30, angle=90) inside[1 mark]How many times does the block under for i in range(10): run?
[1 mark]Put the lines in order to print corner 1, corner 2, corner 3.
Number the lines 1 to 3 to put them in the right order.
for i in range(3): print("corner", corner) corner = i + 1for i in range(3):
corner = i + 1
print("corner", corner)The for line comes first, and the lines of the block are indented under it.
[1 mark]What does this program print?
for n in range(0, 20, 5):
print(n)0 5 10 15
The third number is the step: 0, 5, 10, 15, stopping before 20.
[1 mark]In OCR's Reference Language, for i = 1 to 5 ... next i. How many times does the loop run?
[1 mark]What does AQA call a count-controlled loop?
Drive a square of 30 cm sides with a for loop, printing corner 1, corner 2, corner 3, corner 4 as you go, and finish facing the way you started.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for i in range(4):
forward(60, distance=30)The hint students can ask for: One trip round a square is the same two steps repeated four times, so put them in a loop and use the loop's counter in what you print.
from bugbot import *
connect()
for i in range(4):
forward(60, distance=30)
turn_right(30, angle=90)
print('corner', i + 1)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.