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
After a ← 3 and then a ← 8, what does a hold?Show answerHide answer
- The second assignment replaces the 3.
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 incount.
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 ← bputs the value ofaintob."
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
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:
- Evaluate the right side, using the values the variables hold right now.
- 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
Show answerHide answer
- 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
| Line | a | b |
|---|---|---|
a ← 2 | 2 | |
b ← a | 2 | 2 |
a ← a + 5 | 7 | 2 |
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
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
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.
Step 1: Trace each line.
Line aba ← 55 b ← a + 25 7 a ← b * 321 7 b ← a - b21 14 WhyEach right side uses the values from the row above it, so
a - bis 21 − 7, not 5 − 7.Step 2: Read off the display.
ais 21 andbis 14, shown on one line.WhyEach
DISPLAYshows 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.
Step 1: Trace the first three lines.
xis 10,yis 4, thenx ← ymakesx4.WhyThe old value 10 is overwritten, and no other variable saved it.
Step 2: Trace the last assignment.
y ← xcopies the currentx, which is 4, soystays 4.Whyxalready 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.
Step 1: Pick test values.
Start with
first= 1 andsecond= 2; a swap must end with 2 and 1.WhyDifferent starting values show which variable each result came from.
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.
WhyA overwrites
firstbefore 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)
Step 1: Trace the first two lines.
pis 2, thenqis 2 × 4 = 8.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?
breceived a copy of 2 beforeachanged.
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
- Recall What does
score ← score + 5do?
- A) It checks whether
scoreequalsscore + 5. - B) It stores 5 in
score, replacing the old value. - C) It adds 5 to the current value of
scoreand stores the result inscore. - D) It causes an error, because
scorecannot equalscore + 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.
- 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 ← ncopies 7 intom.n ← n + 3changes onlyn, to 10.
- Apply After this code segment runs, what value does
chold?
a ← 4
b ← a * 2
c ← b - a
a ← c + b
c ← a + c
Show answer
Answer: 16
Marking points:
bis 8, thencis 8 − 4 = 4.abecomes 4 + 8 = 12, thencbecomes 12 + 4 = 16.
- 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.
- 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:
tempsaves 1;xtakes 2 andytakes 3.ztakes the saved 1, so the three values rotate.
- Exam-style This code segment should swap the values of
leftandright, 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
rightfirst, so both end as 6.
- 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 ← expressionassigns;=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 ← expressionevaluates the right side first, then replaces the value on the left.- Copying with
b ← acopies a value; later changes toado not reachb. - 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.