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:
+,-,*,/, andMOD. Standard precedence applies: parentheses first, then*/MODleft to right, then+-left to right. a MOD bis the remainder whenais divided byb.17 MOD 5= 2,20 MOD 4= 0,3 MOD 10= 3 (if a < b, the result is a).- MOD uses:
n MOD 2 = 0tests for even;n MOD 10gets the last digit;i MOD LENGTH(list)wraps an index around;seconds MOD 60converts 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 * 2thenx ← x + 1is 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 3is10 + 1 = 11, not17 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 10is the last digit;n MOD 100is 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 7is 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 / 2is 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 cmeansa + (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 5is 2 (remainder), not 3 (quotient). MOD is always the leftover. - Assuming / truncates. It doesn't in AP pseudocode.
9 / 2is 4.5. - Ignoring statement order.
x ← x + 1thenx ← x * 2is 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?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?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)
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