Selection: if
Relational operators, and a block that runs only when a condition is true.
Covers OCR J277 2.2.1 Programming fundamentals · AQA 8525 3.2.4 Relational operations in a programming language · Edexcel 1CP2 6.5.2 Relational operators
Do this lesson in the simulatorPrint it: worksheet (PDF) · answers (PDF) · as a web page
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)
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: if d = 30: stops with a SyntaxError that asks Maybe you meant '==' or ':=' instead of '='?
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")
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")
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")
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")
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
- Print "even" if the battery percentage is an even number (remember
%from lesson F1.9). - Drive 10 cm only if the way ahead is clear for at least 30 cm, and print what you decided.
- Ask for a password with
inputand turn the LED green only if it is right.
Past paper questions on this
Real exam questions that test this, each worked through with a program you can run.
- AQA 8525 June 2023 Paper 1, Question 7: A ticket price with a group discount 6 marks
- AQA 8525 June 2022 Paper 1, Question 1: MOD, selection, assignment, relational operators and a definite loop 6 marks
- AQA 8525 June 2022 Paper 1, Question 7: Ask for an email address twice and check that they match 5 marks
- AQA 8525 Sample Paper 1, Question 8: Output LOWER or NOT LOWER for a character 7 marks
- OCR J277 June 2025 Paper 2, Question 6(a): Input two passwords and check that they match 4 marks
- OCR J277 June 2024 Paper 2, Question 2: Complete a flowchart that decides odd or even with MOD 4 marks
- OCR J277 June 2022 Paper 2, Question 5(e): Car park charges, repeated until 0 hours is entered 6 marks
- OCR J277 Sample Paper 2, Question 2: Complete a comparison of two numbers, then write a loop that doubles inputs 10 marks