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.
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.
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 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.
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
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")
Three Quick Challenges
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");
Add the keyword that checks the first condition:
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?
2. What is the output of this code for x = 15?if x > 10: print("A")
elif x > 5: print("B")
else: print("C")
3. The conditions in an elif chain should be ordered with the most restrictive first. Why?
4. In C#, what replaces Python's elif keyword?
5. What is the maximum number of times the ELSE branch can execute in a single if-elif-else chain?
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")
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.
Practice what you have learned
Three levelled worksheets. Download, print and complete offline.