01 / 21
{ }
\(x+y\)

Mathematical
Expressions

Learn how programming languages evaluate arithmetic, use operators, and handle the order of operations to build powerful algorithms.

AP Computer Science Principles

What Is a Mathematical Expression?

In programming, a mathematical expression is a combination of values, variables, and operators that the computer evaluates to produce a single result. Just like in algebra, expressions follow specific rules to determine the final value.

The Building Blocks of Expressions

Values
Literal numbers like 42, 3.14, or -7
Variables
Named containers like x, score, or total
Operators
Symbols: + - * / MOD

Why Do Expressions Matter?

Every algorithm relies on mathematical expressions to transform data. Whether you're calculating a game score, converting temperatures, or analyzing data, expressions are the engine that drives computation.

INPUT tempF = 98.6 EXPRESSION
\((tempF - 32) \times \frac{5}{9}\)
OUTPUT tempC = 37.0

The Arithmetic Operators

Operator Symbol Example Result
Addition + 7 + 3 10
Subtraction - 7 - 3 4
Multiplication * 7 * 3 21
Division / 7 / 2 3.5
Modulus MOD 7 MOD 3 1

Order of Operations

Computers follow a strict precedence hierarchy when evaluating expressions, just like PEMDAS in math. Higher-precedence operators are evaluated first.

1
Parentheses: expressions inside ( ) are evaluated first, innermost to outermost.
2
Multiplication, Division, MOD: evaluated left to right, all at equal precedence.
3
Addition, Subtraction: evaluated left to right after all higher-precedence operators.

Visualizing Precedence

\(4 + 3 \times (10 - 2) / 4\)
STEP 1: Parentheses
\(4 + 3 \times {\color{#818cf8}(10 - 2)} / 4\)
\(\rightarrow 4 + 3 \times 8 / 4\)
STEP 2: Multiplication (left to right)
\(4 + {\color{#fbbf24}3 \times 8} / 4\)
\(\rightarrow 4 + 24 / 4\)
STEP 3: Division (left to right)
\(4 + {\color{#f87171}24 / 4}\)
\(\rightarrow 4 + 6\)
STEP 4: Addition
\({\color{#4ade80}4 + 6}\)
\(= 10\)

Integer vs. Float Division

Integer Division
When both operands are integers, some languages truncate the result. 7 / 2 might give 3, not 3.5. The AP Exam uses real division by default.
Real (Float) Division
The AP pseudocode / always returns the full decimal result. 7 / 2 gives 3.5.

The MOD Operator Deep Dive

\(17 \text{ MOD } 5 = ?\)
Group 1 (5) Group 2 (5) Group 3 (5) Remainder: 2
\(17 \text{ MOD } 5 = 2\)

How to Evaluate Any Expression

1
Substitute variables. Replace each variable name with its current value.
2
Solve parentheses first. Work from the innermost ( ) outward.
3
Apply *, /, MOD left to right. These have equal precedence; process whichever comes first.
4
Apply +, - left to right. After all higher-precedence operators are done.
5
Assign the result. Store the final value in the target variable.

Example 1: Simple Expression

a ← 10 b ← 3 result ← a + b * 2 - 1
1
Substitute: 10 + 3 * 2 - 1
2
Multiply first: 3 * 2 = 6 → 10 + 6 - 1
3
Add left to right: 10 + 6 = 16 → 16 - 1
4
Subtract: 16 - 1 = 15

Example 2: Parentheses Change Everything

x ← 8 y ← 2 answer ← (x + y) * (x - y)
1
Substitute: (8 + 2) * (8 - 2)
2
Left parentheses: (8 + 2) = 10
3
Right parentheses: (8 - 2) = 6
4
Multiply: 10 * 6 = 60

Example 3: MOD in Action

hours ← 27 days ← hours / 24 remaining ← hours MOD 24
days
\( 27 / 24 = 1.125 \)
Full days elapsed: the division gives a decimal result.
remaining
\( 27 \text{ MOD } 24 = 3 \)
After 1 full day (24h), there are 3 hours left over.

Assignment vs. Equality

Assignment ←
x ← x + 1
Evaluate the right side, then store the result in x. If x was 5, it becomes 6.
Equality =
x = 5
A comparison that asks "Is x equal to 5?" Returns true or false.

Common Mistakes to Avoid

✕ Ignoring Precedence
Assuming 2 + 3 * 4 equals 20 instead of 14. Multiplication always comes before addition.
✕ Confusing MOD and /
Using / when you need the remainder, or using MOD when you need the quotient.
✕ Left-to-Right Errors
Forgetting that *, /, MOD resolve left to right: 12 / 3 * 2 = 8, not 2.

Summary: Operator Reference

Concept Syntax Precedence Key Note
Parentheses ( ) Highest Override all other rules
Multiplication * Medium Left to right with / and MOD
Division / Medium Returns decimal (real division)
Modulus MOD Medium Returns integer remainder
Addition + Lowest Left to right with -
Subtraction - Lowest Left to right with +
Assignment ← After eval Right side evaluated first

Question 1 of 5

What is the value of result after this code runs?

result ← 5 + 3 * 4 - 2

Question 2 of 5

What does 20 MOD 6 evaluate to?

Question 3 of 5

Given x ← 4, what is the value of x after executing x ← x * x + 1?

Question 4 of 5

What is the value of result?

result ← (10 + 2) / (3 * 2)

Question 5 of 5

Which expression correctly checks if a number n is even?