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.
Operators
- Arithmetic: + - * / (true division, always a float), // (integer division, rounds down), % (remainder, called modulus). Powers use the exponent operator, two asterisks, or pow(x, y).
- Relational: == != < <= > >= give True or False.
- Boolean: and, or, not. Python stops evaluating as soon as the answer is known (short-circuit evaluation), so x != 0 and 10 / x > 2 never divides by zero.
- Bitwise (work on the binary digits of integers): & (AND), | (OR), ^ (XOR), ~ (NOT), << (shift left), >> (shift right). Shifting left by n multiplies by 2 to the power n; shifting right by n divides by 2 to the power n, rounding down.
- The PLS calls << a logical shift left and >> an arithmetic shift right: >> keeps the sign, so -16 >> 2 is -4. Python integers have no fixed width, so ~x always equals -x - 1 (~5 is -6) and << never overflows.
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 -6Selection
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 unknownIteration
- A count-controlled loop repeats a known number of times: for i in range(start, stop, step). The stop value is never reached, so range(1, 5) gives 1, 2, 3, 4 and range(10, 0, -3) gives 10, 7, 4, 1.
- A condition-controlled loop repeats while a condition is true: while total < 100. Use it when you cannot know the number of repetitions in advance, for example validating input.
- break leaves the loop at once; continue skips to the next repetition. Use them sparingly: a clear loop condition is easier to read.
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: sevenWorked 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.
- Use a while loop because the number of attempts is unknown.
- Wrap int() in try / except ValueError so 'abc' does not crash the program.
- Check the range separately, so 150 is rejected with its own message.
- ```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"])) ```
- 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.
Check yourself
- 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`
- 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`
- 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`
- 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.
- 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`.
- 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`.
- 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.
- 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.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification, Issue 1 (March 2026)
- Pearson Edexcel IAL Computer Science Unit 4 Programming Language Subset (PLS)
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 and Unit 4)
Last updated 8 November 2026. Spotted a mistake? Tell us.