3.3 Mathematical expressionsArithmetic, order, and the remainder operator

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.
| Expression | Meaning | Example | Value |
|---|---|---|---|
| a + b | Add | 7 + 3 | 10 |
| a - b | Subtract | 7 - 3 | 4 |
| a * b | Multiply | 7 * 3 | 21 |
| a / b | Divide | 7 / 2 | 3.5 |
| a MOD b | Remainder of a ÷ b | 7 MOD 3 | 1 |
?Wait, what's the order of operations again?
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.
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)
- line 2 135 - 15 = 120, and 120 / 60 = 2
- line 3 The remainder: 15 minutes
?Wait, why not just compute 135 / 60?
I thought about it. Show me
- 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. - Then 12 MOD 5 = 2, since 5 goes into 12 twice with 2 left.
- Now only + and - remain, left to right: 20 - 2 = 18, then 18 + 1 = 19.
Answer: 19
Stuck? Get a hint
- Which operators have the highest priority here?
- 3 * 4 comes first, then the MOD.
- After that, 20 - 2 + 1, left to right.
- An / 6 = 0
- Bn MOD 6 = 0
- Cn MOD 6 = 6
- 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.
- A3
- B12
- C10
- 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.