The answersDownload the PDF
Worksheet

F2.6 Condition-controlled loops: while

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

Looping until something changes, break, and loops that never end.

Questions 8 marks in all

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

    n = 1
    while n < 20:
        print(n)
        n = n * 2
  2. [1 mark]When should you use while rather than for?

    1. AWhen you will only know when to stop by looking, such as until the wall is close
    2. BWhen you know how many times
    3. CAlways
    4. DNever: while is out of date
  3. [1 mark]count = 0 then while count < 5: print(count) with nothing else in the block. What happens?

    1. AIt never ends: count never changes, so the question stays True
    2. BIt prints 0 to 4
    3. CIt prints nothing
    4. DSyntaxError
  4. [1 mark]What does break do?

    1. ALeaves the loop immediately
    2. BStops the whole program with an error
    3. CSkips one line
    4. DPauses the robot
  5. [1 mark]What does this program print?

    for i in range(10):
        if i == 3:
            break
        print(i)
    print("out")
  6. [1 mark]Why is there a wait(0.1) inside a loop that drives until the wall is close?

    1. AIt gives the robot time to move before the next check
    2. BIt makes the robot drive further
    3. CPython needs it to loop
    4. DIt stops the robot
  7. [1 mark]What is the difference between a while loop and REPEAT ... UNTIL?

    1. AREPEAT ... UNTIL checks at the end, so its block always runs at least once
    2. BThere is no difference
    3. Cwhile always runs at least once
    4. DREPEAT ... UNTIL can only count
  8. [1 mark]What does AQA call a condition-controlled loop?

    1. AIndefinite iteration
    2. BDefinite iteration
    3. CNested selection
    4. DA subroutine

The task: creep to the wall

Use a while loop: while the wall is more than 25 cm away, drive 5 cm and print the distance. Stop inside the green band, without touching the wall.

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

while distance() > 25:
    forward(50, distance=5)

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

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

Challenges

  1. In creep to the wall, count the steps and print the count at the end.
  2. Write a loop that halves a number starting at 100 until it is below 1, printing each value.
  3. Use break to stop a while True: loop when the robot has driven 50 cm, using position().