Lesson 4 of 10
Programming: Lesson 4

Selection: IF, ELIF, ELSE

Teaching a computer to make decisions. This lesson covers single, two-branch and multi-branch selection, nested IF statements, and the condition-ordering trap that costs exam marks every year.

55 minutes Python + C# toggle
Language: Saved automatically

A program that does the same thing for every input is not useful. The first time you write "if the score is high enough, print a pass message" you have moved from a calculator to a decision-maker. Every real program is a web of conditions: if the user is logged in, if the payment went through, if the file exists, if the temperature is above the threshold. Selection is the first concept that makes software feel intelligent - even when it is not.

Think about it: A traffic light controller needs to choose between Red, Amber, Green and Red+Amber states. How many IF/ELIF branches would you need? Could you use nested IF statements instead? Which structure is clearer, and why does it matter for maintenance?
Terms you need to know
Selection
A control structure that executes different code depending on whether a condition is True or False.
Condition
A boolean expression (evaluates to True or False) that controls which branch executes.
IF statement
Executes a block of code only if the condition is True. Does nothing if False.
ELSE
Executes when the IF condition is False. Provides a default branch.
ELIF / else if
Additional conditions checked only if all previous conditions were False. Python: elif. C#: else if.
Nested IF
An IF statement inside another IF statement. Used for multi-layered decisions.
IF and IF-ELSE

The simplest selection: do something only if a condition is met. Adding ELSE provides a default action when the condition is False.

# Single-branch: only runs if condition is True
score = 72
if score >= 50:
    print("Pass")

# Two-branch: runs one branch or the other, never both
if score >= 50:
    print("Pass")
else:
    print("Fail")
// C# uses curly braces instead of indentation
int score = 72;

// Single-branch
if (score >= 50)
{
    Console.WriteLine("Pass");
}

// Two-branch
if (score >= 50)
{
    Console.WriteLine("Pass");
}
else
{
    Console.WriteLine("Fail");
}
Python uses indentation; C# uses curly braces

Python relies entirely on indentation to define code blocks. A misplaced space changes the program's logic. C# uses curly braces so indentation is cosmetic - but still expected by convention. Both are correct approaches; they just express the same logic differently.

ELIF and nested statements

When there are more than two possible outcomes, use elif (Python) or else if (C#). The branches are checked in order from top to bottom - the first one that is True fires, and the rest are skipped.

# Multi-branch: grade classifier
score = 84
if score >= 90:
    grade = "A*"
elif score >= 80:
    grade = "A"
elif score >= 70:
    grade = "B"
elif score >= 60:
    grade = "C"
elif score >= 50:
    grade = "D"
else:
    grade = "U"
print(grade)  # A
// C# multi-branch: grade classifier
int score = 84;
string grade;
if (score >= 90) grade = "A*";
else if (score >= 80) grade = "A";
else if (score >= 70) grade = "B";
else if (score >= 60) grade = "C";
else if (score >= 50) grade = "D";
else grade = "U";
Console.WriteLine(grade);  // A
The ordering trap - the most common IF mistake

Conditions in an if-elif chain are checked from top to bottom. If you put the LESS strict condition first, it will fire even for values that should match a later, stricter branch. Example: if you put if score >= 50 before elif score >= 90, a score of 95 would trigger the first branch and never reach the A* check. Always put the MOST restrictive condition first (highest threshold at the top, lowest at the bottom).

Nested IF statements:

# Nested IF: different logic inside each branch
age = 20
has_ticket = True

if age >= 18:
    if has_ticket:
        print("Welcome - adult with ticket")
    else:
        print("Need a ticket to enter")
else:
    print("Must be 18 or over")
Grade Classifier
Selection Branch Visualiser
Enter a score to see which IF/ELIF branch fires and which are skipped.
IF/ELIF branches:
if score >= 90: grade = "A*"
elif score >= 80: grade = "A"
elif score >= 70: grade = "B"
elif score >= 60: grade = "C"
elif score >= 50: grade = "D"
else: grade = "U"
Enter a score and click Classify...

Three Quick Challenges

Predict the output

What does this grade classifier print for score = 65?

score = 65
if score >= 70:
    print("A")
elif score >= 50:
    print("C")
else:
    print("F")
int score = 65;
if (score >= 70) Console.WriteLine("A");
else if (score >= 50) Console.WriteLine("C");
else Console.WriteLine("F");
Fill in the blank

Add the keyword that checks the first condition:

score >= 50: print("Pass") else: print("Fail")
(score >= 50) Console.WriteLine("Pass");
Spot the bug

A student writes two separate if statements. What unexpected output occurs when score = 75?

score = 75
if score >= 70:
    print("A")
if score >= 50:
    print("C")

Test yourself

1. A student writes:
if score >= 50: print("Pass")
if score >= 70: print("Good")
For score = 75, what is printed?

Both conditions are True, and using two separate IF statements means both are evaluated independently. 75 >= 50 (True) prints "Pass", then 75 >= 70 (True) prints "Good". If the student wanted only one to print, they should use elif for the second condition.

2. What is the output of this code for x = 15?
if x > 10: print("A")
elif x > 5: print("B")
else: print("C")

x = 15. First check: 15 > 10 is True, so "A" is printed. Because this is an elif chain, the remaining branches (elif and else) are skipped entirely. Only the first True branch executes.

3. The conditions in an elif chain should be ordered with the most restrictive first. Why?

If you put "if score >= 50" before "elif score >= 90", a score of 95 triggers the first (wrong) branch. The elif for >= 90 is never checked because the first condition was already True. Ordering from most restrictive (highest threshold) to least ensures each value reaches the correct branch.

4. In C#, what replaces Python's elif keyword?

C# uses "else if" - two separate words. Python uses "elif" - one word. Both mean exactly the same thing: check this condition only if all previous conditions were False. Common mistake: writing "elseif" as one word in C# gives a compile error.

5. What is the maximum number of times the ELSE branch can execute in a single if-elif-else chain?

The ELSE branch executes at most once - and only if every single preceding condition (IF and all ELIFs) was False. In a whole program run, the ELSE can execute repeatedly if the surrounding code runs in a loop, but per execution of the if-elif-else structure it fires at most once.
Challenge question

The following code is intended to classify BMI values. It compiles and runs without errors but produces wrong output for many inputs. Identify the bug precisely, explain why it produces wrong results, and write a corrected version.

bmi = float(input("BMI: "))
if bmi >= 18.5:
    print("Normal")
elif bmi >= 25:
    print("Overweight")
elif bmi >= 30:
    print("Obese")
else:
    print("Underweight")
The bug: The conditions are ordered from least restrictive to most restrictive. Any BMI >= 18.5 (including 26 and 31) triggers the first "Normal" branch, and the elif for >= 25 and >= 30 are never checked. A BMI of 28 (overweight) would print "Normal". A BMI of 35 (obese) would also print "Normal".

Fix: Order from most restrictive to least, or restructure the ranges:
if bmi >= 30: print("Obese")
elif bmi >= 25: print("Overweight")
elif bmi >= 18.5: print("Normal")
else: print("Underweight")

Now a BMI of 31 hits the first condition; 27 skips to the second; 22 skips to the third; 16 falls to else.
Printable Worksheets

Practice what you have learned

Three levelled worksheets. Download, print and complete offline.

Recall
Selection Structures
Label IF/ELIF/ELSE structures, trace through conditions with given values and identify which branch fires.
Download
Apply
Writing Selection Code
Write grade classifiers, ticket price calculators and age-group categorisers using multi-branch selection.
Download
Exam Style
Exam-Style Questions
Trace selection code, identify ordering bugs, correct broken conditions and predict outputs for boundary values.
Download
Lesson 4 - Programming
Selection: IF, ELIF, ELSE
Starter activity
Draw a flowchart decision diamond on the board with the question "Score >= 50?" and two branches: Yes - "Print Pass", No - "Print Fail". Ask students to convert it into Python code. This bridges from flowcharts (covered in Algorithms) to code, making selection feel familiar.
Lesson objectives
1
Write single-branch (IF), two-branch (IF/ELSE) and multi-branch (IF/ELIF/ELSE) selection in Python and C#.
2
Correctly order conditions in an elif chain from most to least restrictive.
3
Explain the difference between separate IF statements and an elif chain.
4
Write and trace nested IF statements.
5
Identify condition-ordering bugs and correct them.
Key vocabulary
Selection
Control structure executing different code depending on a condition. Also called branching.
elif / else if
Additional condition checked only if all previous conditions were False.
Nested IF
An IF inside another IF block. Increases complexity - prefer flat elif chains where possible.
Discussion questions
What is the difference between using two separate IF statements versus using IF/ELIF? When would you choose each?
Why is condition ordering particularly important in grade classifiers and age checks?
Can you think of a real system where nested IF is unavoidable? What makes nested IF harder to read and test?
Exit tickets
Write an IF/ELIF/ELSE that assigns a ticket price: under 5 = free, under 16 = £5, 16 to 64 = £12, 65 and over = £8. [4 marks]
The following code has an ordering bug. Identify it and explain the effect on a score of 85. [3 marks]
What is the output when x = 7: if x > 5: print("A") elif x > 3: print("B") else: print("C") [1 mark]
Homework suggestion
Write a program that asks the user for their age and outputs the correct fare category for a transport system: under 5 = free, 5-15 = child (£1.20), 16-25 = young person (£1.80), 26-64 = adult (£2.50), 65+ = senior (£1.00). Test with ages at every boundary: 4, 5, 15, 16, 25, 26, 64, 65.