Menu

Pseudocode

Selection: IF and ELSE

Selection means the next statement or block to be executed depends on whether a condition is true or false. Without selection, statements simply run in the order they are written; with selection, a program can choose between different paths.

IF and ELSE

An IF statement is a selection statement. It holds a condition, and the statements inside the IF block run only when that condition is true. An ELSE branch handles the case where the condition is false, so the IF part and the ELSE part together always choose exactly one path.

bag_weight = float(input("Weight of your bag in kg: "))
if bag_weight > 23:
    print("Over the limit - a fee is due")
else:
    print("Bag accepted")

Output when the user types 20:

Weight of your bag in kg: 20
Bag accepted

Trace it with a bag of 20 kg: 20 > 23 is false, so the IF block is skipped and only the ELSE branch runs. With a bag of 26 kg the condition is true, so the IF branch runs and the ELSE is skipped. Exactly one of the two messages is printed, never both and never neither, which is why the two branches are alternatives rather than a list of steps.

A flowchart of the bag weight program: after the input, the decision bag_weight > 23? sends Yes to the over the limit message and No to Bag accepted, and both routes reach End.

Conditions

A condition compares two values using comparison operators:

OperatorMeaningExample condition
==equal todoor_code == 4821
<less thanage < 13
>greater thantotal > 50
<=less than or equal tolaps <= 10
>=greater than or equal toheight >= 120
!=not equal toanswer != 7

In Python, use these operators exactly as shown; a single = is assignment, not comparison. The indented statements under if and else belong to that branch, and the selection ends at the first statement lined up with the if.

steps = int(input("Steps walked today: "))
if steps >= 10000:
    print("Daily goal reached")
else:
    print("Goal not reached yet")
print("Well done for moving today")

Output when the user types 12000:

Steps walked today: 12000
Daily goal reached
Well done for moving today

Practise this