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 the fundamental programming techniques required by the OCR A-Level Computer Science (H446) specification. These are the building blocks of all programs: sequence, selection, iteration, subroutines, and parameter passing.
Sequence is the most basic programming construct. Instructions are executed one after another, in the order they are written.
# Sequence: each line executes in order
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}, you are {age} years old")
Selection allows a program to choose between different paths of execution based on a condition. The condition evaluates to either True or False.
score = int(input("Enter score: "))
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(f"Grade: {grade}")
IF score >= 90 THEN
grade = "A*"
ELSEIF score >= 80 THEN
grade = "A"
ELSEIF score >= 70 THEN
grade = "B"
ELSE
grade = "U"
ENDIF
Some languages (Java, C++, but not Python) provide switch statements for multi-way selection based on a single value:
// Pseudocode switch statement
SWITCH day:
CASE "Monday":
OUTPUT "Start of the week"
CASE "Friday":
OUTPUT "End of the week"
DEFAULT:
OUTPUT "Midweek"
ENDSWITCH
In Python, the equivalent is the match statement (Python 3.10+):
match day:
case "Monday":
print("Start of the week")
case "Friday":
print("End of the week")
case _:
print("Midweek")
| Selection Type | Use Case |
|---|---|
| if/elif/else | When conditions are ranges or complex expressions |
| switch/case | When selecting based on a single value with multiple options |
| Nested if | When conditions depend on the outcome of previous conditions |
Iteration means repeating a block of code. There are three main types:
The loop runs a predetermined number of times.
# Print numbers 1 to 10
for i in range(1, 11):
print(i)
# Iterate over a list
names = ["Alice", "Bob", "Charlie"]
for name in names:
print(f"Hello, {name}")
FOR i = 1 TO 10
OUTPUT i
NEXT i
The loop repeats while a condition is True. The condition is checked before each iteration, so the body may execute zero times.
password = ""
while password != "secret123":
password = input("Enter password: ")
print("Access granted")
WHILE password != "secret123"
password = INPUT("Enter password: ")
ENDWHILE
The loop body executes at least once because the condition is checked after each iteration. Python does not have a built-in do-while, but it can be simulated:
# Simulating do-while in Python
while True:
number = int(input("Enter a positive number: "))
if number > 0:
break
print(f"You entered: {number}")
DO
number = INPUT("Enter a positive number: ")
UNTIL number > 0
| Loop Type | Condition Check | Minimum Iterations | Best For |
|---|---|---|---|
| for | Count-based | Predetermined | Known number of iterations |
| while | Before each iteration | 0 | Unknown iterations; may not execute |
| do-while | After each iteration | 1 | Must execute at least once (e.g., menu) |
Exam Tip: The examiner may ask you to choose between loop types. Use a for loop when you know how many iterations. Use a while loop when the number of iterations is unknown. Use a do-while when the body must execute at least once.
A subroutine is a named block of code that performs a specific task. Subroutines promote code reuse, modularity, and readability.
| Type | Returns a Value? | Purpose |
|---|---|---|
| Procedure | No | Performs an action (e.g., printing, modifying data) |
| Function | Yes | Computes and returns a value |
def display_menu():
print("1. Play Game")
print("2. View Scores")
print("3. Quit")
display_menu() # Calling the procedure
def calculate_area(length: float, width: float) -> float:
return length * width
area = calculate_area(5.0, 3.0)
print(f"Area: {area}") # Area: 15.0
PROCEDURE displayMenu()
OUTPUT "1. Play Game"
OUTPUT "2. View Scores"
OUTPUT "3. Quit"
END PROCEDURE
FUNCTION calculateArea(length: REAL, width: REAL) RETURNS REAL
RETURN length * width
END FUNCTION
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.