Everyone Can Learn Program
中文 Start learning
Course 1 · Zero to codingLesson 8 of 10Free to read

Keep going: while · Robot Quest

How do you repeat something when you don't know how many times?

Course contents

Why you need it

for i in range(6) needs the number 6. But often you don't know it: "walk until you reach the wall", "keep going until the flag". A while loop repeats as long as a condition is true, so it adapts to any length.

The intuition

"Stir while the soup is still lumpy." You don't count stirs; you check, stir, check, stir... and stop as soon as the check fails.

while not at_goal():
    move()

Each round Python first asks the question. not flips the answer: not at_goal() is True while Bit is not yet on the flag. When the flag is reached it becomes False and the loop ends.

Danger: if nothing in the body can make the condition False, the loop never ends. Bit stops itself after many steps, and the page stops any run after a few seconds.

Definition

while loop

A while loop repeats its body as long as its condition is True, checking the condition before every round.

Infinite loop

An infinite loop is a loop whose condition never becomes False, so it never stops.

Examples

Walk until the flag

Run it in the lesson

Change the corridor length in your head: the code doesn't need to change.

while not at_goal():
    move()
Step by step
  1. L1Check: on the flag? No, so not at_goal() is True.at_goal() → False
  2. L2Move one square, then go back to the check.
  3. L1...the check repeats before every move...
  4. L1On the flag: not at_goal() is False, the loop ends.at_goal() → True

Two whiles in a row

Run it in the lesson

Walk to the wall, turn, walk to the next wall.

while front_clear():
    move()
turn_left()
while front_clear():
    move()
Step by step
  1. L1Front clear? Yes: keep moving east.
  2. L1At the wall the answer is False: the first loop ends.
  3. L3Turn left to face up.
  4. L4Second loop: walk up until the wall above the flag.

Common mistakes

A loop that never ends

while not at_goal(): turn_left() never moves, so the condition never changes. Make sure the body moves Bit closer to making the condition False.

Checking only once

The condition is checked before every round, not just the first one.

Quick quiz

1. Where does Bit end up?

while front_clear():
    move()
  1. ANext to the wall
  2. BIt crashes into the wall
  3. CIt never stops
Show the answer

Answer: A

Before each move Bit checks the front. When the wall is ahead the condition is False, so the loop stops: no crash.

2. When does a while loop check its condition?

  1. ABefore every round
  2. BOnly once, at the start
  3. COnly after the loop ends
Show the answer

Answer: A

Check, run the body, check again... The loop stops the first time the check is False.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictStop at the gem

    Predict the result.

  2. ModifyAny length

    This for loop only works when the corridor is exactly 5 long. Rewrite it with while so it works on all 3 maps.

  3. WriteUnknown L

    Each map is an L of a different size, with gems along the way. Write one program, with while, that reaches the flag on all 3 maps with every gem collected.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
while loopwhile 循环A loop that repeats while its condition is True.
Infinite loop死循环A loop whose condition never becomes False, so it never stops.
notnot 取反An operator that turns True into False and False into True.