The Stacks
BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.3

3.3 Mathematical Expressions

Arithmetic in pseudocode follows normal order of operations, with one operator — MOD — that shows up constantly.

What you need to know

  • Operators: +, -, *, /, and MOD. Standard precedence applies: parentheses first, then * / MOD left to right, then + - left to right.
  • a MOD b is the remainder when a is divided by b. 17 MOD 5 = 2, 20 MOD 4 = 0, 3 MOD 10 = 3 (if a < b, the result is a).
  • MOD uses: n MOD 2 = 0 tests for even; n MOD 10 gets the last digit; i MOD LENGTH(list) wraps an index around; seconds MOD 60 converts to minutes and seconds.
  • / in AP pseudocode is regular division that can produce decimals (7 / 2 = 3.5). There is no integer-division operator on the reference sheet.
  • A sequence of statements executes in order, one after another. The order changes the result: x ← x * 2 then x ← x + 1 is not the same as the reverse.
  • Arithmetic on values of the wrong type (e.g., adding a number to a string) is an error in most languages.

Worked example

a ← 17
b ← a MOD 5
c ← a / 5
d ← (a + 3) MOD 4
DISPLAY(b)
DISPLAY(c)
DISPLAY(d)

b = 17 MOD 5 = 2. c = 17 / 5 = 3.4. d = 20 MOD 4 = 0. Output: 2 3.4 0.

Exam tip: Whenever you see MOD, ask "what's the remainder?" and do the long division. For expressions mixing MOD with other operators, put parentheses around the MOD part mentally — MOD has the same precedence as multiplication, so a + b MOD c computes the MOD first.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • MOD's precedence is the same as multiplication and division — higher than addition and subtraction. 10 + 7 MOD 3 is 10 + 1 = 11, not 17 MOD 3 = 2. When operators are the same precedence, evaluate left to right.
  • MOD for wraparound: if you're cycling through positions 1 to n and want to go from n back to 1, the formula is (i MOD n) + 1. Try it: i = n gives (0) + 1 = 1. i = 1 gives 2. This shows up in circular list and clock problems.
  • MOD for digit extraction: n MOD 10 is the last digit; n MOD 100 is the last two digits. To get other digits you'd need division, and the exam may define an integer-division procedure for that purpose within a question.
  • MOD with a smaller left operand: 3 MOD 7 is 3 — 7 goes in zero times, remainder 3. Students sometimes answer 0 or 4. The remainder of a small number divided by a bigger one is the small number itself.
  • AP pseudocode's / is real division: 7 / 2 is 3.5. Unlike Java or Python 2, there's no integer division operator on the reference sheet. If a question needs integer division, it will define a procedure or use MOD to get the same effect.
  • Expressions can nest: ((a + b) * c) MOD d. Work inside-out, innermost parentheses first.

Mistakes that cost points

  • Applying MOD last. a + b MOD c means a + (b MOD c). Students compute (a + b) MOD c. Same precedence as *, so it goes before +.
  • Returning the quotient instead of the remainder. 17 MOD 5 is 2 (remainder), not 3 (quotient). MOD is always the leftover.
  • Assuming / truncates. It doesn't in AP pseudocode. 9 / 2 is 4.5.
  • Ignoring statement order. x ← x + 1 then x ← x * 2 is not the same as the reverse. Trace in order.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 What is the value of the expression 25 MOD 7?
  1. A 3
  2. B 4
  3. C 18
  4. D 3.57
Show answer

Answer: B. 7 goes into 25 three times (21), leaving a remainder of 4.

Q2 Which of the following expressions evaluates to true exactly when the variable n holds an odd integer?
  1. A n MOD 2 = 0
  2. B n MOD 2 = 1
  3. C n / 2 = 1
  4. D n MOD 1 = 0
Show answer

Answer: B. Odd numbers leave a remainder of 1 when divided by 2.

Q3 What is displayed by the following code?
x ← 4
x ← x * 3
x ← x + 2
DISPLAY(x MOD 5)
  1. A 0
  2. B 2
  3. C 4
  4. D 14
Show answer

Answer: C. x becomes 12, then 14. 14 MOD 5 = 4.

Key vocabulary

MOD
the remainder after integer division; a MOD b
Expression
a combination of values, variables, and operators that evaluates to a single value
Sequencing
executing statements in order, one after another