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.
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:
a ← 5 a ← a * 2 DISPLAY(a)
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:
t ← p * q + p * q * 0.07
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
| Operator | Meaning | Example | Value |
|---|---|---|---|
a + b | addition | 17 + 5 | 22 |
a - b | subtraction | 17 - 5 | 12 |
a * b | multiplication | 17 * 5 | 85 |
a / b | division, keeping any decimal part | 17 / 5 | 3.4 |
a MOD b | the remainder when a is divided by b | 17 MOD 5 | 2 |
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.
5What MOD is for
Many everyday questions are really “what is left over?” questions, which makes MOD one of the most useful operators:
| Expression | Means | Run with |
|---|---|---|
n MOD 2 = 0 | n is even | with n ← 14: true |
n MOD 10 | the last digit of n | with n ← 2026: 6 |
n MOD k = 0 | n is a multiple of k | with n ← 35, k ← 7: true |
(h + 5) MOD 24 | the hour 5 hours after h on a 24-hour clock | with 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.
*, /, and MOD from left to right, then + and - from left to right. Every expression ends as a single value.| Expression | How | Value |
|---|---|---|
13 MOD 4 | 13 is 3 groups of 4, with 1 left | 1 |
4 MOD 13 | no full group of 13 fits, so all 4 are left | 4 |
12 MOD 6 | 6 goes in evenly, nothing left | 0 |
0 MOD 5 | nothing to share, nothing left | 0 |
15 / 4 | division keeps the decimal part | 3.75 |
15 - 3 * 2 MOD 4 | 3 * 2 is 6, then 6 MOD 4 is 2, then 15 - 2 | 13 |
(15 - 3) * 2 MOD 4 | 12, then 24, then 24 MOD 4 | 0 |
2 + 3 * 4 | multiply before adding | 14 |
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
MOD in the middle
Evaluate 10 + 8 MOD 3 * 4.
- 1No parentheses.
MODand*share a level, so go left to right:8 MOD 3first. - 2
8 MOD 3is 2 (two groups of 3 make 6, with 2 left). - 3Then
2 * 4is 8, and last10 + 8.
18Three quick ones
Evaluate 29 MOD 7, 7 MOD 29, and 29 / 7.
- 129 is 4 groups of 7 (28) with 1 left, so
29 MOD 7is 1. - 2No group of 29 fits in 7, so
7 MOD 29is 7. - 3
29 / 7keeps the decimal: about 4.14.
Sequence matters
What is displayed?
x ← 4 y ← x + 1 x ← y * 2 DISPLAY(x) DISPLAY(y)
- 1Line 1:
xis 4. - 2Line 2:
yis 4 + 1, which is 5. - 3Line 3:
xis 5 × 2, which is 10.yis not changed.
10 then 5.Minutes into hours
Write statements that turn 135 minutes into whole hours and leftover minutes.
- 1The leftover minutes are what remains after taking out full groups of 60:
minutes MOD 60. - 2Take 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)
2 and 15: 2 hours and 15 minutes.8Common mistakes
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.
17 / 5 is 3.4. To get the whole number of groups, subtract the remainder first, as in the minutes example.
3 MOD 7 is 3, not 0 and not 4.
Statements run top to bottom. A line that reads a variable has to come after the line that assigns it.
“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.