Article
Arithmetic and MOD
A shop till adds up your prices. A game works out how far your character moved. A clock app wraps from 11 back to 12. All of them use the same small set of operators. An operator is a symbol that does a calculation, like +. This article covers every one the exam uses, including a strange one called MOD.
The arithmetic operators
An expression is a piece of code that the computer works out to get a single value, for example 2 + 3. The values it works on are called operands. Here are the five operators of the exam, next to their Python versions:
| Operation | AP pseudocode | Python | Example | Result |
|---|---|---|---|---|
| Add | a + b | a + b | 17 + 5 | 22 |
| Subtract | a - b | a - b | 17 - 5 | 12 |
| Multiply | a * b | a * b | 17 * 5 | 85 |
| Divide | a / b | a / b | 17 / 5 | 3.4 |
| Remainder | a MOD b | a % b | 17 MOD 5 | 2 |
In AP pseudocode / is real division, so 17 / 5 is 3.4 and not 3. Run them yourself:
Python has one more division, //, which keeps only the whole number: 17 // 5 is 3. It is not in AP pseudocode, so the exam never uses it. Python's / has one more small difference: it always shows a decimal point, so 10 / 2 is 5.0 in Python, while the exam says 10 / 2 is 5. The value is the same.
MOD: the remainder
When you divide 17 sweets between 5 students, each gets 3 and 2 are left over. The leftover is the remainder. The MOD operator gives exactly that:
17 ÷ 5 = 3 remainder 2 so 17 MOD 5 = 2Read a MOD b as "the remainder when a is divided by b". Try these in your head, then check:
DISPLAY(17 MOD 5) DISPLAY(20 MOD 5) DISPLAY(3 MOD 5)
The program shows 2, then 0, then 3. Look at the last one: 5 goes into 3 zero times, so all of 3 is left over. Here is the pattern for MOD 3. It never reaches 3:
n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
n MOD 3 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 |
a MOD b is always between 0 and b - 1. On the exam, a is zero or more and b is more than zero. MOD is useful because of the patterns it gives:
| Use | Expression | What it tells you |
|---|---|---|
| Even or odd | n MOD 2 | 0 means even, 1 means odd |
| Divides exactly | n MOD 5 is 0 | n is a multiple of 5 |
| Last digit | n MOD 10 | 47 MOD 10 is 7 |
| Wrap around a clock | (9 + 5) MOD 12 | 5 hours after 9 o'clock is 2 o'clock |
Check your understanding
What is the value of 29 MOD 7?
Order of operations
What is 2 + 3 * 4? The computer does not simply go from left to right. It follows the rules you know from maths:
- Anything in brackets
( )is worked out first. - Then
*,/andMOD. These three are of equal rank. - Then
+and-. These two are also of equal rank. - When operators have the same rank, work left to right.
| Expression | How it is worked out | Result |
|---|---|---|
2 + 3 * 4 | 3 * 4 is 12 first, then 2 + 12 | 14 |
(2 + 3) * 4 | brackets first: 5 * 4 | 20 |
20 - 6 - 2 | left to right: 14 - 2 | 12 |
10 - 2 * 3 | 2 * 3 is 6, then 10 - 6 | 4 |
7 + 10 MOD 4 | 10 MOD 4 is 2, then 7 + 2 | 9 |
12 / 4 * 3 | left to right: 3 * 3 | 9 |
Check your understanding
What is the value of 10 - 2 * 3?
Worked example: sweets and leftovers
A teacher has 17 sweets for 5 students. Find what each student gets if the sweets are shared exactly, and how many are left if every student gets a whole number.
sweets ← 17 students ← 5 share ← sweets / students left ← sweets MOD students DISPLAY(share) DISPLAY(left)
| After this statement | sweets | students | share | left |
|---|---|---|---|---|
sweets ← 17 | 17 | |||
students ← 5 | 17 | 5 | ||
share ← sweets / students | 17 | 5 | 3.4 | |
left ← sweets MOD students | 17 | 5 | 3.4 | 2 |
The program shows 3.4 and 2. If the sweets must be handed out whole, each student gets 3 and 2 remain. MOD finds the 2.
Now a longer expression: 3 + 12 MOD 5 * 2. Take it one operator at a time:
MODand*rank higher than+, and they are equal to each other, so go left to right. First12 MOD 5is 2. The expression becomes3 + 2 * 2.- Next
2 * 2is 4. The expression becomes3 + 4. - Last
3 + 4is 7.
Common mistakes
- Reading MOD as the answer of a division.
17 MOD 5is the leftover, 2. It is not 3, and it is not 3.4. - Using whole-number division in AP pseudocode.
17 / 5is3.4, not3. The exam has no//. - Going strictly left to right.
2 + 3 * 4is 14, not 20. Do*,/andMODbefore+and-. - Forgetting that equal ranks go left to right.
20 - 6 - 2is 12, not 16. - Getting
a MOD bwrong whenais smaller thanb.3 MOD 5is 3, because nothing can be taken out. - Dividing by zero.
5 / 0and5 MOD 0have no answer. The program stops with an error.
On the exam
Expect to work out expressions that mix + - * / MOD and brackets, to say what DISPLAY shows, and to use MOD for "is it even?", "does it divide exactly?" and "wrap around" questions. Do it in small steps and write down each result. In MOD questions, always find the remainder, never the quotient.
Key words
Operator: a symbol that does a calculation, such as + or MOD.
Expression: code that is worked out to give one value.
Operand: a value an operator works on.
MOD: the operator that gives the remainder after a division.
Remainder: what is left after dividing as many whole times as possible.
Order of operations: the rules for which operators are done first.