All subjects · Computer Science · WCP04 Advanced Practical Programming and Problem-solving

Operators and control flow constructs

Edexcel IAL Computer Science · WCP04 Advanced Practical Programming and Problem-solving

Every Unit 4 program is built from a handful of operators and control structures. This lesson revisits the Unit 2 toolkit and adds the extra constructs the Unit 4 Programming Language Subset (PLS) expects: match/case, bitwise operators and exception handling.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Operators

a, b = 17, 5
print(a / b, a // b, a % b)
print(12 & 10, 12 | 10, 12 ^ 10)
print(6 << 2, 6 >> 1, -16 >> 2, ~5)

Output:

3.4 3 2
8 14 6
24 3 -4 -6

Selection

if / elif / else chooses between branches; only the first true branch runs. When one value is compared against many fixed options, match / case is often clearer. A pattern can list alternatives with |, and case _ is the default that catches everything else.

def day_type(day):
    match day:
        case "Sat" | "Sun":
            return "weekend"
        case "Mon" | "Tue" | "Wed" | "Thu" | "Fri":
            return "weekday"
        case _:
            return "unknown"

print(day_type("Sun"), day_type("Wed"), day_type("Xyz"))

Output:

weekend weekday unknown

Iteration

Handling exceptions

A run-time error raises an exception. try / except lets the program recover instead of crashing. Name the error you expect (ValueError, TypeError, IndexError, KeyError, ZeroDivisionError, ArithmeticError) rather than using a bare except, which would also hide your own bugs.

values = ["12", "4.5", "seven"]
for v in values:
    try:
        print(int(v) * 2)
    except ValueError:
        print("not a whole number:", v)

Output:

24
not a whole number: 4.5
not a whole number: seven

Worked example

Write a loop that keeps asking for a mark until the user enters a whole number from 0 to 100, then prints it. Show how the code copes with 'abc', then 150, then 72.

  1. Use a while loop because the number of attempts is unknown.
  2. Wrap int() in try / except ValueError so 'abc' does not crash the program.
  3. Check the range separately, so 150 is rejected with its own message.
  4. ```python def get_mark(inputs): for raw in inputs: # stands in for repeated input() calls try: mark = int(raw) except ValueError: print("Enter a whole number") continue if 0 <= mark <= 100: return mark print("Out of range") print(get_mark(["abc", "150", "72"])) ```
  5. Output: ``` Enter a whole number Out of range 72 ```

Answer: Two error messages, then 72 is accepted. In the exam the real program would call input() inside a while True loop.

Key idea: / gives a float, // rounds down, % gives the remainder; & | ^ ~ << >> work on bits; match/case with case _ as the default; for for a known count, while for an unknown one; catch named exceptions with try/except.

Check yourself

  1. What does this program print? ```python print(17 // 5, 17 % 5, -17 // 5) ```
    Show answer

    3 2 -4. 17 // 5 = 3 and 17 % 5 = 2. Integer division rounds **down**, so -17 // 5 = -4 (not -3). Output: `3 2 -4`

  2. What does this program print? ```python print(12 & 10, 12 | 10, 12 ^ 10) ```
    Show answer

    8 14 6. 12 = 1100 and 10 = 1010. AND keeps bits set in both (1000 = 8), OR keeps bits set in either (1110 = 14), XOR keeps bits set in exactly one (0110 = 6). Output: `8 14 6`

  3. What does this program print? ```python print(5 << 3, 100 >> 2) ```
    Show answer

    40 25. Shifting left by 3 multiplies by 8: 5 x 8 = 40. Shifting right by 2 divides by 4: 100 / 4 = 25. Output: `40 25`

  4. What does this program print? ```python code = 404 match code: case 200: print("OK") case 400 | 404: print("client error") case _: print("other") ```
    Show answer

    client error. 404 matches the pattern 400 | 404, so the program prints `client error`. Only the first matching case runs.

  5. What does this program print? ```python total = 0 for i in range(10, 0, -3): total = total + i print(total) ```
    Show answer

    22. range(10, 0, -3) gives 10, 7, 4, 1 (it stops before 0). 10 + 7 + 4 + 1 = `22`.

  6. What does this program print? ```python x = 0 print(x != 0 and 10 / x > 2) ```
    Show answer

    False. x != 0 is False, so and stops there and never evaluates 10 / x. No ZeroDivisionError; the output is `False`.

  7. A program must keep reading sensor values until a value above 50 appears. Which construct fits best?
    • A for loop over range(50)
    • A match / case statement
    • A while loop with the condition value <= 50
    • An if / elif / else chain
    Show answer

    A while loop with the condition value <= 50. The number of repetitions is not known in advance, so a condition-controlled while loop is right.

  8. Which exception does the expression [3, 6, 9][3] raise?
    • KeyError
    • ValueError
    • TypeError
    • IndexError
    Show answer

    IndexError. The list has indexes 0, 1 and 2, so index 3 is out of range: IndexError.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.