The use of the three basic programming constructs used to control the flow of a program: Selection
2.2.1 Programming Fundamentals
Practise exam questionsProgramming Fundamentals — Note 3 of 6
Practical use of the techniques, Understanding of each technique
Selection allows a program to make decisions and choose different paths based on a condition. The most common forms are IF-THEN-ELSE statements and SWITCH/CASE SELECT statements. A condition is evaluated as True or False, and the program executes different code depending on the result.
True.False.elif in Python) allows multiple conditions to be checked in sequence.==, !=, <, >, <=, >=) and Boolean operators (AND, OR, NOT).if/elif/else chain only the FIRST true condition runs. Put the most restrictive check first (e.g. score >= 10 before score > 5), otherwise the later branches are never reached.match/case for switch/case selection. OCR Exam Reference Language uses switch/case/default/endswitch for the same idea.case _: is the default case. It runs when none of the other cases match, like else in an if statement.if = Selection, for = Iteration, while = Iteration, switch/case = SELECTION. Students commonly misidentify switch/case as iteration.input() returns a string, so match it with case "1":, not case 1:, unless you cast the input to int first.score = int(input("Enter your score: "))
if score >= 70:
print("Distinction")
elif score >= 50:
print("Merit")
elif score >= 40:
print("Pass")
else:
print("Fail")score = int(input("Enter your score: "))
if score >= 10:
print("Well done")
elif score > 5: # only checked if the first condition was False
print("Keep going")
else: # runs if no condition above was True
print("Try again")choice = input("Enter a number (1-3): ")
match choice:
case "1":
print("You chose Option 1")
case "2":
print("You chose Option 2")
case "3":
print("You chose Option 3")
case _: # default: runs if no other case matches
print("Invalid choice")Ready to test yourself?
Practise 2.2.1 questions