AP® Computer Science Principles review sheet from Aim for Five (aimforfive.com/csp/units/3/3-3)
Unit 3 · Topic 3.3
3.3 Mathematical Expressions
An algorithm is a step-by-step process, and sequencing means doing those steps in order. This topic covers algorithms and expressions, the exam's arithmetic operators, especially MOD, and the order of operations you need to evaluate them correctly.
Key terms
- algorithm
- sequencing
- expression
- arithmetic operators
- MOD (remainder)
- order of operations
Algorithms and sequencing
An algorithm is a list of steps, with a definite end, for getting a particular job done. A recipe, directions to a friend's house and the steps for long division are all algorithms.
Algorithms can be written in many ways: plain English, a flowchart or other diagram, pseudocode like the exam's, or a real programming language. A program is an algorithm written in a programming language so a computer can run it.
Every algorithm can be built from just three structures: sequencing (steps in order), selection (choosing which steps to run, covered in 3.6) and iteration (repeating steps, covered in 3.8).
Sequencing means each statement runs in the order it's written, one after another. A code statement is one instruction, like an assignment or a DISPLAY. Changing the order of statements can change the result, so order matters. Clear, readable code makes an algorithm easier to check.
Expressions
An expression is anything that works out to a single value. It can be a value (7), a variable (price), an operator applied to values (price * 2) or a call to a procedure that returns a value (LENGTH(list)).
The exam's arithmetic operators are +, -, *, / and MOD.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Add | 7 + 2 | 9 |
| - | Subtract | 7 - 2 | 5 |
| * | Multiply | 7 * 2 | 14 |
| / | Divide (ordinary division) | 7 / 2 | 3.5 |
| MOD | Remainder after division | 7 MOD 2 | 1 |
MOD, the remainder operator
a MOD b is the remainder when a is divided by b. 20 MOD 6 is 2, because 6 goes into 20 three times (18) with 2 left over. On the exam, a is a whole number 0 or greater and b is a whole number greater than 0.
If a is smaller than b, the answer is just a: 3 MOD 5 is 3, because 5 goes into 3 zero times with 3 left over.
MOD has handy uses:
- Even or odd:
n MOD 2is 0 for even numbers and 1 for odd numbers. - Divisibility:
a MOD b = 0is true exactly when b divides a evenly. - Last digit:
n MOD 10gives the ones digit, so 347 MOD 10 is 7. - Wrapping around:
(day + 1) MOD 7cycles through 0 to 6, like days of the week.
Order of operations
The usual math order applies: parentheses first, then *, / and MOD (which all rank the same and go left to right), then + and - (left to right).
Remember that / is ordinary division on the exam, not whole-number division. 10 / 4 is 2.5, while 10 MOD 4 is 2. Some real languages divide whole numbers differently, but the exam's pseudocode doesn't.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Order of operations with MOD
What does this display?
DISPLAY(3 + 14 MOD 4 * 2)Show the solutionHide the solution
- Step 1: MOD and * have the same rank and come before +. Work left to right among them.
- Step 2: 14 MOD 4: 4 goes into 14 three times (12), remainder 2.
- Step 3: Then 2 * 2 = 4.
- Step 4: Finally 3 + 4 = 7.
Answer: 7
- Example 2
Sequencing with MOD
This segment converts a number of minutes into hours and minutes. What does it display?
totalMinutes ← 135 minutes ← totalMinutes MOD 60 hours ← (totalMinutes - minutes) / 60 DISPLAY(hours) DISPLAY(minutes)Show the solutionHide the solution
- Step 1: Line 1: totalMinutes = 135.
- Step 2: Line 2: 135 MOD 60. 60 goes into 135 twice (120), remainder 15. So minutes = 15.
- Step 3: Line 3: (135 - 15) / 60 = 120 / 60 = 2. So hours = 2.
- Step 4: The order matters: line 3 uses minutes, so it must come after line 2.
Answer: 2 15
- Example 3
The small-number trap
What does this display?
DISPLAY(3 MOD 5) DISPLAY(10 / 4) DISPLAY(10 MOD 4)Show the solutionHide the solution
- Step 1: 3 MOD 5: 5 goes into 3 zero times, so the whole 3 is left over. The answer is 3, not 0 and not 2.
- Step 2: 10 / 4 is ordinary division: 2.5.
- Step 3: 10 MOD 4: 4 goes in twice (8), remainder 2.
Answer: 3 2.5 2
Common mistakes
- Treating MOD as division.
20 MOD 6is the remainder, 2, not the quotient, 3. - Saying
3 MOD 5is 0 or 2. When the first number is smaller, the remainder is the first number itself. - Doing + before MOD. MOD ranks with * and /, so
3 + 14 MOD 4is 3 + 2, not 17 MOD 4. - Rounding
/down to a whole number. On the exam, 7 / 2 is 3.5.
On the exam
- Expect short expressions to evaluate and short sequences of assignments to trace. MOD shows up often, especially for checking even numbers or divisibility.
- Some questions describe an algorithm in words and ask which code matches it. Check that the steps happen in the same order.
Connected topics
Videos
Check yourself
4 questions on 3.3 Mathematical Expressions. Pick an answer to see if you got it, and why.
What is displayed when the following code segment is run?
DISPLAY(17 MOD 5 + 12 / 4 * 2)
The variable n holds a positive integer. Which expression evaluates to true exactly when n is a multiple of 3?
total ← 10
total ← total * 2
total ← total + 5
total ← total / 5
DISPLAY(total)What is displayed when the code segment is run?
The programmer swaps lines 2 and 3. What is displayed when the changed code segment is run?
0 of 4 answered