AP · Computer Science Principles · Variables, Expressions and Strings

Variables and Assignment

12 min readUpdated December 1, 2026FreeAP Computer Science Principles, Algorithms and Programming

A variable is a named abstraction that holds one value at a time. In the pseudocode of the AP® Computer Science Principles exam, a ← expression works out the expression on the right, then stores the result in the variable on the left, replacing whatever was there. So a variable always holds the value most recently assigned to it, and you find that value by tracing the assignments in order.

Quick check

  1. After a ← 3 and then a ← 8, what does a hold?Show answerHide answer
    1. The second assignment replaces the 3.
  2. In count ← count + 1, which side is worked out first?Show answerHide answer

    The right side. The program adds 1 to the current value of count, then stores the result back in count.

Know these already? Jump to the practice questions

What you'll be able to do

  • State what an assignment statement does to a variable.
  • Predict the output of a sequence of assignments using a trace table.
  • Explain why the simplest swap uses a temporary variable.
  • Choose meaningful variable names.

Key terms

Variable

A named abstraction in a program that holds one value at a time; the value can change as the program runs.

In plain English
A labeled box with exactly one thing in it.
Don't write
"A variable keeps all the values it has ever been given."

Assignment

A statement that evaluates an expression and stores the result in a variable, written a ← expression in the exam's pseudocode.

In plain English
Work out the right side, then put the answer in the box on the left.
Don't write
"a ← b puts the value of a into b."

Trace table

A table with one column per variable and one row per step, showing each variable's value after every line runs.

In plain English
A record of the boxes after each line.
Don't write
"A trace table shows only the final values."

What is a variable, and how should you name it?

A variable gives a name to a value, so you can use the value without knowing where or how it is stored: that is why it counts as an abstraction. It holds exactly one value at a time. That value can be a number, true or false, text, or a whole list, which counts as one value made of many elements.

Choose names that say what the value means. livesLeft tells a reader what the number is for; x or n2 does not. The name has no effect on how the program runs, but a meaningful name makes code easier to read, check and fix, for you and for anyone else.

Check yourself

Which is the better name for the highest score so far: hs or highScore?
Show answerHide answer

highScore. It says what the value means, so a reader does not have to guess.

How does the assignment arrow work?

a ← expression is on the exam reference sheet. It runs in two stages:

  1. Evaluate the right side, using the values the variables hold right now.
  2. Store the result in the variable on the left, replacing its old value.

So x ← x * 3 is a normal instruction, not an impossible equation: if x is 4, the right side is 12, and 12 becomes the new value of x. In the pseudocode, the arrow assigns and = compares two values, as in IF(x = 12).

DISPLAY(expression) is also on the sheet: it shows the value followed by a space, so two DISPLAY calls show both values on one line.

Key idea

Assignment copies a value from right to left: the right side is worked out first, then stored on the left.

Check yourself

After x ← 4 and then x ← x * 3, what does x hold?
Show answerHide answer
  1. The right side uses the old value, 4, and 4 × 3 = 12 is stored in x.

How do you trace a sequence of assignments?

Make a trace table with one column per variable and one row per line. On each row, change only the variable on the left of the arrow and copy the others down unchanged.

a ← 2
b ← a
a ← a + 5
Lineab
a ← 22
b ← a22
a ← a + 572

The second line copies the value 2 into b. It does not link b to a, so when a changes to 7, b keeps 2.

Check yourself

After a ← 1, b ← a + 1 and a ← b + 1, what are a and b?
Show answerHide answer

a is 3 and b is 2. b becomes 1 + 1 = 2, then a becomes 2 + 1 = 3.

Why does a safe swap use a third variable?

To swap x and y, the obvious attempt x ← y then y ← x fails. The first line overwrites x, so its old value is gone before the second line needs it, and both variables end up holding y's value.

The fix is to save one value first in a temporary variable:

temp ← x
x ← y
y ← temp

temp keeps the old x, so it can still be put into y after x has been overwritten.

Check yourself

With x ← 5 and y ← 8, what are x and y after the three lines of the swap?
Show answerHide answer

x is 8 and y is 5. temp saves 5, x takes 8, then y takes the saved 5.

Worked example 1: Trace four assignments

Question: What is displayed as a result of running the code segment?

a ← 5
b ← a + 2
a ← b * 3
b ← a - b
DISPLAY(a)
DISPLAY(b)

What tells you the method

A sequence of assignments and a display: trace it line by line.

  1. Step 1: Trace each line.

    Lineab
    a ← 55
    b ← a + 257
    a ← b * 3217
    b ← a - b2114
    Why

    Each right side uses the values from the row above it, so a - b is 21 − 7, not 5 − 7.

  2. Step 2: Read off the display.

    a is 21 and b is 14, shown on one line.

    Why

    Each DISPLAY shows its value followed by a space.

Check it

Re-trace only the last line: just before it, a is 21 and b is 7, so b becomes 14.

Answer

21 14

Worked example 2: Trace a broken swap

Question: A programmer wants to swap x and y. What is displayed as a result of running the code segment?

x ← 10
y ← 4
x ← y
y ← x
DISPLAY(x)
DISPLAY(y)

What tells you the method

A suspected swap: trace it rather than trusting what it is meant to do.

  1. Step 1: Trace the first three lines.

    x is 10, y is 4, then x ← y makes x 4.

    Why

    The old value 10 is overwritten, and no other variable saved it.

  2. Step 2: Trace the last assignment.

    y ← x copies the current x, which is 4, so y stays 4.

    Why

    x already holds 4, so there is no 10 left to copy.

Check it

With temp ← x before x ← y and y ← temp as the last assignment, the code displays 4 10 instead, a true swap.

Answer

4 4

Worked example 3: Choose the correct swap

Question: Which code segment swaps the values of first and second?

  • A)
    first ← second
    second ← first
    
  • B)
    temp ← first
    first ← second
    second ← temp
    
  • C)
    temp ← first
    second ← first
    first ← temp
    
  • D)
    temp ← second
    first ← second
    second ← temp
    

What tells you the method

"Which code segment": test each option by tracing it on the same starting values.

  1. Step 1: Pick test values.

    Start with first = 1 and second = 2; a swap must end with 2 and 1.

    Why

    Different starting values show which variable each result came from.

  2. Step 2: Trace each option.

    A gives 2 and 2. B gives 2 and 1. C gives 1 and 1. D gives 2 and 2.

    Why

    A overwrites first before saving it; C and D save one value but then copy the wrong one.

Check it

Trace B with 7 and 3: temp is 7, first becomes 3, second becomes 7.

Answer

B

Your turn

Question: What is displayed as a result of running the code segment?

p ← 2
q ← p * 4
p ← q - p
q ← q + p
DISPLAY(q)
  1. Step 1: Trace the first two lines.

    p is 2, then q is 2 × 4 = 8.

  2. Step 2: Your go: trace the last two assignments, using the newest values each time.

Show the full solutionHide the solution

Step 2. p ← q - p gives 8 − 2 = 6. Then q ← q + p gives 8 + 6 = 14, using the new p.

Answer: 14

Traps

Trap

a ← b puts the value of a into b.

It seems right because you read the line from left to right. But the arrow points to where the value goes: the value of b is copied into a, and b is unchanged.

After a ← 3, b ← 9 and a ← b, what are a and b?

Both are 9. a receives b's value; b keeps 9.

Trap

After b ← a, changing a also changes b.

It seems right because the line looks like a lasting link between the two. But it copies the value at that moment; later assignments to a leave b alone.

After a ← 2, b ← a and a ← 9, what does b hold?
  1. b received a copy of 2 before a changed.

What's examined

On the AP® Computer Science Principles exam, this topic belongs to Big Idea 3: Algorithms and Programming. Expect multiple-choice questions that ask what is displayed after a sequence of assignments, which code segment swaps or updates values correctly, or what a variable holds at a given point. For operators inside expressions, see the note on arithmetic expressions and MOD.

Practice questions

  1. Recall What does score ← score + 5 do?
  • A) It checks whether score equals score + 5.
  • B) It stores 5 in score, replacing the old value.
  • C) It adds 5 to the current value of score and stores the result in score.
  • D) It causes an error, because score cannot equal score + 5.
Show answer

Answer: C. The right side is evaluated with the current value, then stored back in score.

Why the others are wrong:

  • A treats the arrow as a comparison; comparison uses =.
  • B ignores the old value used on the right side.
  • D reads assignment as a math equation.
  1. Apply What is displayed as a result of running the code segment?
n ← 7
m ← n
n ← n + 3
DISPLAY(m)
DISPLAY(n)
Show answer

Answer: 7 10

Marking points:

  • m ← n copies 7 into m.
  • n ← n + 3 changes only n, to 10.
  1. Apply After this code segment runs, what value does c hold?
a ← 4
b ← a * 2
c ← b - a
a ← c + b
c ← a + c
Show answer

Answer: 16

Marking points:

  • b is 8, then c is 8 − 4 = 4.
  • a becomes 4 + 8 = 12, then c becomes 12 + 4 = 16.
  1. Recall Which name best describes a variable that holds the number of tickets still available?
  • A) x
  • B) tickets1
  • C) t
  • D) ticketsLeft
Show answer

Answer: D. It says what the value means: the tickets that are left.

Why the others are wrong:

  • A gives no meaning at all.
  • B suggests a first set of tickets, not the number remaining.
  • C is too short to show what it counts.
  1. Exam-style What is displayed as a result of running the code segment?
x ← 1
y ← 2
z ← 3
temp ← x
x ← y
y ← z
z ← temp
DISPLAY(x)
DISPLAY(y)
DISPLAY(z)
Show answer

Answer: 2 3 1

Marking points:

  • temp saves 1; x takes 2 and y takes 3.
  • z takes the saved 1, so the three values rotate.
  1. Exam-style This code segment should swap the values of left and right, but it does not.
1: left ← 6
2: right ← 9
3: temp ← left
4: left ← right
5: right ← left

Which change makes it swap the values?

  • A) Change line 5 to right ← temp
  • B) Change line 3 to temp ← right
  • C) Change line 4 to left ← temp
  • D) Swap the order of lines 4 and 5
Show answer

Answer: A. temp holds the original 6, so right ← temp gives left 9 and right 6.

Why the others are wrong:

  • B saves the wrong value: the code still ends with 9 and 9.
  • C copies the saved 6 back into left, so both end as 6.
  • D overwrites right first, so both end as 6.
  1. Recall Name the symbol the exam's pseudocode uses to assign a value to a variable.
Show answer

Answer: The left arrow, ←

Marking points:

  • a ← expression assigns; = is used only to compare.

Key points

Recall, then reveal

Say the main points from memory first, then check them.

Show key pointsHide key points
  • A variable holds one value at a time: the one most recently assigned.
  • a ← expression evaluates the right side first, then replaces the value on the left.
  • Copying with b ← a copies a value; later changes to a do not reach b.
  • A swap saves one value in a temporary variable before overwriting it.
  • Meaningful names do not change how code runs, but they make it far easier to read and check.

Keep learning

Explore AP Computer Science Principles tutoring →

View the full Computer Science Principles syllabus →

More in this unit

Algorithms and Programming

All Variables, Expressions and Strings notes

Part of Novark's AP Computer Science Principles notes