AP Computer Science Principles · Study guide

Mathematical Expressions

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

Mathematical Expressions

What an algorithm is, how statements run in order, and how expressions with +, -, *, /, and MOD are worked out to a single value.

1Algorithms

An algorithm is a finite set of instructions that accomplish a specific task. Finite means it ends. Instructions means each step says what to do well enough to follow without guessing. A specific task means you can tell whether it worked. A recipe and long division are algorithms; “add a reasonable amount” is not a usable step.

The same algorithm can be written many ways: in plain language, as a diagram such as a flowchart, in pseudocode, or in a programming language. Only the programming-language version can be run by a computer. Here is one algorithm written two ways:

In words: take the temperature in Celsius, multiply it by 9, divide by 5, add 32, and report the result.

In the reference language
celsius ← 25
fahrenheit ← celsius * 9 / 5 + 32
DISPLAY(fahrenheit)

With 25 degrees Celsius, it displays 77. Every algorithm, however large, is built from three building blocks: sequencing (steps in order), selection (choosing between steps), and iteration (repeating steps). This guide is about the first one and the arithmetic inside the steps.

2Statements run in order

A code statement is one part of a program that carries out an action, like an assignment or a DISPLAY. Sequencing means the statements are carried out in the order they are written, top to bottom. Change the order and you can change the result:

In this order
a ← 5
a ← a * 2
DISPLAY(a)
First two lines swapped
a ← a * 2
a ← 5
DISPLAY(a)

The first displays 10. The second does not even run: its first line tries to use a before a has a value.

3Expressions

An expression is anything that works out to a value. It can be a value (7), a variable (price), values and variables joined by operators (price * qty), or a procedure call that returns a value (LENGTH(scores)). However long it is, evaluating an expression produces one value, and that single value is what a statement stores or displays.

Clear code splits a long expression into named steps. These two give the same total, 64.2, when the price is 20 and the quantity is 3:

Correct, but hard to check
t ← p * q + p * q * 0.07
Three readable steps
subtotal ← price * qty
tax ← subtotal * 0.07
total ← subtotal + tax

Clarity and readability matter when you write an algorithm. The second version is easier to check and easier to change if the tax rate changes.

4The five arithmetic operators

OperatorMeaningExampleValue
a + baddition17 + 522
a - bsubtraction17 - 512
a * bmultiplication17 * 585
a / bdivision, keeping any decimal part17 / 53.4
a MOD bthe remainder when a is divided by b17 MOD 52

Two details trip people up. First, / keeps the decimal: 17 / 5 is 3.4, not 3. Second, a MOD b is defined for whole numbers with a at least 0 and b greater than 0.

Figure 1. Division answers “how many groups, including the part of a group?” MOD answers “how many are left over?”

5What MOD is for

Many everyday questions are really “what is left over?” questions, which makes MOD one of the most useful operators:

ExpressionMeansRun with
n MOD 2 = 0n is evenwith n ← 14: true
n MOD 10the last digit of nwith n ← 2026: 6
n MOD k = 0n is a multiple of kwith n ← 35, k ← 7: true
(h + 5) MOD 24the hour 5 hours after h on a 24-hour clockwith h ← 22: 3

A smaller number MOD a larger one is the smaller number itself: 4 MOD 13 is 4, because no group of 13 fits and all 4 are left.

6Order of operations

Expressions follow the order of operations from math. MOD has the same precedence as * and /, so when they sit next to each other, work from left to right.

Figure 2. Parentheses first, then *, /, and MOD from left to right, then + and - from left to right. Every expression ends as a single value.
ExpressionHowValue
13 MOD 413 is 3 groups of 4, with 1 left1
4 MOD 13no full group of 13 fits, so all 4 are left4
12 MOD 66 goes in evenly, nothing left0
0 MOD 5nothing to share, nothing left0
15 / 4division keeps the decimal part3.75
15 - 3 * 2 MOD 43 * 2 is 6, then 6 MOD 4 is 2, then 15 - 213
(15 - 3) * 2 MOD 412, then 24, then 24 MOD 40
2 + 3 * 4multiply before adding14
The key idea

Statements run in the order they are written. An expression always evaluates to one value. / keeps decimals, MOD gives the remainder, and MOD sits on the same level as * and /.

7Worked examples

Worked Example 1 Evaluate

MOD in the middle

Evaluate 10 + 8 MOD 3 * 4.

  1. 1
    No parentheses. MOD and * share a level, so go left to right: 8 MOD 3 first.
  2. 2
    8 MOD 3 is 2 (two groups of 3 make 6, with 2 left).
  3. 3
    Then 2 * 4 is 8, and last 10 + 8.
Answer: 18
Worked Example 2 Evaluate

Three quick ones

Evaluate 29 MOD 7, 7 MOD 29, and 29 / 7.

  1. 1
    29 is 4 groups of 7 (28) with 1 left, so 29 MOD 7 is 1.
  2. 2
    No group of 29 fits in 7, so 7 MOD 29 is 7.
  3. 3
    29 / 7 keeps the decimal: about 4.14.
Answer: 1, 7, and about 4.14.
Worked Example 3 Trace

Sequence matters

What is displayed?

x ← 4
y ← x + 1
x ← y * 2
DISPLAY(x)
DISPLAY(y)
  1. 1
    Line 1: x is 4.
  2. 2
    Line 2: y is 4 + 1, which is 5.
  3. 3
    Line 3: x is 5 × 2, which is 10. y is not changed.
Answer: 10 then 5.
Worked Example 4 Write an algorithm

Minutes into hours

Write statements that turn 135 minutes into whole hours and leftover minutes.

  1. 1
    The leftover minutes are what remains after taking out full groups of 60: minutes MOD 60.
  2. 2
    Take those away and divide by 60 to count the full hours.
    minutes ← 135
    leftover ← minutes MOD 60
    hours ← (minutes - leftover) / 60
    DISPLAY(hours)
    DISPLAY(leftover)
Answer: It displays 2 and 15: 2 hours and 15 minutes.

8Common mistakes

Common Mistake 1 · Doing MOD last

MOD is on the same level as * and /. In 10 + 8 MOD 3, the MOD comes before the addition: the value is 12, not 0.

Common Mistake 2 · Dropping the decimal from /

17 / 5 is 3.4. To get the whole number of groups, subtract the remainder first, as in the minutes example.

Common Mistake 3 · Getting small MOD large backward

3 MOD 7 is 3, not 0 and not 4.

Common Mistake 4 · Using a variable before it has a value

Statements run top to bottom. A line that reads a variable has to come after the line that assigns it.

Explain an evaluation like this

“MOD has the same precedence as *, so working left to right, 8 MOD 3 is 2, then 2 * 4 is 8, and finally 10 + 8 is 18.”

Name the order you used and give the value after each step.