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")
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.