AP® Computer Science Principles pseudocode has five arithmetic operators, +, -, *, / and MOD, where a MOD b gives the remainder when a is divided by b. The operators *, / and MOD are applied before + and -, and operators at the same level go left to right. A call to RANDOM(a, b) gives a whole number from a to b, including both ends.
Quick check
After x ← 7 and then x ← x + 3, what value does x hold?Show answerHide answer
It holds 10. The right side,
x + 3, is worked out with the old value 7, and the result replaces it.What is the remainder when 17 is divided by 5?Show answerHide answer
The remainder is 2: 5 goes into 17 three times (15), and 17 − 15 = 2.
Know these already? Jump to the practice questions
What you'll be able to do
- Evaluate an expression that mixes
+,-,*andMOD, in the right order. - Calculate
a MOD band use it to test for even numbers, find a last digit or split a total. - Explain how
MODmakes a value cycle back to the start. - State every value that
RANDOM(a, b), or an expression built on it, can produce.
Key terms
Expression
A combination of values, variables and operators that evaluates to a single value.
- In plain English
- A calculation that boils down to one answer.
- Don't write
- "An expression stores a value in a variable."
MOD
The operator that gives the remainder when its left value is divided by its right value.
- In plain English
- What is left over after sharing out equally.
- Don't write
- "
17 MOD 5is 3." That is how many times 5 fits, not the remainder.
Order of operations
The rule for which operator is applied first: parentheses, then *, / and MOD from left to right, then + and - from left to right.
- In plain English
- Multiply-type operators before add-type ones.
- Don't write
- "
MODis always worked out last."
How is an arithmetic expression evaluated?
On the exam reference sheet: the five operators +, -, *, / and MOD, and the assignment a ← expression, which evaluates the right side first and then stores the result in a.
An expression always reduces to one value. In total ← price * 3 + 2 with price holding 4, the right side is 14, and only then is 14 stored in total. The variable never holds the expression itself.
This note uses / only where the division is exact, such as 20 / 4, which is 5. When you need a whole-number remainder, use MOD.
Check yourself
Show answerHide answer
It holds 8. 4 * 3 is 12 first, and 20 − 12 = 8.
Which operator goes first?
On the exam reference sheet: the usual order of operations, with MOD at the same level as * and /.
- Anything in parentheses.
*,/andMOD, working from left to right.+and-, working from left to right.
In 9 MOD 4 * 3, both operators share a level, so you go left to right: 9 MOD 4 = 1, then 1 × 3 = 3.
Key idea
MOD is a multiply-type operator: it is applied before + and - unless parentheses say otherwise.
Check yourself
Show answerHide answer
It is 6. 6 MOD 4 is 2, then 2 × 2 = 4, then 10 − 4 = 6.
What can MOD tell you about a number?
a MOD b is the remainder after taking out as many whole copies of b as fit into a. This note uses MOD only with a ≥ 0 and b > 0, so the result is always from 0 to b − 1.
| Use | Expression | Example |
|---|---|---|
| Even test | n MOD 2 = 0 | 38 MOD 2 is 0, so 38 is even |
| Odd test | n MOD 2 = 1 | 15 MOD 2 is 1, so 15 is odd |
| Divisible by k | n MOD k = 0 | 42 MOD 7 is 0 |
| Last digit | n MOD 10 | 4071 MOD 10 is 1 |
| Smaller first value | a MOD b with a < b | 3 MOD 5 is 3 |
Learn this: the patterns in this table.
Check yourself
Show answerHide answer
38 MOD 10 is 8, the last digit, and 38 MOD 2 is 0, so 38 is even.
How does MOD make values cycle?
Because x MOD n can only be 0 to n − 1, adding to a value and then taking MOD n wraps it back to 0 after n − 1.
For days numbered 0 to 6, day ← (day + 1) MOD 7 moves to the next day: 6 becomes (6 + 1) MOD 7 = 0, the start of the week. The parentheses matter, because without them MOD would apply to the 1 alone.
If the values run from 1 to n instead, shift down, wrap, then shift back up: x ← x MOD n + 1 sends n to 1 and every other value to the next one.
Check yourself
Show answerHide answer
It becomes 2, because 26 MOD 24 = 2.
What values can RANDOM(a, b) produce?
On the exam reference sheet: RANDOM(a, b) gives a random whole number from a to b, including a and b, so there are b − a + 1 possible values. RANDOM(3, 7) can give 3, 4, 5, 6 or 7: five values.
When RANDOM sits inside an expression, list its possible values first, then push each one through the rest of the expression. RANDOM(1, 3) * 4 can only be 4, 8 or 12, so 6 can never appear.
Check yourself
Show answerHide answer
Five: 7 − 3 + 1 = 5.
Worked example 1: Trace the order of operations
Question: What is displayed when this code segment runs?
a ← 4
b ← 10
c ← a + b MOD 3 * 2
d ← (a + b) MOD 3 * 2
DISPLAY(c)
DISPLAY(d)
What tells you the method
Two expressions with the same numbers but different parentheses, so apply the order of operations line by line.
Step 1: Work out
c.b MOD 3is 1, then 1 × 2 = 2, then 4 + 2 = 6.WhyMODand*come before+, and go left to right.Step 2: Work out
d.(a + b)is 14,14 MOD 3is 2, then 2 × 2 = 4.WhyParentheses go first, so the addition happens before
MOD.Line a b c d 1–2 4 10 3 4 10 6 4 4 10 6 4 Step 3: Write the display.
Each
DISPLAYshows its value followed by a space.WhyThat puts both values on one line.
Check it
14 = 3 × 4 + 2, so the remainder in Step 2 is 2.
Answer
6 4
Worked example 2: Design a minutes converter
Question: A program stores a length of time in minutes in total. Design a code segment that displays the whole hours, then the minutes left over. What does it display when total is 135?
What tells you the method
Splitting a total into whole groups and a leftover is a MOD job.
Step 1: State the input, output and steps.
Input:
total. Output: hours, then minutes. Steps: the leftover istotal MOD 60; subtracting it leaves an exact multiple of 60, which you divide by 60.WhyThe subtraction makes the division exact, so
/has no remainder to deal with.Step 2: Write the code.
mins ← total MOD 60 hours ← (total - mins) / 60 DISPLAY(hours) DISPLAY(mins)Whyminsmust be found first becausehoursuses it.Step 3: Trace it with
total= 135.minsis 15, andhoursis 120 / 60 = 2.WhyTracing a design on a real input tests it.
Check it
At the boundary total = 60, mins is 0 and hours is 1, which displays 1 0, as it should.
Answer
2 15
Worked example 3: Wrap a seat number
Question: Seats are numbered 1, 2, 3 and 4 around a table. After seat 4, play returns to seat 1. Which statement moves seat to the next seat for every starting value?
- A)
seat ← (seat + 1) MOD 4 - B)
seat ← seat MOD 4 + 1 - C)
seat ← seat + 1 MOD 4 - D)
seat ← (seat + 1) MOD 5
What tells you the method
A "for every value" question: test every option on all four seats, especially the boundary seat 4.
Step 1: Test seat 3 and seat 4 in each option.
A gives 0 for seat 3. C gives 5 for seat 4. D gives 0 for seat 4. B gives 4 and then 1.
WhySeats 3 and 4 are where the wrap happens or should not happen.
Step 2: Test B on seats 1 and 2.
They become 2 and 3.
WhyAn option must work for every value, not only the boundary.
Check it
B maps 1, 2, 3, 4 to 2, 3, 4, 1, so every seat moves on by one.
Answer
B
Your turn
Question: List every value that RANDOM(0, 3) * 5 + 10 can produce.
Step 1: List what
RANDOM(0, 3)gives.It gives 0, 1, 2 or 3, including both ends.
Step 2: Your go: push each value through
* 5 + 10.
Show the full solutionHide the solution
Step 2. 0 × 5 + 10 = 10, 1 × 5 + 10 = 15, 2 × 5 + 10 = 20 and 3 × 5 + 10 = 25.
Answer: 10, 15, 20 and 25.
Traps
Trap
When the first number is smaller, as in 3 MOD 8, the result is 0.
It seems right because 8 does not go into 3 at all. But that means zero copies of 8 are taken out, so the whole 3 is left over: 3 MOD 8 is 3.
What is 3 MOD 8?
It is 3. No whole 8 fits into 3, so all of it is the remainder.
Trap
Operations run left to right, so 2 + 6 MOD 4 is 8 MOD 4.
It seems right because you read the line from left to right. But MOD outranks +, so 6 MOD 4 is 2 first, and the value is 4.
What is the value of 2 + 6 MOD 4?
It is 4: 2 + 2.
Trap
RANDOM(1, 6) stops before 6.
It seems right because the second number looks like a stopping point. But both ends are included, so 6 is one of the six possible values.
Can RANDOM(1, 6) produce 6?
Yes. RANDOM(a, b) includes both a and b.
What's examined
On the AP® Computer Science Principles exam, this belongs to Big Idea 3: Algorithms and Programming. Multiple-choice questions ask what a code segment displays, which expression does a job, such as an even test, or which values RANDOM can produce. The operators and RANDOM are on the exam reference sheet; the MOD patterns are Learn this.
Practice questions
- Recall What is the value of
23 MOD 4?
- A) 5
- B) 4
- C) 3
- D) 20
Show answer
Answer: C. 4 fits into 23 five times (20), leaving 3.
Why the others are wrong:
- A is how many times 4 fits, not the remainder.
- B is
4 MOD 23, with the values swapped. - D is the part taken out, not the part left over.
- Apply What value does
xhold afterx ← 3 + 12 MOD 5 * 2?
Show answer
Answer: 7
Marking points:
12 MOD 5is 2, then 2 × 2 = 4.- 3 + 4 = 7. Adding first,
(3 + 12) MOD 5 * 2, would give 0.
- Recall Which condition is true exactly when the whole number
nis even?
- A)
n MOD 2 = 1 - B)
2 MOD n = 0 - C)
n MOD 2 = 2 - D)
n MOD 2 = 0
Show answer
Answer: D. An even number leaves remainder 0 when divided by 2.
Why the others are wrong:
- A is true for odd numbers.
- B swaps the values: it is true only when
nis 1 or 2. - C can never be true, because a remainder is always less than the number you divide by.
- Apply How many different values can
RANDOM(5, 12)produce?
Show answer
Answer: 8
Marking points:
- Both ends are included: 12 − 5 + 1 = 8 values, from 5 to 12.
- Exam-style A program runs
r ← RANDOM(1, 5) * 2 + 1. Which list shows every valuercan hold?
- A) 3, 5, 7, 9, 11
- B) 2, 4, 6, 8, 10
- C) 3, 5, 7, 9
- D) 1, 3, 5, 7, 9, 11
Show answer
Answer: A. RANDOM(1, 5) gives 1 to 5, and doubling then adding 1 gives 3, 5, 7, 9 and 11.
Why the others are wrong:
- B forgets the
+ 1. - C leaves out 5 as a possible random value.
- D starts the random values at 0 instead of 1.
- Exam-style Days are numbered 0 to 6. What is displayed when this code segment runs?
day ← 5
day ← (day + 4) MOD 7
DISPLAY(day)
day ← (day + 4) MOD 7
DISPLAY(day)
Show answer
Answer: 2 6
Marking points:
- (5 + 4) MOD 7 = 9 MOD 7 = 2.
- (2 + 4) MOD 7 = 6 MOD 7 = 6.
- Exam-style This code segment should display the tens digit and then the ones digit of the two-digit number in
n, so it should display5 8. Give the number of the line that stops it working.
1: n ← 58
2: ones ← n MOD 100
3: tens ← (n - ones) / 10
4: DISPLAY(tens)
5: DISPLAY(ones)
Show answer
Answer: Line 2.
Marking points:
58 MOD 100is 58, soonesis 58,tensis 0, and the code displays0 58.ones ← n MOD 10gives 8, thentensis 50 / 10 = 5, which displays5 8.
- Apply What value does
xhold after these two statements?
x ← 7
x ← x * 2 MOD 5 + x
Show answer
Answer: 11
Marking points:
*andMODshare a level, so left to right: 7 × 2 = 14, then14 MOD 5is 4.- 4 + 7 = 11, using the old value of
x.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- An expression is evaluated to one value before
←stores it. - Parentheses first, then
*,/andMODleft to right, then+and-. a MOD bis a remainder from 0 tob− 1;n MOD 2 = 0tests for even.(x + k) MOD nwraps values around 0 to n − 1.RANDOM(a, b)includes both ends, givingb − a + 1values.