Skip to content

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 CASE

Iteration (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 valid

Nested 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

SituationBest loop
You know how many times to repeatFOR
You repeat until a condition changesWHILE
You need to run the body at least onceREPEAT...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))   # False

Procedures vs functions

ProcedureFunction
Returns a value?NoYes
Pseudocode keywordPROCEDUREFUNCTION
Pythondef with no returndef 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: Distinction

Summary

TermMeaning
SelectionIF/ELSE - runs different code based on a condition
IterationLoop - repeats a block of code
FOR loopUsed when number of repetitions is known
WHILE loopUsed when repetitions depend on a changing condition
FunctionNamed reusable block of code that can return a value
ParameterVariable name in function definition
ArgumentValue 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?

Ad

PassMaven - revision made simple.