Selection: if

Relational operators, and a block that runs only when a condition is true.

F2.2Decisions and loopsGCSE12 min

Do this lesson in the simulator

Every program so far did the same thing every time. A robot that only does that is a toy. From this lesson on, your programs look at the world and choose. Choosing which lines run is called selection.

Questions with a yes-or-no answer

Python can compare values. The answer is one of two Boolean values, True or False:

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

d = distance()
print("wall in", d, "cm")
print("closer than 30?", d < 30)
print("further than 50?", d > 50)
print("exactly 80?", d == 80)

Run this in the simulator

These are the relational operators (also called comparison operators):

Operator Means
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to

One = stores a value in a variable. Two == ask whether two values are equal. Mixing them up is a very common early mistake, and Python's error message for it mentions "assignment".

if: run a block only when the answer is True

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

forward(60, distance=20)
if distance() < 40:
    led("red")
    print("something is close")
print("this line runs whatever happens")

Run this in the simulator

Here is the block from the last lesson doing its real job. The if line asks a question, the condition. If the answer is True, the block runs. If it is False, the block is skipped, and the program carries on with the first line after the block.

Run it. Then change 20 to 50 and run again: the robot ends up nearer the edge of the mat, the answer becomes True, and the block runs.

Predict, then run

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

speed = 40
if speed > 50:
    print("fast")
if speed > 30:
    print("medium")
if speed > 10:
    print("slow")
print("checked")

Run this in the simulator

Three separate questions, each with its own block. Work out which blocks run before pressing Run. Then change speed to 60 and predict again.

Comparing text

== and != work on strings too, which makes if and input a good pair:

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

answer = input("Shall I drive? (yes or no) ")
if answer == "yes":
    forward(50, distance=20)
    print("off I go")
if answer != "yes":
    print("staying here")

Run this in the simulator

Try yes, no and Yes. The capital Y makes a different string, so "Yes" == "yes" is False.

The answer can come from a variable

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

close = distance() < 40
print("is it close?", close)
if close:
    led("red")

Run this in the simulator

close holds True or False: a Boolean variable. if close: reads naturally, and naming a question like this makes the if line easy to read.

Task: near or far

Drive 20 cm. Then, with two separate ifs: if the wall is less than 40 cm away, print near and make the LED red; if it is 40 or more, print far and make it green. The wall has been placed so the answer should be near, but write both.

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

forward(50, distance=20)
d = distance()
if d < 40:
    print("near")

Challenges

  1. Print "even" if the battery percentage is an even number (remember % from lesson F1.9).
  2. Drive 10 cm only if the way ahead is clear for at least 30 cm, and print what you decided.
  3. Ask for a password with input and turn the LED green only if it is right.