Skip to main content
United StatesComputer Science PrinciplesSyllabus dot point

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.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. What a variable is
  3. The assignment operator
  4. Evaluate the right side first
  5. Why swapping needs a temporary variable
  6. Try this

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. So n is 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

Sources & how we know this