else, elif and Boolean operators

Choosing between outcomes, and joining conditions with and, or and not.

F2.3Decisions and loopsGCSE15 min

Covers OCR J277 2.2.1 Programming fundamentals · AQA 8525 3.2.5 Boolean operations in a programming language · Edexcel 1CP2 1.2.3 Algorithms with arithmetic, relational and logical operators · Edexcel 1CP2 6.5.3 Logical operators

Do this lesson in the simulator

if runs a block or skips it. Most decisions have more than one outcome: do this, otherwise do that; or pick one of several. This lesson adds else and elif, and shows how to join conditions together with and, or and not.

else: what to do otherwise

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

if distance() > 60:
    led("green")
    print("plenty of room")
else:
    led("red")
    print("not much room")
print("decided")

Run this in the simulator

else: has its own block. Exactly one of the two blocks runs: the if block when the condition is True, the else block when it is False. Never both, never neither.

Flowchart of the if else program: a decision on distance() > 60, a Yes path that turns the LED green and prints plenty of room, a No path that turns it red and prints not much room, then both paths meet before decided is printedStartdistance()> 60?YesNoled("green")print("plenty of room")led("red")print("not much room")print("decided")End
One question, two paths. The program goes down exactly one of them, never both and never neither, and the two paths join up again before decided is printed.

Compare with two separate ifs from the last lesson: those could both run, or neither. if/else is for when the outcomes are opposites.

elif: several choices

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

forward(50, distance=35)
d = distance()
if d < 20:
    led("red")
    print("stop")
elif d < 40:
    led("orange")
    print("careful")
else:
    led("green")
    print("go")
print("wall in", d, "cm")

Run this in the simulator

elif is short for "else if". Python checks the conditions from the top, runs the block of the first one that is True, and skips all the rest. So the order matters: d < 20 must come before d < 40, or a distance of 15 would be called "careful" instead of "stop".

Flowchart of the elif program: d is read, then d < 20 is asked first; if it is not true, d < 40 is asked; if neither is true the else block runs. Each Yes path sets the LED and prints, and every path meets before wall in d cm is printedStartd = distance()d < 20?Yesled("red")print("stop")d < 40?Yesled("orange")print("careful")led("green")print("go")NoNoprint("wall in", d, "cm")Endelse
The questions are asked from the top. The first one that is True wins and the rest are never asked, so a wall 15 cm away gets stop from the first question and never reaches the second.

Predict which block runs for d equal to 10, 30 and 50. As it stands the robot stops 46 cm from the edge of the mat, so it says go. Then change the 35 in the drive to 50 (30 cm left: careful) and to 65 (15 cm left: stop) and check.

Joining conditions: and, or, not

These are the Boolean operators. and needs both sides True. or needs at least one. not flips the answer:

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

d = distance()
b = battery()
print("clear and charged:", d > 30 and b > 20)
print("clear or charged:", d > 30 or b > 20)
print("not clear:", not d > 30)

Run this in the simulator

The whole set of answers fits in a truth table:

A B A and B A or B not A
False False False False True
False True False True True
True False False True False
True True True True False

In a program, a joined condition goes straight into an if:

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

if distance() > 30 and battery() > 20:
    print("safe to drive")
    forward(50, distance=20)
else:
    print("staying put")

Run this in the simulator

Checking a range

"Between 20 and 40" is two conditions joined with and:

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

d = distance()
if d >= 20 and d <= 40:
    print("in the parking zone")
else:
    print("outside the zone:", d)

Run this in the simulator

Python also lets you write it as 20 <= d <= 40, the way you would in maths.

Number lines for d from 0 to 60 cm: d >= 20 is true from 20 up, d <= 40 is true up to 40, and d >= 20 and d <= 40 is true only between 20 and 40, including both ends; the else block runs below 20 and above 40d >= 20d <= 40d >= 20 and d <= 40else block(d < 20 or d > 40)0102030405060d, the distance in cm
Joining the two with and keeps only the part where both are True: from 20 to 40, with a filled dot where the end counts and an open dot where it does not. Everything else, below 20 or above 40, goes to the else block.

Task: traffic light

Drive 15 cm, then read the distance and set the LED: red and print stop if the wall is under 20 cm away; orange and careful if under 40; green and go otherwise. The wall is placed so that the right answer is orange, but write all three branches.

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

forward(50, distance=15)
d = distance()
if d < 20:
    led("red")
    print("stop")

Task: safe to go?

If the way ahead is clear (more than 50 cm), print clear and drive 40 cm forward into the green zone. Otherwise print blocked and do not move.

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

if distance() > 50:
    print("clear")

Challenges

  1. Add a fourth colour for "very far" (over 80 cm) to the traffic light. Where must its condition go?
  2. Only drive if the battery is above 50 as well, using and.
  3. Rewrite safe-to-go so the blocked case comes first, using not.

Past paper questions on this

Real exam questions that test this, each worked through with a program you can run.