You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers selection (also called branching or conditional statements) as required by OCR J277 Section 2.3. Selection allows a program to make decisions and execute different blocks of code depending on whether a condition is true or false.
Selection is a programming construct that allows the program to choose between different paths of execution based on a condition. Without selection, programs would always execute the same instructions in the same order, regardless of input.
The three main selection structures in OCR pseudocode are:
The following flowchart shows how if/elseif/else selection works:
flowchart TD
A([Start]) --> B{Condition 1\ntrue?}
B -- Yes --> C["Execute\nif block"]
B -- No --> D{Condition 2\ntrue?}
D -- Yes --> E["Execute\nelseif block"]
D -- No --> F["Execute\nelse block"]
C --> G([Continue])
E --> G
F --> G
Executes a block of code only if the condition is true.
OCR Pseudocode:
age = int(input("Enter your age: "))
if age >= 18 then
print("You are an adult")
endif
Python:
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult")
If the condition is false, nothing happens — the program continues to the next line after endif.
Chooses between two paths: one if the condition is true, another if it is false.
OCR Pseudocode:
mark = int(input("Enter your mark: "))
if mark >= 50 then
print("Pass")
else
print("Fail")
endif
Python:
mark = int(input("Enter your mark: "))
if mark >= 50:
print("Pass")
else:
print("Fail")
OCR Exam Tip: In OCR pseudocode, you must include
thenafter the condition andendifto close the block. Forgettingendifis a common mistake that will lose marks.
Chooses between multiple paths by checking conditions in order.
OCR Pseudocode:
mark = int(input("Enter your mark: "))
if mark >= 90 then
print("Grade A*")
elseif mark >= 80 then
print("Grade A")
elseif mark >= 70 then
print("Grade B")
elseif mark >= 60 then
print("Grade C")
elseif mark >= 50 then
print("Grade D")
else
print("Grade U")
endif
Python:
mark = int(input("Enter your mark: "))
if mark >= 90:
print("Grade A*")
elif mark >= 80:
print("Grade A")
elif mark >= 70:
print("Grade B")
elif mark >= 60:
print("Grade C")
elif mark >= 50:
print("Grade D")
else:
print("Grade U")
The conditions are checked in order from top to bottom. As soon as one condition is true, that block executes and the rest are skipped. If none are true, the else block runs.
| Mark | First True Condition | Output |
|---|---|---|
| 95 | mark >= 90 | Grade A* |
| 85 | mark >= 80 | Grade A |
| 75 | mark >= 70 | Grade B |
| 45 | None | Grade U |
OCR Exam Tip: Note that OCR pseudocode uses
elseif(one word), while Python useselif. Make sure you use the correct keyword for whichever language you are writing in.
You can place if statements inside other if statements. This is called nesting.
OCR Pseudocode:
age = int(input("Enter age: "))
member = input("Are you a member? (yes/no): ")
if age >= 18 then
if member == "yes" then
print("Adult member discount: 20% off")
else
print("Adult full price")
endif
else
print("Under 18: free entry")
endif
Python:
age = int(input("Enter age: "))
member = input("Are you a member? (yes/no): ")
if age >= 18:
if member == "yes":
print("Adult member discount: 20% off")
else:
print("Adult full price")
else:
print("Under 18: free entry")
Conditions can be combined using AND, OR, and NOT:
OCR Pseudocode:
temperature = int(input("Enter temperature: "))
raining = input("Is it raining? (true/false): ")
if temperature > 20 AND raining == "false" then
print("Go to the park")
elseif temperature > 20 AND raining == "true" then
print("Visit an indoor attraction")
elseif temperature <= 20 OR raining == "true" then
print("Stay at home")
endif
OCR Pseudocode:
storedUser = "admin"
storedPass = "pass123"
username = input("Enter username: ")
password = input("Enter password: ")
if username == storedUser AND password == storedPass then
print("Login successful")
elseif username != storedUser then
print("Username not recognised")
else
print("Incorrect password")
endif
Python:
stored_user = "admin"
stored_pass = "pass123"
username = input("Enter username: ")
password = input("Enter password: ")
if username == stored_user and password == stored_pass:
print("Login successful")
elif username != stored_user:
print("Username not recognised")
else:
print("Incorrect password")
if...then...endif for a single condition.if...then...else...endif for two paths.if...then...elseif...else...endif for multiple paths.AND, OR, NOT.elseif, Python uses elif.OCR Exam Tip: When writing selection statements in the exam, always close your blocks with
endifin OCR pseudocode. In Python, ensure correct indentation — Python uses indentation instead ofendifto define code blocks.
A program takes a person's height (metres) and weight (kg), calculates their Body Mass Index (BMI), and reports which category they fall into. The BMI formula is weight divided by height squared.
OCR reference language pseudocode:
weight = real(input("Enter weight in kg: "))
height = real(input("Enter height in metres: "))
bmi = weight / (height ^ 2)
if bmi < 18.5 then
category = "Underweight"
elseif bmi < 25 then
category = "Healthy"
elseif bmi < 30 then
category = "Overweight"
else
category = "Obese"
endif
print("Your BMI is " + str(bmi) + " (" + category + ")")
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.