The answersDownload the PDF
Worksheet

F2.5 Count-controlled loops: for

Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.2, Edexcel 1CP2 6.2.2 · about 15 min

BugBotLab
NameClassDate

What this lesson is about

range, the loop variable, and repeating a known number of times.

Questions 9 marks in all

  1. [1 mark]What does this program print?

    for i in range(4):
        print(i)
  2. [1 mark]What does this program print?

    for n in range(1, 4):
        print(n, n * n)
  3. [1 mark]What does this print? total = 0 then for i in range(5): total = total + i, then print(total)

  4. [1 mark]A square has four sides. Which loop drives it?

    1. Afor i in range(4): with forward and turn_right(30, angle=90) inside
    2. Bfor i in range(3): with forward and turn_right(30, angle=90) inside
    3. Cfor i in range(90): with forward inside
    4. Dfor i in 4: with forward and turn_right(30, angle=90) inside
  5. [1 mark]How many times does the block under for i in range(10): run?

  6. [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.

    1. for i in range(3):
    2. print("corner", corner)
    3. corner = i + 1
  7. [1 mark]What does this program print?

    for n in range(0, 20, 5):
        print(n)
  8. [1 mark]In OCR's Reference Language, for i = 1 to 5 ... next i. How many times does the loop run?

  9. [1 mark]What does AQA call a count-controlled loop?

    1. ADefinite iteration
    2. BIndefinite iteration
    3. CSelection
    4. DRecursion

The task: count the corners

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)

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

QR code
Do it on the robot
www.bugbotlab.com/learn/f2-5-count-controlled-loops-for/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Draw a triangle with a for loop (three sides, turn_right(30, angle=120)).
  2. Blink the LED five times using wait(0.2) inside a loop.
  3. Print the 7 times table, from 7 to 70, with one loop.