- Understand what selection is and why programs need it
- Use comparison operators to write conditions that evaluate to True or False
- Write
if,if/else, andif/elif/elsestatements in Python and pseudocode
- I can explain what a condition is and give examples using comparison operators
- I can write an
if/elsestatement that handles two different outcomes - I can write an
if/elif/elsechain that handles three or more outcomes - I can trace through selection code and predict the output for a given input
- I can identify and correct common mistakes in selection code
Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.
1. What data type should you use to store a pupil's test score out of 100?
2. What does input() always return in Python, regardless of what the user types?
3. A program stores a person's age as age = 16. Which line of code would correctly display their age?
Key vocabulary
score >= 50.True or False.==, !=, <, >, <=, >=.if condition is False.Selection: if/else Statements
What is selection?
In everyday life, we constantly make decisions based on conditions: "If it's raining, take an umbrella — otherwise, leave it." Programming has the same idea. Selection allows a program to run different code depending on whether a condition is True or False. Without selection, a program would do exactly the same thing every single time, no matter what data was entered. Selection makes programs intelligent and responsive.
In N5 Computing Science, selection is implemented using if statements. The SQA course expects you to read, trace, and write selection in both Python and SQA pseudocode.
Comparison operators
A condition uses comparison operators to compare two values and produces a boolean result — either True or False. You must know all six operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
< | Less than | 4 < 10 | True |
> | Greater than | 10 > 20 | False |
<= | Less than or equal to | 5 <= 5 | True |
>= | Greater than or equal to | 3 >= 7 | False |
The single equals sign = is the assignment operator — it stores a value. The double equals == is the comparison operator — it tests equality. Confusing them is one of the most common errors in all programming.
The simple if statement
The simplest form of selection only runs code when a condition is True. If the condition is False, nothing happens and the program moves on.
if condition:
# code that runs only when condition is True
Two rules Python enforces strictly: the condition line must end with a colon (:), and the code inside the block must be indented (4 spaces or 1 Tab). Python uses indentation to know which lines belong inside the if — unlike many other languages that use curly braces.
The if/else statement
Often you want one thing to happen when a condition is True and something different when it is False. Add an else clause:
if condition:
# runs when condition is True
else:
# runs when condition is False
Exactly one of the two blocks always runs — you are guaranteed to get one outcome or the other. The else line also ends with a colon and its code is also indented.
The if/elif/else chain
When there are more than two possible outcomes, use elif (short for "else if") to test additional conditions:
if condition1:
# runs when condition1 is True
elif condition2:
# runs when condition1 is False AND condition2 is True
elif condition3:
# runs when condition1 and condition2 are False AND condition3 is True
else:
# runs when all conditions above are False
Python works through each condition in order from top to bottom. The moment one condition is True, that block runs and all remaining conditions are skipped entirely. You can have as many elif branches as you need. The else at the end is optional, but it is good practice to include it as a safety net for unexpected values.
Pseudocode vs Python
In the SQA exam you may be asked to write pseudocode or Python. Both mean the same thing but look different:
| SQA pseudocode | Python |
|---|---|
IF score >= 50 THEN SEND "Pass" TO DISPLAY ELSE SEND "Fail" TO DISPLAY END IF |
if score >= 50:
print("Pass")
else:
print("Fail") |
Key differences: pseudocode uses IF … THEN, ELSE, END IF as keywords. Python uses lowercase if, else, colons, and indentation — there is no END IF. Python is also case-sensitive: If or IF will cause an error.
Tracing selection code
An exam "trace" question gives you a piece of code and an input, then asks what the output will be. For selection, the technique is:
- Substitute the input value into any variables.
- Evaluate the first condition — is it True or False?
- If True, follow that branch and ignore the rest. If False, move to the next condition.
- Continue until one branch runs or you reach
else.
Work through every condition in sequence — never assume a branch runs without checking.
Worked examples
Write a program that asks for a user's age and prints "Welcome!" only if they are 18 or older.
input() returns a string, so we convert it immediately with int().age = int(input("Enter your age: "))>=.if statement. Note the colon and the indented print:age = int(input("Enter your age: "))
if age >= 18:
print("Welcome!")If age is 20, the condition 20 >= 18 is True → "Welcome!" is printed. If age is 15, the condition is False → nothing is printed.A test is out of 100. A score of 50 or above is a pass; anything below is a fail. Write a program to display the result.
score = int(input("Enter your score: "))if/else — there are exactly two outcomes (pass or fail), so else handles everything below 50 automatically.score = int(input("Enter your score: "))
if score >= 50:
print("Pass — well done!")
else:
print("Fail — please try again.")Trace with score = 72: condition 72 >= 50 is True → "Pass — well done!" is printed, else branch is skipped.Trace with score = 34: condition
34 >= 50 is False → else branch runs → "Fail — please try again."A marking scheme awards grades: 70+ is an A, 60–69 is a B, 50–59 is a C, below 50 is a Fail. Write the program.
else. We list them from highest to lowest — if we put the lowest condition first, nearly every score would match it.elif score >= 60 is only reached when score >= 70 was already False — so we don't need to write 60 <= score < 70; the ordering handles it automatically.score = int(input("Enter your score (0–100): "))
if score >= 70:
grade = "A"
elif score >= 60:
grade = "B"
elif score >= 50:
grade = "C"
else:
grade = "Fail"
print("Your grade is: " + grade)Trace with score = 65: first condition 65 >= 70 is False → second condition 65 >= 60 is True → grade = "B" → output: Your grade is: B.Write a program that asks for a password. If it matches "open123", print "Access granted." Otherwise, print "Wrong password."
input() with no conversion — it already returns a string.== works on strings too. Python compares them character by character, and the comparison is case-sensitive: "Open123" ≠ "open123".password = input("Enter password: ")
if password == "open123":
print("Access granted.")
else:
print("Wrong password.")A leisure centre charges different entry prices depending on age: under 5 years old enter free; ages 5 to 15 pay £4; ages 16 to 64 pay £9; ages 65 and over pay £5.
Answer the following:
- How many outcomes does this problem have? Which type of selection should you use?
- Write the conditions in the correct order — explain why the order matters.
- Write the complete Python program that asks for the user's age and prints the correct entry price.
- There are four outcomes (free, £4, £9, £5), so we use an
if/elif/elif/elsechain. - We must order conditions from the most specific/smallest value upwards (or downwards consistently). Here, checking
age < 5first makes sense; thenage <= 15(because we already know it's ≥ 5); thenage <= 64; andelsecatches 65+. If we put a broad condition likeage <= 64first, it would catch almost everyone and nothing else would ever run. age = int(input("Enter your age: ")) if age < 5: print("Entry price: Free") elif age <= 15: print("Entry price: £4") elif age <= 64: print("Entry price: £9") else: print("Entry price: £5")
= instead of == in a condition. Writing if score = 50: causes a syntax error in Python. = assigns a value; == tests equality. Always use == inside conditions.if score >= 50 (no colon) is a syntax error. Every if, elif, and else line must end with :.if isn't indented consistently, you'll get an IndentationError. Always use 4 spaces (or 1 Tab) and be consistent.else, no further elif can follow it — else must always be last. If you write else before elif, Python raises a syntax error.SQA questions on selection use the command words "write", "implement", "describe", and "identify". For trace questions, you are given code and an input and must write down the output — work through each condition in order and stop as soon as one is True. For write questions, make sure you:
- Use the correct operator (
==not=) - Include the colon after every
if,elif, andelse - Indent the code inside each block
- Order your conditions correctly in an
elifchain
In pseudocode answers, use IF … THEN, ELSE IF … THEN, ELSE, END IF and SEND … TO DISPLAY for output.
Questions 1–5 are auto-checked. Questions 6–10 are self-marked — write your answer, then reveal the model answer to check your work.
1. What does an if statement do when its condition evaluates to False? TYPE 1
2. Which of the following correctly checks whether a variable score is greater than or equal to 50? TYPE 1
3. What is the output of this code when the user enters 17?
age = int(input("Enter age: "))
if age >= 18:
print("You can vote.")
else:
print("Too young to vote.")
TYPE 1
4. Which symbol must appear at the end of every if, elif, and else line in Python? TYPE 1
5. A program needs to test three different conditions in order. After checking if and finding it False, which keyword should be used for the second test? TYPE 1
6. Explain the difference between the = operator and the == operator in Python. Give one example of each in context. TYPE 2
= is the assignment operator — it stores a value in a variable. Example: score = 75 stores the integer 75 in the variable score.== is the comparison operator — it tests whether two values are equal and returns True or False. Example: if score == 75: checks whether the value stored in score is equal to 75.Writing
if score = 75: causes a SyntaxError because you cannot use the assignment operator inside a condition.
7. Write the SQA pseudocode for an if/else statement that checks a variable called balance. If balance is greater than or equal to 0, display "Sufficient funds". Otherwise display "Account overdrawn". TYPE 2
IF balance >= 0 THEN SEND "Sufficient funds" TO DISPLAY ELSE SEND "Account overdrawn" TO DISPLAY END IFNote: SQA pseudocode uses
IF … THEN, ELSE, END IF, and SEND … TO DISPLAY for output.
8. The following code contains an error. Identify the mistake, explain why it is wrong, and write the corrected version. TYPE 2
score = int(input("Enter score: "))
if score >= 70:
grade = "A"
else:
grade = "B"
elif score >= 50:
grade = "C"
elif appears after else. Once an else is written, no further conditions can be tested — else must always be the final branch. Python raises a SyntaxError because elif is not allowed after else.Corrected version:
score = int(input("Enter score: "))
if score >= 70:
grade = "A"
elif score >= 50:
grade = "B"
else:
grade = "C"
9. Write a Python program that asks the user to enter a whole number. If the number is positive, print "Positive". If the number is zero, print "Zero". If the number is negative, print "Negative". Test your program with the values 7, 0, and −3 and write down the expected output for each. TYPE 3
number = int(input("Enter a number: "))
if number > 0:
print("Positive")
elif number == 0:
print("Zero")
else:
print("Negative")
Expected outputs:Input 7 →
PositiveInput 0 →
ZeroInput −3 →
Negative
10. A cinema charges different prices depending on age: under 12 → £5; ages 12–17 → £7; ages 18–64 → £10; ages 65 and over → £6. Write a complete Python program that asks for the user's age and prints the correct ticket price. Include at least four test cases covering each price band. TYPE 3
age = int(input("Enter your age: "))
if age < 12:
print("Ticket price: £5")
elif age <= 17:
print("Ticket price: £7")
elif age <= 64:
print("Ticket price: £10")
else:
print("Ticket price: £6")
Test cases:Input 8 →
Ticket price: £5 (under 12)Input 15 →
Ticket price: £7 (12–17)Input 30 →
Ticket price: £10 (18–64)Input 70 →
Ticket price: £6 (65+)Note: Conditions are ordered from lowest to highest age boundary. Because Python stops at the first True condition,
elif age <= 17 is only reached when age < 12 was already False — meaning we know age is 12 or above.
Suggested timing: 60 minutes. Warm up 5 min; vocabulary + notes 20 min; worked examples (live code) 10 min; now you try 5 min; task set 20 min.
Key misconception to address: Many pupils write if score = 50: using the assignment operator. Emphasise that == is always used inside conditions. A useful rule: "one equals stores, two equals compares."
Live demo suggestion: In PyCharm, deliberately write if score = 50: and show the red squiggly / SyntaxError. Then fix it to ==. Also demo an elif chain where conditions are in the wrong order (e.g. least restrictive first) and show how a score of 85 incorrectly triggers the lowest grade.
Common pupil confusion: When to use elif vs a second separate if. Explain that chained elif guarantees only one branch runs; independent if statements could each run separately. For mutually exclusive outcomes (grade bands, age groups), always use elif.
Extension question: Ask all pupils to add input validation to the cinema ticket program (Q10) — what should happen if the user enters a negative age? (Leads naturally into SDD8 complex conditions or SDD10 conditional loops.)
SQA command words covered: identify, explain, write, implement, trace.