PrepGo

Mathematical Expressions - AP Computer Science Principles Study Guide

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Learn with study guides reviewed by top AP teachers. This guide takes about 17 minutes to read.

Getting Started

At their core, computers are powerful calculators. To solve problems, from rendering a video game to processing a credit card transaction, programs must perform mathematical calculations. This requires a clear and unambiguous way to write and interpret mathematical instructions, ensuring the computer evaluates them in the correct sequence to produce a reliable result.

What You Should Be Able to Do

  • Evaluate a computational expression that uses mathematical operators.

  • Determine the result of an expression that uses the modulus (MOD) operator.

  • Trace the sequence of operations a computer follows when evaluating a complex expression.

  • Explain how a variable's value is changed by an assignment statement.

Key Concepts & Application

The Core Idea

In programming, an expression is a combination of values, variables, and operators that a computer evaluates to produce a single, new value. Think of it as a recipe: the ingredients are your numbers and variables, the instructions are the operators (+, *, etc.), and the final dish is the single value that results.

To make programs work, we need to store these results. An assignment operator (represented in pseudocode as ) takes the final value of an expression and stores it in a variable. This is not a statement of equality like in mathematics; it is an action of placing a value into a storage location.

Logic & Application

Computers rely on a specific set of rules to evaluate expressions. Without them, an expression like 3 + 5 * 2 could be interpreted as 8 * 2 (16) or 3 + 10 (13). To ensure consistency, programming languages use a standard order of operations.

Arithmetic Operators

Arithmetic operators are symbols that perform mathematical calculations. The most common ones are:

OperatorNameExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division7 / 23.5
MODModulus7 MOD 21

The Modulus (MOD) operator is particularly useful. It returns the remainder after division. For example, 7 MOD 2 is 1 because 7 divided by 2 is 3 with a remainder of 1. This is excellent for tasks like checking if a number is even or odd (e.g., number MOD 2 will be 0 if number is even).

Order of Operations

The sequence for evaluating expressions is:

  1. Parentheses (): Expressions inside parentheses are always evaluated first, from the inside out.

  2. Multiplication *, Division /, Modulus MOD: These are evaluated next, from left to right.

  3. Addition +, Subtraction -: These are evaluated last, from left to right.

Annotated Pseudocode Examples

Example 1: Basic Assignment

This example shows how the value of an expression is calculated and then assigned to the variable finalScore.


baseScore ← 50

bonusPoints ← 10

penalty ← 5


// The expression on the right is evaluated first.

// (50 + 10 * 2) - 5  --> (50 + 20) - 5  --> 70 - 5  --> 65

finalScore ← (baseScore + bonusPoints * 2) - penalty


// The variable finalScore now holds the value 65.

DISPLAY(finalScore)

Example 2: Using the MOD Operator

This code checks if a number of items can be distributed evenly among a group of people.


itemCount ← 25

peopleCount ← 4


// 25 MOD 4 calculates the remainder of 25 / 4.

// 25 / 4 is 6 with a remainder of 1.

itemsLeftOver ← itemCount MOD peopleCount


// The variable itemsLeftOver now holds the value 1.

DISPLAY(itemsLeftOver)

Tracing & Analysis

Logic Trace

Let's trace the evaluation of a complex expression step-by-step.

Expression: (6 + 9) / 3 + 4 * 2

StepExpressionActionResult
1(6 + 9) / 3 + 4 * 2Evaluate parentheses first.15 / 3 + 4 * 2
215 / 3 + 4 * 2Evaluate division (left-most of * or /).5 + 4 * 2
35 + 4 * 2Evaluate multiplication.5 + 8
45 + 8Evaluate addition.13

The final value of the expression is 13.

A Note on Numerical Precision

Computers store numbers using a finite number of bits. This can lead to rounding errors for numbers that cannot be represented perfectly, such as the result of 1 / 3 (0.33333...). While these errors are often tiny, they can accumulate in complex calculations, which is an important consideration in scientific and financial software.

Key Terminology & Logic

This table summarizes the core operators used in mathematical expressions.

Symbol / KeywordNamePurpose
Assignment OperatorAssigns the value of the expression on the right to the variable on the left.
+, -, *, /Arithmetic OperatorsPerform standard addition, subtraction, multiplication, and division.
MODModulus OperatorReturns the integer remainder of a division operation.
()ParenthesesGroups parts of an expression to force their evaluation first.

Core Concepts & Terminology

  • Expression: A combination of values, variables, operators, and/or procedure calls that is evaluated to produce a single value.

  • Arithmetic Operator: A symbol in a programming language (e.g., +, *, MOD) that performs a mathematical calculation on one or more values.

  • Assignment Operator (): An operator that assigns the value calculated from the expression on its right to the variable on its left.

  • Modulus (MOD): An operator that yields the remainder from the division of the first argument by the second. It is useful for determining if a number is even or odd.

  • Order of Operations: The set of rules that dictates the sequence in which operators are evaluated in an expression to ensure a consistent and unambiguous result.

  • Variable Assignment: The action of storing the evaluated result of an expression in a variable.

    
    // The expression '10 * 5' is evaluated to 50.
    
    // The value 50 is then stored in the variable 'score'.
    
    score ← 10 * 5
    

Core Skill Check

  • Logic Tracing: What is the final value of result after this pseudocode runs: x ← 10, y ← 4, result ← (x + 2) MOD y * 3?

  • Debugging: A student wants to calculate the average of three numbers: a, b, and c. Identify the logic error in their code: average ← a + b + c / 3.

  • Application: Describe a real-world scenario where the MOD operator would be useful (other than checking for even/odd numbers).

Common Misconceptions & Clarifications

  • Confusing Assignment () with Equality (=): The statement x ← x + 1 is a valid and common programming instruction meaning "calculate x + 1 and store the new value back into x." It is not a mathematical claim that x is equal to x + 1.

  • Forgetting MOD's Precedence: The MOD operator has the same level of precedence as multiplication and division. It is performed before addition and subtraction.

  • Ignoring Left-to-Right Evaluation: For operators at the same level of precedence (like * and /), the expression is evaluated from left to right. 10 / 2 * 5 is 5 * 5 (25), not 10 / 10 (1).

  • Integer vs. Floating-Point Division: In some programming languages, dividing two integers (e.g., 7 / 2) may result in an integer (3), discarding the remainder. The exam pseudocode assumes standard division unless specified otherwise, but it's a crucial distinction in real-world programming.

Summary

Mathematical expressions are the fundamental building blocks for performing calculations in a program. They combine values and variables with arithmetic operators to produce a single result. To ensure every program gets the same, correct answer from an expression, computation follows a strict order of operations, prioritizing parentheses, then multiplication, division, and modulus, and finally addition and subtraction. The assignment operator () is used to store the result of an expression in a variable, allowing programs to remember and manipulate data over time.