The worksheetDownload the PDF
Answers

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

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
    Answer:
    1
    2
    4
    8
    16

    The question is asked before every repeat. When n becomes 32 it is no longer below 20, so the loop ends.

  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
    Answer: A. for when you know how many times; while when a question decides.
  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
    Answer: A. Every while needs something inside it that will eventually make the question False, here count = count + 1.
  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
    Answer: A. break jumps out of the loop it is in, for or while, and the program carries on below the loop.
  5. [1 mark]What does this program print?

    for i in range(10):
        if i == 3:
            break
        print(i)
    print("out")
    Answer:
    0
    1
    2
    out

    When i is 3 the loop is left before the print, so 0, 1 and 2 are printed, then 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
    Answer: A. Without it the loop would check thousands of times before the robot had moved at all.
  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
    Answer: A. while checks the condition before each time round, so its block may never run. A post-condition loop checks after.
  8. [1 mark]What does AQA call a condition-controlled loop?

    1. AIndefinite iteration
    2. BDefinite iteration
    3. CNested selection
    4. DA subroutine
    Answer: A. Indefinite: nobody knows beforehand how many times it will run.

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)

The hint students can ask for: Keep going while there is still room: check the distance at the top of the loop and move a short way each time round, so you can stop as soon as it is close enough.

A solution

from bugbot import *
connect()
while distance() > 25:
    forward(50, distance=5)
    print(distance())

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.