AP Computer Science Principles · Unit 3 lecture · Part 2 of 14 · 8 min

3.3 Mathematical expressionsArithmetic, order, and the remainder operator

Mr. Thompkins
Programs do a lot of arithmetic, and the exam uses exactly the operators you know, plus one that might be new: MOD. Master it and a whole family of questions becomes easy.

An algorithm is a finite set of instructions that accomplishes a specific task. Every algorithm can be built from just three ingredients: sequencing, selection and iteration. Sequencing means running the steps in the order they are written, which is what you did in the last section. Each line of code that expresses an action is a code statement.

An expression is anything that produces a single value: a value like 7, a variable like score, an operator combining them like score + 4, or a procedure call that returns a value. The exam's arithmetic operators are +, -, * (multiply), / (divide) and MOD.

ExpressionMeaningExampleValue
a + bAdd7 + 310
a - bSubtract7 - 34
a * bMultiply7 * 321
a / bDivide7 / 23.5
a MOD bRemainder of a ÷ b7 MOD 31
The arithmetic operators on the reference sheet. / gives an exact answer, decimals included.
?Wait, what's the order of operations again?
Parentheses first, then multiplication and division from left to right, then addition and subtraction from left to right. On the exam, MOD has the same priority as * and /, so it happens before + and -.

MOD, the remainder

a MOD b gives the remainder when a is divided by b. 17 MOD 5 is 2, because 5 goes into 17 three times with 2 left over. The reference sheet assumes a is a whole number at least 0 and b a whole number greater than 0. Two quick facts: if a is smaller than b, the remainder is a itself (3 MOD 5 is 3), and 0 MOD b is 0.

17 eggs into cartons of 53 full cartons2 left over17 MOD 5 = 2MOD gives the remainder, not the 3n MOD 2 = 0 means n is even
MOD is the leftover. 17 eggs fill 3 cartons of 5 with 2 left: 17 MOD 5 = 2.

MOD is everywhere in programs. n MOD 2 = 0 tests whether n is even. n MOD 10 gives the last digit of n. n MOD 7 cycles 0, 1, 2, ..., 6, 0, 1, ..., which is perfect for days of the week. And MOD together with subtraction and division splits a quantity into whole units and leftovers, like minutes into hours and minutes.

1minutes ← 1352hours ← (minutes - minutes MOD 60) / 6013left ← minutes MOD 6024DISPLAY(hours)5DISPLAY(left)
  1. line 2 135 - 15 = 120, and 120 / 60 = 2
  2. line 3 The remainder: 15 minutes
output › 2 15
Splitting 135 minutes into whole hours and leftover minutes.
?Wait, why not just compute 135 / 60?
Because / gives the exact answer, 2.25, not "2 whole hours". Subtracting the remainder first (135 - 15 = 120) makes the division come out to a whole number, 2.
Pause and think
What is 7 + 10 MOD 4 * 2? Decide the order before you compute.
I thought about it. Show me
MOD and * have the same priority and go left to right, before +. So 10 MOD 4 = 2, then 2 * 2 = 4, then 7 + 4 = 11. Adding first would give (17 MOD 4) * 2 = 2, the trap.
Evaluate an expression
What is the value of 20 - 3 * 4 MOD 5 + 1?
  1. Find the highest-priority operators: * and MOD. They go left to right: 3 * 4 = 12 first.
    Why?
    MOD has the same priority as * and /. Among equals, work left to right.
  2. Then 12 MOD 5 = 2, since 5 goes into 12 twice with 2 left.
  3. Now only + and - remain, left to right: 20 - 2 = 18, then 18 + 1 = 19.

Answer: 19

Stuck? Get a hint
  1. Which operators have the highest priority here?
  2. 3 * 4 comes first, then the MOD.
  3. After that, 20 - 2 + 1, left to right.
Check yourself
A program needs to know whether the whole number n is a multiple of 6. Which expression is true exactly when it is?
  1. An / 6 = 0
  2. Bn MOD 6 = 0
  3. Cn MOD 6 = 6
  4. D6 MOD n = 0
Show the answer

B. A multiple of 6 leaves no remainder when divided by 6, so n MOD 6 = 0. n MOD 6 can never equal 6 (remainders go from 0 to 5), and 6 MOD n reverses the division.

Check yourself
What is 23 MOD 4 + 9 MOD 10?
  1. A3
  2. B12
  3. C10
  4. D4
Show the answer

B. 23 MOD 4 = 3 (4 × 5 = 20, 3 left) and 9 MOD 10 = 9 (9 is smaller than 10, so it is its own remainder). 3 + 9 = 12. Choice C uses 0 for 9 MOD 10, the classic slip.

  • An algorithm is a finite set of steps. Every algorithm uses sequencing, selection and iteration.
  • An expression produces one value. Operators: +, -, *, /, MOD.
  • a MOD b is the remainder of a ÷ b. Same priority as * and /, before + and -.
  • n MOD 2 = 0: even. If a < b, a MOD b is a.