AP CSP Variables And Assignment
AP CSP Variables & Assignment: Complete Guide (2025‑2026)
A variable is a named container that stores a value. AP pseudocode uses a left-arrow (←) for assignment: x ← 5 stores 5 in x. Assignment is not mathematical equality — x ← x + 1 is valid and means “take x’s current value, add 1, store the result back in x.” Tracing variable values through a program is one of the most frequently tested skills on the AP CSP exam.
Contents
The Assignment Arrow
The left arrow stores a value into a variable. The right side is fully evaluated first, then the result replaces whatever was previously in the variable.
Assignment always evaluates the right side completely before storing. The old value is permanently replaced.
-
x ← 5— x now holds 5 -
name ← “Alice”— name holds that string -
total ← a + b— sum stored in total -
x ← x + 1— increments x by 1 - Right side evaluated completely first
- Not algebraic equality: x ← x + 1 makes sense
- Not two-way: x ← y does NOT set y ← x
- Not permanent: any later assignment overwrites it
- Not in Java: Java uses = not ←
- Not boolean: it stores, it doesn’t compare
Code Trace Gauntlet
Predict each output before revealing. Variable tracing is tested on almost every AP CSP exam.
What does this program display? Predict x and y before revealing.
26 13 Trace: x=10 → y=10+3=13 → x=13*2=26 DISPLAY(x) outputs 26, DISPLAY(y) outputs 13
A student says this swaps a and b. Does it? What displays?
3 3 This does NOT swap. Line 3: a becomes 3 (b’s value). Now both a and b are 3. Line 4: b becomes a which is already 3. A real swap needs a temporary variable: temp ← a, a ← b, b ← temp.
What does total equal after all assignments?
25 Trace: total=0 → 0+5=5 → 5+12=17 → 17+8=25. This accumulator pattern appears frequently in loop questions.
What does DISPLAY output?
AP CSP String concatenation with + joins strings. result = “AP” + “ ” + “CSP” = “AP CSP”.
Spot the Bug
After line 2, a already holds b’s original value. Line 3 sets b = a, which is now b’s original value. Both variables end up with b’s original value. Fix: add temp ← a before line 2, then b ← temp at the end.
score is used on line 1 before it is assigned on line 2. At line 1, score has no value. In AP pseudocode, using an undefined variable is an error. Always assign a variable before using it.
Assignment goes FROM right TO left: value → variable. The left side must be a variable name, not a literal value. 5 cannot store anything. Fix: x ← 5
Common Exam Pitfalls
It is valid. The right side is evaluated using x’s current value, then the result is stored back. It is an increment, not an equation.
After x ← 10 then x ← 20, x holds 20. The 10 is gone permanently. There is no undo.
In AP pseudocode, using a variable before assigning it is an error. No default of 0 or empty string is assumed.
AP pseudocode uses ← for assignment and = for comparison. Java uses = for assignment and == for comparison. Do not mix the two on the exam.
Check for Understanding
1. x ← 4
y ← x * 2
x ← y - 1
DISPLAY(x)
What is displayed?
- 4
- 7
- 8
- 3
2. a ← 5
b ← a
a ← 10
DISPLAY(b)
What is displayed?
- 10
- 5
- 15
- 0
3. Consider the following code:x ← 3
x ← x + x
x ← x * x
DISPLAY(x)
What is displayed?
- 9
- 36
- 12
- 18
4. Which line contains a logic error in a program intended to compute the average of a and b?
I. total ← a + b
II. avg ← a + b / 2
III. avg ← (a + b) / 2
- I only
- II only
- III only
- I and II
5. n ← 1
n ← n * 2
n ← n * 2
n ← n * 2
DISPLAY(n)
What is displayed?
- 6
- 2
- 8
- 16
6. A programmer wants to swap the values of x and y. Their code:x ← y
y ← x
This code is incorrect because:
- Variables cannot be assigned from other variables.
- After line 1, x holds y’s original value, so line 2 sets y to y’s original value, not x’s original value.
- The assignment operator direction is reversed.
- Two lines are not enough to swap values in any language.
7. Consider these statements about variables:
I. A variable can hold only one value at a time.
II. Assigning a new value to a variable does not affect other variables that previously received the same value.
III. In AP pseudocode, variables must be declared with a type before they can be assigned.
Which are correct?
- I only
- I and II only
- II and III only
- I, II, and III
8. p ← 2
q ← p + 1
p ← q + 1
q ← p + 1
DISPLAY(p)
DISPLAY(q)
What is displayed?
- 3 then 4
- 4 then 5
- 2 then 3
- 3 then 5
9. Which of the following correctly initializes a counter to zero and then increments it three times?
count ← 1
count ← count + 1
count ← count + 1
count ← count + 1count ← 0
count ← count + 1
count ← count + 1
count ← count + 1count = 0
count = count + 1
count = count + 1
count = count + 1count ← 0
count + 1 → count
count + 1 → count
count + 1 → count
10. After executing the following, what are the final values of x and y?x ← 10
y ← 3
x ← x MOD y
y ← x + y
- x=1, y=4
- x=3, y=6
- x=10, y=3
- x=1, y=7
How the AP Exam Tests This
- Trace through 3-6 lines of sequential assignment and report the final value of a specific variable
- Identify which assignment line introduces an error (operator precedence, wrong direction, use before assignment)
- Determine whether a swap procedure correctly swaps two values
- I/II/III format: identify which statements about assignment are true
- Given expected output, identify which assignment statement would produce it
FAQ
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]