AP COMPUTER SCIENCE PRINCIPLES • ALGORITHMS AND PROGRAMMING

Mathematical Expressions

How computers evaluate arithmetic, assign values, and translate math into executable code.

Historical Context & Motivation

Long before modern programming languages existed, mathematicians and engineers grappled with the fundamental challenge of expressing computation in a precise, unambiguous notation. The idea of translating mathematical expressions into mechanical steps dates back centuries, but it became an urgent engineering problem once electronic computers emerged in the mid-twentieth century. Early machines required programmers to specify every arithmetic operation as a raw sequence of machine-level instructions, making even simple formulas like y = 3x + 5 tedious and error-prone to encode. The evolution from hand-coded numeric opcodes to high-level expressions that closely resemble standard mathematical notation is one of the defining stories of computer science.

1843
Ada Lovelace's Notes
Ada Lovelace published detailed notes on Charles Babbage's Analytical Engine, describing how mathematical operations could be sequenced as algorithmic steps — the first articulation of programming with expressions.
1957
FORTRAN Released
IBM released FORTRAN (FORmula TRANslation), the first high-level language that let scientists write mathematical expressions in near-standard algebraic notation, compiled automatically into machine code.
1960
ALGOL 60 Standard
The ALGOL 60 specification formalized operator precedence rules and expression evaluation order, establishing conventions still used by virtually every modern language.
1991–2000
Python & JavaScript
Scripting languages like Python and JavaScript made mathematical expressions accessible to non-specialists, incorporating intuitive operators and dynamic typing that simplified rapid prototyping of calculations.

The central question these developments address is deceptively simple: how does a computer interpret, evaluate, and store the result of a mathematical formula? Understanding the answer requires mastering the syntax of expressions, the semantics of operators, and the rules governing evaluation order — precisely the skills tested on the AP Computer Science Principles exam.

Core Principles & Definitions

A mathematical expression in programming is a combination of values, variables, operators, and function calls that a language evaluates to produce a single result. On the AP CSP exam, expressions appear in both text-based pseudocode and the AP reference language, so fluency with their structure is essential. The following principles underpin every expression you will encounter.

1

Operators & Operands

An operator (such as +, −, ×, /, MOD) acts on operands (literal values or variables) to produce a result.
2

Order of Operations

Computers follow a strict precedence hierarchy: parentheses first, then multiplication/division/MOD, then addition/subtraction. Within equal precedence, evaluation proceeds left to right.
3

Assignment vs. Equality

The assignment operator (← in AP pseudocode, = in most languages) stores an expression's result in a variable. This is distinct from the equality comparison (=) used in Boolean expressions.
4

Data Types in Expressions

Mixing integers and real numbers may produce integer division (truncation) in some languages. The MOD operator returns the remainder of integer division.
5

Nested Expressions

Expressions can be composed: the result of one sub-expression becomes an operand in a larger expression, enabling arbitrarily complex formulas to be evaluated step by step.
KEY TAKEAWAY
KEY TAKEAWAY

Visual Explanation — Expression Evaluation Tree

One of the clearest ways to understand how a computer evaluates a mathematical expression is to visualize it as a tree. The diagram below shows how the expression result ← 3 + 5 × (10 − 4) / 2 is decomposed into an expression tree, where each internal node is an operator and each leaf is a value. Evaluation proceeds from the bottom up: the deepest sub-expressions resolve first, exactly matching precedence and parenthesization rules.

The pink node (−) inside parentheses evaluates first (Step 1), producing 6. Multiplication (Step 2) and division (Step 3) resolve next due to higher precedence than addition. Finally, the root + node (Step 4) yields 18.

Notice that the tree structure makes the evaluation order completely unambiguous. The parenthesized sub-expression (10 − 4) sits deepest in the tree, forcing it to evaluate first. Without parentheses, the subtraction would have lower precedence than multiplication, and the answer would be different. This visual model mirrors exactly how a compiler or interpreter parses and executes your code internally.

Mathematical Framework — Operators & Precedence

The AP CSP exam uses a specific pseudocode notation for mathematical expressions. Understanding how each operator works — and the order in which they are applied — is essential for tracing code accurately. Below are the key operators and formal evaluation rules you need to know.

ASSIGNMENT
variable ← expression
The right side is evaluated first, producing a value. That value is then stored in variable. Example: x ← 7 + 3 stores 10 in x.
ARITHMETIC OPERATORS
a + b, a − b, a × b, a / b, a MOD b
Addition, subtraction, multiplication, division, and modulus (remainder). For example, 17 MOD 5 = 2 because 17 ÷ 5 = 3 remainder 2.
PRECEDENCE HIERARCHY
( ) → ×, /, MOD → +, −
Parentheses override everything. Multiplication, division, and MOD share the same level and evaluate left to right. Addition and subtraction are last, also left to right.
COMPOUND EXPRESSION
result ← (a + b) × c − d MOD e
Evaluation order: (1) a + b inside parentheses, (2) multiply that sum by c, (3) compute d MOD e, (4) subtract step 3 from step 2. The result is stored in result.
AP EXAM TIP

Detailed Breakdown — Operator Behavior & Common Pitfalls

While the basic operators are straightforward, several subtleties arise when they interact. This section catalogs each operator's behavior and highlights the mistakes students most frequently make on the AP exam.

The pyramid visualizes precedence levels: parentheses at the top override everything. Multiplication, division, and MOD share the middle tier. Addition and subtraction occupy the base. Within a tier, evaluation proceeds left to right.
AP CSP Arithmetic Operators — Behavior and Pitfalls
OperatorExampleResultCommon Pitfall
+7 + 310Confusing + with string concatenation when operands are strings.
10 − 46Negative results are valid; watch sign errors.
×6 × 318Forgetting that × binds tighter than + and −.
/7 / 23.5 (or 3)Integer division truncates; check context.
MOD17 MOD 52MOD has the same precedence as × and /; students often give it lower priority.
MOD — THE MOST TESTED OPERATOR

Worked Example — Tracing a Multi-Step Expression

Let us trace a realistic AP-style problem from start to finish. Suppose the following pseudocode appears on the exam and you are asked: what value is stored in answer after these lines execute?

a ← 10 b ← 3 c ← a MOD b + 4 × 2 answer ← c + a / b

1
Step 1 — Assign a and bThe first two lines simply store literal values: a = 10 and b = 3. No expression evaluation is needed beyond recognizing these as direct assignments.
a = 10, b = 3
2
Step 2 — Evaluate c ← a MOD b + 4 × 2Apply precedence. MOD, ×, and / share the same tier, so we evaluate left to right among them before handling +. First: a MOD b = 10 MOD 3 = 1. Next, multiplication: 4 × 2 = 8. Now the expression reduces to 1 + 8.
c = 9
3
Step 3 — Evaluate answer ← c + a / bDivision has higher precedence than addition, so evaluate a / b = 10 / 3 first. If we assume real-valued division, this equals approximately 3.33. Then: c + 3.33 = 9 + 3.33. If the problem specifies integer division, 10 / 3 = 3 and the answer is 12.
answer ≈ 12.33 (real) or 12 (integer division)
4
Step 4 — Verify with ParenthesizationTo confirm, rewrite using explicit parentheses reflecting precedence: c ← (a MOD b) + (4 × 2) and answer ← c + (a / b). Inserting known values: (10 MOD 3) + (4 × 2) = 1 + 8 = 9, then 9 + (10 / 3). Our answer checks out.
Confirmed: answer = 12 (integer division)

Mathematical vs. Programming Notation — Key Differences

Students who are comfortable with standard algebraic notation sometimes stumble when translating into code because programming notation introduces subtle differences. The table below compares the two worlds side by side, clarifying where mismatches most often cause errors.

Math Notation vs. AP CSP Pseudocode
FeatureStandard MathAP CSP Pseudocode
Assignmentx = 5 (equation)x ← 5 (store value)
Multiplication3x (implied)3 × x (explicit operator)
Exponentiationx² (superscript)Not a built-in operator; use x × x
DivisionFraction bar (exact)a / b (may truncate)
Remainderr in a = bq + ra MOD b
Order of operationsPEMDAS / BODMASSame, but MOD is at × / level
KEY TAKEAWAY
KEY TAKEAWAY

Connection to Advanced Concepts

Mathematical expressions are the building blocks for more sophisticated constructs in computer science. As you advance through the AP CSP curriculum, you will encounter expressions embedded within conditionals, loops, and procedure calls. Understanding how a simple arithmetic expression evaluates is a prerequisite for mastering these more complex structures.

From Simple Expressions to Advanced Constructs
ConceptBasic Expression UseAdvanced Extension
Boolean expressionsx + 3x + 3 > 10 — result is true/false
Loop controli ← i + 1Counter incremented each iteration to control repetition
Procedures with returna × bRETURN(a × b) — expression becomes a reusable function
List indexingi + 1list[i + 1] — expression computes an index

In university-level courses, expression evaluation connects to formal language theory and abstract syntax trees (ASTs), which are the data structures compilers build to represent code internally. The expression tree you saw in Section 3 is, in fact, a simplified AST. Understanding expressions at this level prepares you not only for the AP exam but for deeper study in compiler design, numerical computing, and algorithm analysis.

Practice Problems

1
Which of the following best explains why x ← x + 1 is valid in a program but not in algebra?
2
What value is stored in result after the following statement executes? result ← 14 MOD 4 + 3 × 2
3
Consider the following code: x ← 20 y ← 6 x ← x − y × 2 y ← x MOD 3 Which TWO of the following statements are true after this code executes?
PROBLEM 4APPLIED
A programmer wants to extract the tens digit from a three-digit integer stored in variable num. For example, if num = 472, the result should be 7. Using only integer division (truncation) and MOD, write a single expression that computes the tens digit and assign it to tensDigit.
PROBLEM 5CRITICAL THINKING
A student claims that for any positive integers a and b, the expression (a / b) × b + (a MOD b) always equals a (assuming integer division with truncation). (a) Explain why this identity holds by describing what integer division and MOD each compute. (b) Provide a specific numerical example demonstrating the identity. (c) Describe a practical programming scenario where this identity would be useful. (d) Explain what would happen if real-valued (non-truncating) division were used instead, and whether the identity would still hold.
Varsity Tutors • AP Computer Science Principles • Mathematical Expressions