How do variables store and update values, and how does the assignment operator work?
Topic 3.1 Variables and Assignments: a variable is a named reference to a stored value, and the assignment operator stores the value of an expression into a variable.
A focused answer to AP CSP Topics 3.1 to 3.3, covering variables as named storage, the assignment operator (the arrow) in AP CSP pseudocode, evaluating the right side first, updating variables, data types, and tracing assignment statements.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topics 3.1 to 3.3) wants you to understand variables and the assignment operator. A variable is a named reference to a stored value; assignment stores the value of an expression into a variable. In the AP CSP pseudocode the assignment operator is the left-pointing arrow (a β expression). You need to evaluate the right side first, store the result in the variable on the left, and trace how variable values change as a program runs. This is the foundation of all programming in Big Idea 3.
What a variable is
The assignment operator
So a β a + b does not mean "a equals a plus b" as a fact; it means "compute a + b with the current values, then store the result back into a". This is why a can appear on both sides. The same idea appears in count β count + 1, the standard way to increase a counter: it reads the current count, adds one, and stores the new value back. Reading the arrow as an instruction to store, rather than as a statement of equality, is the single most important habit for tracing code correctly.
Evaluate the right side first
The rule that makes tracing reliable: always evaluate the entire right-hand expression using the current values, then store the result. For count β count + 1, read the current count, add 1, and store the new value back. The old value is gone after the assignment.
Why swapping needs a temporary variable
If you try to swap x and y with x β y then y β x, the first line overwrites x with y's value, so when the second line runs, x already holds y's value and both become equal. A temporary variable preserves the original before it is overwritten.
The values a variable can hold also have types. AP CSP pseudocode works with numbers, strings (text), Boolean values (true or false) and lists. A variable's type is determined by the value assigned to it, and operations expect compatible types: you can add two numbers, but adding a number to a string behaves differently from adding two numbers. Knowing the type of each variable as you trace helps you predict what each operation will do.
Try this
Q1. After n β 7 and then n β n * 2 + 1, what is the value of n? [1 point]
- Cue. Evaluate the right side:
7 * 2 + 1 = 15. Sonis 15.
Q2. Explain why swapping two variables requires a temporary variable. [2 points]
- Cue. Assigning one variable into the other overwrites the original value before it can be saved, so both end up equal; a temporary variable preserves the original until it can be placed.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2022 (style)1 marksMultiple choice. Consider the following AP CSP pseudocode.
```
a β 5
b β 3
a β a + b
b β a
```
What are the values of `a` and `b` after this code runs?
(A) a = 5, b = 3
(B) a = 8, b = 8
(C) a = 8, b = 3
(D) a = 5, b = 8
Show worked answer β
The answer is (B).
Trace one line at a time. a β 5 sets a to 5. b β 3 sets b to 3. a β a + b evaluates the right side first: 5 + 3 = 8, then stores 8 in a, so a is 8. b β a stores the current value of a (8) into b, so b is 8. (C) forgets the final assignment to b; (D) and (A) misread the order.
Markers reward evaluating the right side of an assignment first, then storing the result, and updating values in order.
AP 2021 (style)2 marksFree response (code writing). Write AP CSP pseudocode that swaps the values of two variables `x` and `y` using a temporary variable `temp`.
Show worked answer β
A 2-point question on assignment and the need for a temporary variable.
temp β x
x β y
y β temp
Point 1: save the original x in temp before overwriting x. Point 2: copy y into x, then copy the saved original (temp) into y. A common error is x β y then y β x, which loses the original x because it was overwritten before being saved, leaving both equal to y.
Related dot points
- Topic 3.3/3.5 Mathematical and Boolean Expressions: programs evaluate arithmetic (including MOD) and Boolean expressions using relational and logical operators (AND, OR, NOT) that produce true or false.
A focused answer to AP CSP Topics 3.3 and 3.5, covering arithmetic operators and the MOD operator, relational operators, the Boolean operators AND, OR and NOT with truth tables, evaluating compound conditions, and the common uses of MOD such as testing even or odd.
- Topic 3.6/3.7 Conditionals and Nested Conditionals: conditional (IF/ELSE) statements select which code runs based on a Boolean condition, and nested conditionals handle multiple decision paths.
A focused answer to AP CSP Topics 3.6 and 3.7, covering IF and IF/ELSE selection, the role of the Boolean condition, nested conditionals for multiple paths, tracing which branch runs, and writing decision logic in AP CSP pseudocode.
- Topic 3.8 Iteration: iteration (REPEAT n TIMES and REPEAT UNTIL) repeats a block of code, with the number of repetitions controlled by a count or a condition.
A focused answer to AP CSP Topic 3.8, covering REPEAT n TIMES and REPEAT UNTIL loops in AP CSP pseudocode, counting iterations, accumulating values, infinite loops and off-by-one errors, and tracing loop execution.
- Topic 3.10 Lists: a list is an ordered collection of elements accessed by index; AP CSP lists are 1-indexed and support traversal and modification with APPEND, INSERT and REMOVE.
A focused answer to AP CSP Topic 3.10, covering lists as ordered collections, 1-based indexing in AP CSP pseudocode, accessing elements, traversing with FOR EACH and REPEAT, list operations (APPEND, INSERT, REMOVE, LENGTH), and why lists scale data processing.
- Topic 3.2 Data Abstraction: data abstraction manages complexity by giving a collection of data a single name, most commonly using a list to represent many values as one variable.
A focused answer to AP CSP Topic 3.2, covering what data abstraction is, how a list represents many values under one name, the benefits for managing and modifying programs, the link to procedural abstraction, and why abstraction manages complexity.
Sources & how we know this
- AP Computer Science Principles Course and Exam Description β College Board (2025)