Recap
- An expression (value, variable, operator, or procedure call that returns a value) is evaluated to a single value.
- Operators in the notation:
+,-,*,/,MOD. Assumea ≥ 0andb > 0fora MOD b. - Order: brackets, then
*,/,MOD(same level, left to right), then+,-(same level, left to right). a MOD bis the remainder whenais divided byb:17 MOD 5 = 2.17 / 5is 3.4, not 3.
Key relationships
| Task | Expression | Example |
|---|---|---|
| Even test | n MOD 2 = 0 |
10 MOD 2 = 0 is true |
Divisible by d |
n MOD d = 0 |
21 MOD 7 = 0 is true |
| Last digit | n MOD 10 |
4826 MOD 10 = 6 |
| Wrap on 24-hour clock | (h + k) MOD 24 |
(21 + 8) MOD 24 = 5 |
| Whole hours from minutes | (m - m MOD 60) / 60 |
(135 - 15) / 60 = 2 |
| Smaller than divisor | a MOD b = a when a < b |
3 MOD 7 = 3 |
Assumptions
a MOD bis only used with an integera ≥ 0and an integerb > 0./gives a real-number result (7 / 2is 3.5).- Statements run top to bottom and variables hold their most recent values.
Method for evaluating an expression
- Replace each variable with its current value.
- Scan for brackets and evaluate inside them first.
- Evaluate every
*,/andMODfrom left to right. - Evaluate every
+and-from left to right. - Check the result is sensible: a remainder must be smaller than the divisor.
Mistakes to avoid
- Treating MOD as lower precedence than
*. - Giving the quotient instead of the remainder.
- Rounding
17 / 5to 3. - Doing
+before*, or going right to left on equal precedence. - Expecting
a MOD bto reachb.
Worked mini-example
x ← 14
y ← x MOD 5 + x / 2 * 3
DISPLAY(y)
Output: 25
14 MOD 5 = 4; 14 / 2 = 7, 7 * 3 = 21; 4 + 21 = 25.
Quick self-check
- Evaluate
8 + 6 MOD 4 * 3. - Which expression tests whether
nis a multiple of 5? - A clock shows hour
hon a 12-hour dial numbered 0 to 11. Write the hour afterkmore hours.
Answers
6 MOD 4 = 2,2 * 3 = 6,8 + 6 = 14.n MOD 5 = 0.(h + k) MOD 12.
Where marks are won and lost
Expect short evaluation tasks: work out the value of an expression or the value left in a variable after a few assignments. Marks go to candidates who respect precedence (MOD with * and /) and the left-to-right rule. Another favourite is a “which expression” question: choose the expression that gives the last digit, tests divisibility, or wraps a value to a clock. Practise the patterns in the key-relationships table until you can write them without looking.
Back to the full study guide for worked examples and explanations.