Home / GCSE · AQA / Computer Science / Programming

CS5Operators and Selection

3.2.3 Arithmetic operations

Arithmetic operators

You need addition, subtraction, multiplication, real division, and integer division with remainders.

  • Integer division is a two-stage idea: 11 ÷ 2 gives 11 DIV 2 = 5 and 11 MOD 2 = 1, so 5 remainder 1.
  • DIV drops the remainder; it doesn’t round. 19 DIV 4 is 4, not 5.
  • Order: brackets first, then *, /, DIV, MOD, then + and -. Equal ones work left to right. Use brackets whenever you’re not sure.
  • In Python, / always gives a real (float): 8 / 2 is 4.0.
  • Never put a £ sign in a calculation: total = £5 * qty is an error.
OperationAQA pseudo-codePythonExample → result
Addition++7 + 2 → 9
Subtraction--7 - 2 → 5
Multiplication**7 * 2 → 14
Real division//7 / 2 → 3.5
Integer divisionDIV//7 DIV 2 → 3
Remainder (modulus)MOD%7 MOD 2 → 1
Worked exampleWork out what each line outputs.
print(3 + 4 * 2)
print((3 + 4) * 2)
print(20 / 8)
print(20 // 8)
print(20 % 8)
  1. 3 + 8 = 11* before +
  2. 7 × 2 = 14brackets first
  3. 2.5real division
  4. 28 goes into 20 twice; the remainder is dropped
  5. 420 − 2 × 8 = 4 left over
11, 14, 2.5, 2, 4 (one per line)
Exam tipWhen you edit a line of code (for example to add a fee), check the brackets: cost = price * qty + 2 adds the £2 once, but cost = price * (qty + 2) doesn’t.
Real division
Division that can give an answer with a fractional part: 7 / 2 = 3.5.
Integer division (DIV, //)
Division that gives the whole-number part only: 7 DIV 2 = 3.
Remainder (MOD, %)
What is left over after integer division: 7 MOD 2 = 1.
3.2.3 Arithmetic operations

Using DIV and MOD

DIV and MOD solve lots of small problems in exam programs.

  • If n MOD k is 0, then n divides exactly by k.
  • Odd numbers give n MOD 2 = 1.
TaskPseudo-codePython
Is n even?n MOD 2 = 0n % 2 == 0
Is n a multiple of 5?n MOD 5 = 0n % 5 == 0
Every 3rd item (count is 3, 6, 9…)count MOD 3 = 0count % 3 == 0
Last digit of nn MOD 10n % 10
n without its last digitn DIV 10n // 10
Whole hours in a number of minutesmins DIV 60mins // 60
Minutes left overmins MOD 60mins % 60
Worked exampleConvert 200 minutes into hours and minutes.
totalMinutes = int(input("Minutes: "))
hours = totalMinutes // 60
minutes = totalMinutes % 60
print(f"{hours} h {minutes} min")
  1. 200 DIV 60 = 360 goes into 200 three whole times
  2. 200 MOD 60 = 20200 − 3 × 60 = 20
  3. Check: 3 × 60 + 20 = 200 ✓DIV and MOD together rebuild the number
3 h 20 min
Exam tipMOD gives the remainder, not the decimal part: 7 MOD 2 is 1, not 0.5.
3.2.4 Relational operations

Relational operators

Relational operators compare two values. The result is always a Boolean: True or False.

  • In Python, == compares and = assigns. In pseudo-code, = compares.
  • Strings are compared character by character in alphabetical (character-code) order: 'adam' < 'adele' because d comes before e.
  • ≤ and ≥ include the value itself: 4 <= 4 is True; 4 < 4 is False.
  • In Python write >= and <= (the equals sign second).
  • The pointed end of < or > points at the smaller value.
MeaningAQA pseudo-codePythonExample that is True
Equal to===3 == 3
Not equal to≠!=qty != 7
Less than<<4 < 6
Greater than>>4.1 > 4.0
Less than or equal to≤<=4 <= 4
Greater than or equal to≥>=4.5 >= 4.5
Exam tipIf asked to identify the relational operator in a line, give just the operator (for example >), not the whole line or a description.
Relational operator
An operator that compares two values and gives True or False: =, ≠, <, >, ≤, ≥.
3.2.5 Boolean operations

Boolean operators: NOT, AND, OR

Boolean operators combine or reverse conditions in selection and iteration.

  • Each comparison needs both sides. Write size == "S" or size == "M", never size == "S" or "M".
  • Inside a range: mark >= 0 and mark <= 100. Outside a range: mark < 0 or mark > 100.
  • Never write a chained comparison (two comparison operators in a row, as you might in maths). It is wrong or an error in other languages, and examiners discourage it.
  • NOT is worked out before AND, and AND before OR. Use brackets to make the meaning clear: (a < 1) OR (a > 9).
OperatorPythonTrue when…
ANDandboth conditions are True
ORorat least one condition is True
NOTnotthe condition is False (it reverses it)
Worked exampleage is 15 and member is True. Work out each value.
age = 15
member = True
print(age >= 16 and member)
print(age >= 16 or member)
print(not member)
  1. age >= 16 is False; False and True → FalseAND needs both to be True
  2. False or True → TrueOR needs at least one
  3. not True → FalseNOT reverses
False, True, False
Exam tipif letter == "A" or "B": is always True: "B" on its own counts as True. Examiners see this every year. Repeat the variable in each comparison.
AND
True only when both conditions are True.
OR
True when at least one condition is True.
NOT
Reverses a condition: True becomes False and False becomes True.
3.2.2 Programming concepts

Selection: IF, ELSE and ELSE IF

A selection statement allows a block of code to be executed only when a certain condition is satisfied.

  • Conditions are checked from the top. The first True one runs, and the rest are skipped. Exactly one branch of an IF … ELSE runs.
  • Python uses a colon and indentation to show which lines are inside each branch; pseudo-code uses ENDIF. In the exam, use the vertical guide lines in the answer box to show indentation.
  • elif is Python’s ELSE IF.
AQA pseudo-codePython
IF temp > 30 THEN
   OUTPUT 'Hot'
ENDIF
if temp > 30:
    print("Hot")
IF temp > 30 THEN
   OUTPUT 'Hot'
ELSE
   OUTPUT 'Not hot'
ENDIF
if temp > 30:
    print("Hot")
else:
    print("Not hot")
IF temp > 30 THEN
   OUTPUT 'Hot'
ELSE IF temp > 15 THEN
   OUTPUT 'Warm'
ELSE
   OUTPUT 'Cold'
ENDIF
if temp > 30:
    print("Hot")
elif temp > 15:
    print("Warm")
else:
    print("Cold")

IF … ELSE as a flowchart: exactly one of the two outputs happens

Selection
A selection statement allows a block of code to be executed only when a certain condition is satisfied.
Condition
An expression that is True or False, used to decide which branch runs.
3.2.2 Programming concepts

Nested selection

When a selection statement appears inside another selection statement, it is nested.

  • The inner IF is only checked when the outer condition is True.
  • Nesting lets each situation have its own message. Some nested IFs can be joined into one condition with AND, but then you lose the separate messages.
  • IF statements can be nested inside the THEN part, the ELSE part or both.
AQA pseudo-codePython
IF doorClosed THEN
   IF floor ≠ target THEN
      OUTPUT 'Moving'
   ELSE
      OUTPUT 'Arrived'
   ENDIF
ELSE
   OUTPUT 'Close the door'
ENDIF
doorClosed = True
floor = 2
target = 5
if doorClosed:
    if floor != target:
        print("Moving")
    else:
        print("Arrived")
else:
    print("Close the door")
Worked exampleUsing the lift program, what is output when (a) doorClosed is True, floor 5, target 5; (b) doorClosed is False, floor 1, target 4?
  1. (a) outer: doorClosed is True → check innergo inside the THEN part
  2. (a) inner: 5 ≠ 5 is False → ELSE → Arrivedthe floors match
  3. (b) outer: doorClosed is False → ELSE → Close the doorthe inner IF is never checked
(a) Arrived (b) Close the door
Nested selection
A selection statement inside another selection statement.
3.2.4, 3.2.5

Writing conditions exactly: words, boundaries and NOT

Turn the words of a brief into the exact operator. The boundary value decides it.

  • Removing NOT: flip the operator to its opposite. The opposite of < is ≥; the opposite of > is ≤; the opposite of = is ≠.
  • So NOT (x < 10) is the same as x ≥ 10, not x > 10.
  • Check any rewrite with the boundary value: both versions must give the same result for it.
The brief says…ConditionIs 10 included?
more than 10, over 10x > 10No
at least 10, 10 or morex >= 10Yes
less than 10, under 10x < 10No
up to 10, no more than 10, at most 10x <= 10Yes
between 1 and 10 inclusivex >= 1 and x <= 10Yes (and 1)
Worked exampleRewrite NOT (score ≥ 40) without using NOT.
  1. The opposite of ≥ is <flip the operator
  2. score < 40the rewrite
  3. Check 40: NOT (40 ≥ 40) = NOT True = False; 40 < 40 = False ✓the boundary
  4. Check 39: NOT (39 ≥ 40) = True; 39 < 40 = True ✓a value just below
score < 40
Exam tipIn 2024 only 1 in 5 students rewrote a condition without NOT correctly. The usual slip was getting the boundary wrong (using > where ≥ was needed).
3.2.2 Programming concepts

Writing selection to a brief: order matters

When values fall into bands, the order of the conditions decides which band is chosen.

  • With > or ≥, test the biggest threshold first. (With < or ≤, test the smallest first.)
  • If a smaller threshold comes first, big values match it and the later branch can never run.
  • Store the result in a variable in each branch and output it once after the IF. Then one output line covers every branch.
  • Read every boundary in the brief: ‘6 or more’ needs >= 6.

Biggest threshold first: each value reaches only one output

Worked exampleRate a broadband speed: 300 or more is Ultrafast, 30 or more is Superfast, anything else is Standard.
  1. if speed >= 300:biggest threshold first
  2. elif speed >= 30:only reached when speed is under 300
  3. else:everything under 30
  4. Wrong order: testing speed >= 30 first means 500 is rated Superfast, and Ultrafast is never outputthe first True branch wins
speed = int(input("Speed in Mbps: "))
if speed >= 300:
    rating = "Ultrafast"
elif speed >= 30:
    rating = "Superfast"
else:
    rating = "Standard"
print(rating)
Exam tipIn 2025 many students checked > 15 before > 25, so the top band was never reached. In 2023 many missed the 6 in ‘six or more’. Test your conditions with the boundary values before moving on.
3.2.2 Programming concepts

CASE and match: another form of selection

AQA lists IF THEN ELSE and SWITCH/CASE/MATCH as selection statements. Python 3.10 and later has match.

  • match tests one value against several possibilities. case _ catches anything not matched above, like else.
  • AQA’s marking guidance accepts match, but AQA’s own pseudo-code and Python always use IF … ELSE IF. Learn if/elif first; recognise match.
Python matchThe same with if/elif
choice = input("Choose A, B or Q: ")
match choice:
    case "A":
        print("Add a record")
    case "B":
        print("Browse records")
    case "Q":
        print("Quit")
    case _:
        print("Invalid choice")
if choice == "A":
    print("Add a record")
elif choice == "B":
    print("Browse records")
elif choice == "Q":
    print("Quit")
else:
    print("Invalid choice")
match … case
Python 3.10+ selection that compares one value with several cases; case _ matches anything else.