Appearance
C5 · Control Structures
Spec reference: Section C - Programming Paradigms
Key idea: Control the order in which instructions execute using loops, branches and function calls.
Selection (branches)
IF...ELSE
python
# Simple IF
if temperature > 30:
print("It's hot!")
# IF...ELSE
if score >= 50:
print("Pass")
else:
print("Fail")
# IF...ELIF...ELSE (multiple conditions)
if score >= 70:
grade = "A"
elif score >= 60:
grade = "B"
elif score >= 50:
grade = "C"
else:
grade = "U"
print("Grade:", grade)CASE / SWITCH (pseudocode)
Some languages use a CASE statement for multiple fixed options:
CASE day OF
"Mon": SEND "Start of week" TO DISPLAY
"Fri": SEND "End of week" TO DISPLAY
"Sat", "Sun": SEND "Weekend!" TO DISPLAY
OTHERWISE: SEND "Midweek" TO DISPLAY
END CASEIteration (loops)
FOR loop - known number of repetitions
python
# Print 1 to 10
for i in range(1, 11):
print(i)
# Loop through a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)WHILE loop - unknown number of repetitions
python
# Keep asking until valid input
password = ""
while password != "secret":
password = input("Enter password: ")
print("Access granted")REPEAT...UNTIL (do...while equivalent)
python
# Python doesn't have REPEAT...UNTIL natively, but logic is:
while True:
age = int(input("Enter age: "))
if age >= 0 and age <= 120:
break # exit loop only when validNested loops
python
# Multiplication table
for i in range(1, 4):
for j in range(1, 4):
print(i, "×", j, "=", i * j)Output:
1 × 1 = 1
1 × 2 = 2
1 × 3 = 3
2 × 1 = 2
...Choosing the right loop
| Situation | Best loop |
|---|---|
| You know how many times to repeat | FOR |
| You repeat until a condition changes | WHILE |
| You need to run the body at least once | REPEAT...UNTIL |
Function calls
A function is a named, reusable block of code. You define it once and call it as many times as needed.
python
# Define
def calculate_area(length, width):
area = length * width
return area
# Call
room1 = calculate_area(5, 4) # → 20
room2 = calculate_area(3, 7) # → 21
print(room1, room2)Parameters vs arguments
- Parameter - the variable name in the function definition:
length,width - Argument - the actual value passed when calling:
5,4
Return values
python
def is_even(n):
return n % 2 == 0
print(is_even(4)) # True
print(is_even(7)) # FalseProcedures vs functions
| Procedure | Function | |
|---|---|---|
| Returns a value? | No | Yes |
| Pseudocode keyword | PROCEDURE | FUNCTION |
| Python | def with no return | def with return |
Worked example: Grade calculator
python
def get_grade(score):
if score >= 70:
return "Distinction"
elif score >= 55:
return "Merit"
elif score >= 40:
return "Pass"
else:
return "Fail"
scores = [82, 61, 45, 33, 70]
for s in scores:
print(f"Score {s}: {get_grade(s)}")Output:
Score 82: Distinction
Score 61: Merit
Score 45: Pass
Score 33: Fail
Score 70: DistinctionSummary
| Term | Meaning |
|---|---|
| Selection | IF/ELSE - runs different code based on a condition |
| Iteration | Loop - repeats a block of code |
| FOR loop | Used when number of repetitions is known |
| WHILE loop | Used when repetitions depend on a changing condition |
| Function | Named reusable block of code that can return a value |
| Parameter | Variable name in function definition |
| Argument | Value passed to function when calling it |
Test Yourself
Question 1 of 5
A program needs to repeat a block of code exactly 10 times. Which loop type is most appropriate?