AP CSP Variables And Assignment

AP CSP Topics › Variables & 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.

AP pseudocode assignment operator — NOT = and NOT ==
RRight side evaluates first, then result stores in left side variable
0Variables that exist before any assignment: they don’t

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.

How assignment works: x ← 5 then x ← x + 3 Before: x ← 5 variable x 5 x holds the value 5 x ← x + 3 reads old value, computes 5+3=8, stores result back After: x is now 8 variable x 8 5 is gone, replaced by 8

Assignment always evaluates the right side completely before storing. The old value is permanently replaced.

Assignment (←)
Stores a value into a variable
  • 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
What Assignment Is NOT
Common misconceptions
  • 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.

Trace 1 — Sequential Assignment
x 10 y x + 3 x y * 2 DISPLAY(x) DISPLAY(y)

What does this program display? Predict x and y before revealing.

Output

26 13 Trace: x=10 → y=10+3=13 → x=13*2=26 DISPLAY(x) outputs 26, DISPLAY(y) outputs 13

Trace 2 — Swap (Classic Trap)
a 7 b 3 a b b a DISPLAY(a) DISPLAY(b)

A student says this swaps a and b. Does it? What displays?

Output

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.

Trace 3 — Cumulative Update
total 0 total total + 5 total total + 12 total total + 8 DISPLAY(total)

What does total equal after all assignments?

Output

25 Trace: total=0 → 0+5=5 → 5+12=17 → 17+8=25. This accumulator pattern appears frequently in loop questions.

Trace 4 — String Assignment
first “AP” second “CSP” result first + “ ” + second DISPLAY(result)

What does DISPLAY output?

Output

AP CSP String concatenation with + joins strings. result = “AP” + “ ” + “CSP” = “AP CSP”.

Spot the Bug

SPOT THE BUG — Swap Without Temp
PROCEDURE swap(a, b) a b b a DISPLAY(a, b)
Bug Explained

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.

SPOT THE BUG — Using a Variable Before Assigning It
DISPLAY(score) score 95
Bug Explained

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.

SPOT THE BUG — Wrong Direction Arrow
5 x
Bug Explained

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

1
Thinking x ← x + 1 is a contradiction

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.

2
Forgetting that assignment overwrites the old value

After x ← 10 then x ← 20, x holds 20. The 10 is gone permanently. There is no undo.

3
Assuming variables have default values

In AP pseudocode, using a variable before assigning it is an error. No default of 0 or empty string is assumed.

4
Confusing AP ← with Java = or ==

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
y = 4*2 = 8. x = 8-1 = 7. DISPLAY(x) outputs 7.

2. a ← 5
b ← a
a ← 10
DISPLAY(b)


What is displayed?

  • 10
  • 5
  • 15
  • 0
b is assigned a’s value (5) on line 2. Line 3 changes a to 10 but b is already set to 5 and is unaffected. DISPLAY(b) outputs 5.

3. Consider the following code:

x ← 3
x ← x + x
x ← x * x
DISPLAY(x)


What is displayed?

  • 9
  • 36
  • 12
  • 18
x=3 → x=3+3=6 → x=6*6=36. DISPLAY outputs 36.

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
Line II has an operator precedence error: b/2 is computed first (division before addition), so avg = a + (b/2), not the true average. Line III is correct with the parentheses. Line I correctly computes the total.

5. n ← 1
n ← n * 2
n ← n * 2
n ← n * 2
DISPLAY(n)


What is displayed?

  • 6
  • 2
  • 8
  • 16
n=1 → 1*2=2 → 2*2=4 → 4*2=8. Each line doubles n. After 3 doublings: 1 x 2^3 = 8.

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.
Line 1 overwrites x with y. Line 2 sets y = x, but x is already y’s original value. Both variables end up holding y’s original value.

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
I is correct — assignment replaces the previous value. II is correct — b ← a copies the value; changing a later does not change b. III is false — AP pseudocode has no variable declarations or type annotations.

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
p=2, q=2+1=3, p=3+1=4, q=4+1=5. DISPLAY(p)=4, DISPLAY(q)=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 + 1
  • count ← 0
    count ← count + 1
    count ← count + 1
    count ← count + 1
  • count = 0
    count = count + 1
    count = count + 1
    count = count + 1
  • count ← 0
    count + 1 → count
    count + 1 → count
    count + 1 → count
Option B correctly initializes count to 0 and uses ← to increment. Option A starts at 1, not 0. Option C uses = (Java syntax). Option D has the arrow pointing the wrong direction.

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
x=10 MOD 3 = 1 (remainder of 10/3). y = 1 + 3 = 4. Final: x=1, y=4.

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

What is MOD in AP pseudocode?
MOD returns the remainder of integer division. 10 MOD 3 = 1 (since 10 = 3×3 + 1). 8 MOD 4 = 0 (evenly divisible). MOD is frequently used in AP exam questions to test divisibility, even/odd checks, and wrapping values.
Can a variable hold different types at different times?
In AP pseudocode, yes — there is no strict typing. A variable can hold a number then be reassigned to a string. In practice, mixing types leads to logic errors, but AP pseudocode does not generate type errors.
What does DISPLAY do exactly?
DISPLAY outputs the value of its argument. Multiple DISPLAY calls each output on separate lines unless specified otherwise. DISPLAY(x, y) outputs x then y on the same line with a space or comma depending on the question context.

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]