Fiveable

⌨️AP Computer Science Principles Unit 3 Review

QR code for AP Computer Science Principles practice questions

3.3 Mathematical Expressions

⌨️AP Computer Science Principles
Unit 3 Review

3.3 Mathematical Expressions

Written by the Fiveable Content Team • Last updated September 2025
Verified for the 2026 exam
Verified for the 2026 examWritten by the Fiveable Content Team • Last updated September 2025
⌨️AP Computer Science Principles
Unit & Topic Study Guides
Pep mascot

An algorithm is a set of instructions used to accomplish a specific task or solve a problem.

Sound familiar? The definition of an algorithm is very close to the definition of a program. That said, there are some major differences. The key difference is that an algorithm represents the problem-solving logic, while a program is how you carry it out. Programs execute algorithms.

Going back to our cake metaphor from Big Idea 1, an algorithm would represent the steps you take to make the cake while a program would represent the written recipe that the baker, or computer, uses to make the cake. 

Algorithms can be expressed in a wide range of ways. They can be formally written out in a programming language like C+ or Java (or a block-based language like Scratch!) for a computer to execute, written out in Pseudocode or a natural language like English, or even drawn out in a diagram.

Your computer will only do exactly what you tell it to, so it's important to make sure you're being clear and detailed when transferring your algorithms from your head to a program. 

Sequencing

Every algorithm is created using three basic concepts: sequencing, selection, and iteration. These concepts are conveyed in code statements, which are parts of program code that express an action to be carried out.

Sequencing consists of steps in your algorithm that are implemented in the order they're written in. Once you execute one statement, you go on to the next one.

first_number = 7
second_number = 5
sum_value = first_number + second_number
print (sum_value)

Each of these statements are run in sequential order: the computer reads and processes them one after the other.

We'll talk more about selection and iteration later in Big Idea 3.

Pep mascot
more resources to help you study

Expressions

Like you learned in math class, an expression is a statement that returns only one value. 

It can consist of a value, a variable, an operator or a procedure call that returns a value.

Programs work with arithmetic operators: your good old fashioned addition, subtraction, multiplication and division signs.

Here are the operators in College Board's Pseudocode. 

The arithmetic operators in Python are the same, except MOD is represented by the % sign.

You'll notice the addition of a new operator known as the MOD operator. This is short for the Modulo operator. In the a MOD b format, a is divided by b and MOD gives you what the remainder would be. For example, 13 MOD 3 would return 1 because 13 divided by 3 can be written as 4 remainder 1.

🔗 Code.org has created a visual to represent how MOD works known as the Modulo Clock. 

Evaluating an expression is done through an order of operations specified by the programming language you're working with. It's generally going to be PEMDAS, like it is in mathematics. In College Board's Pseudocode, the MOD operator has the same precedence as the multiplication and division operators. (PEMMDAS?)

❗Don't forget that, in the PEMDAS system, it's multiplication or division and addition or subtraction, depending on what order they're written in the equation. Liberal use of parentheses can help make some of this less confusing.

Frequently Asked Questions

How do I write a mathematical expression in pseudocode for AP Computer Science?

Write expressions in clear pseudocode using variables, values, and the allowed arithmetic operators: +, -, *, /, and MOD (remainder). Put the expression on the right side of an assignment so it evaluates to a single value. Follow normal order of operations (*/MOD before + -) and use parentheses to force the order you want. Examples: - total ← price * quantity + tax - avg ← (a + b + c) / 3 - remainder ← n MOD 5 - area ← π * r * r // use a named constant π if allowed Keep statements sequential and readable (one action per line) and name variables clearly. The exam reference sheet gives MOD semantics and operators; use those forms on the AP exam. For more examples and tricks, check the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

What's the difference between `/` and `MOD` operators in programming?

The / operator does normal division and produces a numeric quotient (can be a decimal). Example from the CED: 17 / 5 = 3.4. MOD (the modulus operator) gives the integer remainder after division—the CED defines a MOD b as the remainder when a (≥ 0) is divided by b (> 0). Example: 17 MOD 5 = 2. Important AP details: the Exam Reference Sheet includes both / and MOD and shows MOD has the same precedence as * and / (so they’re evaluated together). Use / when you need the exact quotient (possibly fractional); use MOD when you need the leftover whole-number remainder (often for tasks like “is this number even?” or wrapping indexes). For a quick refresher, check the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

Why does `17 / 5` equal 3.4 but `17 MOD 5` equal 2?

Think of division as “how many” (possibly with a fraction) and MOD as “what’s left over.” 17 / 5 = 3.4 because ordinary division gives the quotient 3.4 (17 divided by 5 is 3 whole fives plus 0.4 of a five). 17 MOD 5 = 2 because MOD returns the remainder after dividing integers: 5 goes into 17 three whole times (3 * 5 = 15) and 17 − 15 = 2 remains. The AP Exam reference sheet defines a MOD b as the remainder when a (an integer ≥ 0) is divided by b (an integer > 0) (EK AAP-2.C.2). For extra practice on expressions and the modulus operator, check the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and the practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

What's the syntax for writing arithmetic expressions on the AP exam reference sheet?

On the AP reference sheet you write arithmetic in plain text using these operators: + (addition), - (subtraction), * (multiplication), / (division), and MOD (modulus). Examples from the CED: a + b, a - b, a * b, a / b, and a MOD b. MOD is written as two words (e.g., 17 MOD 5 = 2) and has the same precedence as * and /. Standard order of operations applies (parentheses first, then *, /, MOD, then + and -), so use parentheses when you need a different evaluation order: (a + b) * c. Note: 17 / 5 on the reference sheet evaluates to 3.4 (floating-point), while a MOD b assumes nonnegative integer a and positive integer b. For a quick review, see the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and try practice questions (https://library.fiveable.me/practice/ap-computer-science-principles).

I'm confused about order of operations in programming - is it the same as math class?

Short answer: yes—in AP CSP the usual math order of operations applies in expressions, and the exam reference sheet’s arithmetic operators (+, -, *, /, MOD) follow that order. Multiplication, division, and MOD have higher precedence than addition and subtraction (EK AAP-2.C.3–4 and EK AAP-2.B.5). Practically: eval multiply/divide/MOD before add/subtract, and use parentheses whenever you want to be sure of the order. Programming languages can define precedence precisely, so rely on the language or the exam sheet, and use parentheses for clarity and readability (EK AAP-2.B.7). For review, check the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-principles) to get comfortable evaluating expressions.

How do I trace through a code segment with multiple mathematical expressions step by step?

Start by remembering sequencing and order of operations from the CED: statements run in the order they appear (EK AAP-2.B.6) and expressions follow normal precedence—parentheses, then * / MOD, then + - (EK AAP-2.C.4). Trace like this: 1) Read the first statement left-to-right and note any assignments (e.g., x ← 3 * (y + 2)). 2) If an expression has subexpressions, evaluate the innermost parentheses first. Write each intermediate value down. 3) Apply * / MOD next, then + -, showing results after each step. For MOD remember it’s remainder and has */* precedence (EK AAP-2.C.2–3). 4) When you reach an assignment, update the variable value and use that new value for later statements (sequencing). 5) Continue until the end and report final variable values. Practice with short fragments and write each evaluation line-by-line. For more examples and targeted practice, check the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and the large practice set (https://library.fiveable.me/practice/ap-computer-science-principles).

When do I use the `MOD` operator instead of regular division?

Use MOD when you care about the remainder after integer division; use regular division when you need the actual quotient (possibly a decimal). The CED defines a MOD b as “the remainder when a is divided by b” (e.g., 17 MOD 5 = 2) while 17 / 5 = 3.4. Typical uses for MOD (and when you’d pick it instead of /): - Check divisibility: a MOD b = 0 means b divides a. - Even/odd tests: n MOD 2 (0 → even, 1 → odd). - Wrap-around/indexing: (index + 1) MOD length cycles indices back to 0. - Extract digits: n MOD 10 gives the last digit of n. - Tiling/chunking problems where you need leftover pieces. Remember the AP exam reference sheet gives +, -, *, /, MOD and says MOD has same precedence as * and / (EK AAP-2.C.2–3). For more examples and practice problems, see the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and Fiveable’s practice set (https://library.fiveable.me/practice/ap-computer-science-principles).

What does it mean when they say expressions are evaluated to produce a single value?

It means when you write an expression (like 3 * (x + 2) – y MOD 4), the program follows rules (operators, procedure calls, and order of operations) to compute one final result—a single value—before that value is used in the next statement. An expression can include literals, variables, operators, or a procedure call that returns a value (CED EK AAP-2.B.3–2.B.5). Evaluation applies precedence (multiplication, division, MOD before addition/subtraction) and any language-specific rules so the expression reduces step-by-step to one number or Boolean. That single value is what an assignment or a conditional sees when sequencing executes (EK AAP-2.B.1, 2.B.6). Practice evaluating mixed expressions and MOD examples on Fiveable’s Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and try more problems at (https://library.fiveable.me/practice/ap-computer-science-principles).

I don't understand how sequencing works - do all statements just run in order?

Short answer: yes—sequencing means statements run in the exact order they’re written, unless you explicitly change the flow with selection (if/else) or iteration (loops). That’s EK AAP-2.B.1 and AAP-2.B.6 in the CED: each code statement is an action and sequential statements execute in the order they appear. What that means for you: - If you assign x ← 5 then x ← x + 2, x will be 7. Order matters. - Expressions inside a statement still follow operator precedence (EK AAP-2.C.5), so a + b * c evaluates b * c first. - Sequencing is one of three building blocks (sequencing, selection, iteration—EK AAP-2.A.4), so you’ll see questions asking you to trace code step-by-step on the exam. Want practice tracing sequences and expressions? Use the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and thousands of practice problems (https://library.fiveable.me/practice/ap-computer-science-principles) to get faster at it.

Why am I getting weird decimal answers when I divide integers in my program?

You’re seeing “weird” decimals because division behavior depends on the operator and the types of the values. The AP CED lists / as normal division (17 / 5 = 3.4) and also gives a MOD operator for remainders (17 MOD 5 = 2). If your program treats numbers as floating-point, / will give a decimal result. If your language does integer division (or you explicitly convert to an integer), the result may be truncated (e.g., 17 / 5 → 3). What to do: - If you want the exact decimal, make sure operands are floats (or use the language’s float division). - If you want an integer quotient, use integer division or convert/cast the result to an int (or use floor/ceil/round as needed). - If you want the remainder, use MOD (or % in many languages). These are covered in Topic 3.3 (EK AAP-2.C and EK AAP-2.C.3). For quick review, see the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

What's the difference between an expression and a code statement?

An expression is anything that evaluates to a single value—a number, variable, operator combo, or a procedure call that returns something (EK AAP-2.B.3–2.B.4). For example, 3 * (a + b) or x MOD 5 are expressions. A code statement is a line of code that tells the program to do something—an action like assigning a value, calling a procedure, or printing output (EK AAP-2.B.2). For example, total ← 3 * (a + b) is a statement that uses an expression on the right-hand side. Key differences: expressions are evaluated (follow order of operations, EK AAP-2.C.4) to produce values; statements are executed in sequence (EK AAP-2.B.1, EK AAP-2.B.6) to carry out actions. Expressions often appear inside statements (e.g., in assignments or conditionals). For more AP-style examples and practice, see the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and lots of practice questions (https://library.fiveable.me/practice/ap-computer-science-principles).

How do I write an algorithm using natural language instead of actual code?

Write it as a numbered, step-by-step set of instructions that use sequencing (one action per step), name the variables, and show any mathematical expressions you’ll evaluate. The AP CED explicitly allows natural language algorithms (LO AAP-2.A). Keep steps clear and unambiguous so someone (or a computer) could follow them in order (EK AAP-2.B.6, EK AAP-2.B.7). Example (compute average of three scores and report rounded-down integer): 1. Input score1, score2, score3. 2. total ← score1 + score2 + score3. 3. average ← total / 3. 4. result ← average MOD 1 removed (round down). 5. Display result. Notes: write expressions exactly (use +, -, *, /, MOD as on the exam), state order of operations when needed, and name when a step repeats or branches. For more examples on Topic 3.3, see the Topic 3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

Can someone explain what `a MOD b` means in simple terms with examples?

a MOD b means “the remainder left when a is divided by b.” The CED defines it for non-negative integer a and positive integer b. Example: 17 MOD 5 = 2 because 17 = 3×5 + 2 (3 is the quotient, 2 is the remainder). A few quick cases to remember: - If a < b, then a MOD b = a (e.g., 3 MOD 5 = 3). - If a is divisible by b, remainder is 0 (e.g., 12 MOD 4 = 0). - Use it to test even/odd: n MOD 2 = 0 means n is even; = 1 means odd. On the AP exam the MOD operator has the same precedence as * and / and appears on the reference sheet (see EK AAP-2.C.2). For a quick review of examples and how expressions evaluate, check the Topic 3.3 study guide on Fiveable (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD)—and for more practice problems, visit (https://library.fiveable.me/practice/ap-computer-science-principles).

What are all the arithmetic operators I need to know for the AP exam?

For the AP CSP exam you only need to know these arithmetic operators (they’re on the Exam Reference Sheet): + (addition), - (subtraction), * (multiplication), / (division), and MOD (remainder). MOD behaves like “a MOD b = remainder when a divided by b” (e.g., 17 MOD 5 = 2). Division is real-valued on the sheet (17 / 5 = 3.4). Order of operations follows normal math rules; MOD has the same precedence as * and /, so it’s evaluated before + and -. For practice, review examples of expressions, operator precedence, and MOD in the Topic 3.3 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and try problems from the unit practice set (https://library.fiveable.me/practice/ap-computer-science-principles) to get comfortable.

I'm confused about precedence - does `MOD` come before or after multiplication and division?

MOD has the same precedence as multiplication and division—so it’s evaluated at the same step in the order of operations. When operators have the same precedence, evaluate left-to-right (associativity). This is exactly what EK AAP-2.C.4 says on the Exam Reference Sheet: MOD has the same precedence as * and /. Example: 17 MOD 5 * 2 → first do 17 MOD 5 = 2, then 2 * 2 = 4. Remember the AP exam uses the reference sheet rules, so apply standard order of operations (parentheses, then * / MOD left-to-right, then + -). For more practice and review on Topic 3.3, see the Topic Study Guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/mathematical-expressions/study-guide/00lGBdF7QyY5hmd1rubD) and the AP practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).