AP® Computer Science Principles review sheet from Aim for Five (aimforfive.com/csp/units/3/3-1)
Unit 3 · Topic 3.1
3.1 Variables and Assignments
Variables are how a program remembers things. This topic covers what a variable holds, the kinds of values it can hold, and exactly how the assignment arrow ← works, which is the foundation for tracing every code question on the exam.
Key terms
- variable
- assignment operator (←)
- value
- data type
- meaningful variable name
What a variable is
A variable is a name in a program that holds a value. Think of it as a labeled box: the label is the name, and the box holds one value at a time. That one value can itself be a list holding many values.
A variable is an abstraction: you work with the name score instead of worrying about where in memory the value is stored or which bits represent it.
Many languages sort values into types. The ones you need are numbers, Booleans (true or false), strings (text like "hello") and lists. Some values fit one type better than another: a phone number is better stored as a string than as a number, because you never do math with it and it may start with 0.
Meaningful names make code readable. totalCost tells a reader what the value means; x7 doesn't.
How assignment works
On the exam, a ← expression is assignment. It works in two steps: first it works out the value of the expression on the right, then it stores a copy of that value in the variable on the left, replacing whatever was there before.
That's why count ← count + 1 makes sense. It isn't an equation. It means "take the current value of count, add 1, and store the result back in count."
A variable always holds the most recent value assigned to it. Copying a value into another variable doesn't link the two:
x ← 10
y ← x
x ← x + 5
DISPLAY(y)
DISPLAY(x)
This displays 10 15. When y ← x ran, y got a copy of 10. Changing x afterward has no effect on y.
Tracing assignments
To trace code, keep a small table with a column for each variable and update it one line at a time. Never jump ahead; each line uses the values as they are at that moment.
Note that = on the exam is a comparison (is a equal to b?), not assignment. Assignment is always the arrow ←. In block-based questions, assignment shows up as a block like "a ← expression" with the arrow drawn in.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Trace a sequence of assignments
What does this code segment display?
a ← 4 b ← a * 2 a ← b - 1 b ← a + b DISPLAY(a) DISPLAY(b)Show the solutionHide the solution
- Step 1: Line 1: a = 4.
- Step 2: Line 2: b = a * 2 = 4 * 2 = 8. Now a = 4, b = 8.
- Step 3: Line 3: a = b - 1 = 8 - 1 = 7. Now a = 7, b = 8.
- Step 4: Line 4: b = a + b = 7 + 8 = 15, using the new value of a. Now a = 7, b = 15.
- Step 5: DISPLAY shows each value followed by a space.
Answer: 7 15
- Example 2
The swap trap
A student wants to swap the values of two variables and writes this code. What does it display, and how should it be fixed?
first ← "red" second ← "blue" first ← second second ← first DISPLAY(first) DISPLAY(second)Show the solutionHide the solution
- Step 1: After
first ← second, first holds "blue". The original "red" is gone; nothing saved it. - Step 2: Then
second ← firstcopies "blue" into second. Both now hold "blue". - Step 3: So it displays blue blue, not blue red.
- Step 4: Fix: save one value in a temporary variable before overwriting it:
first ← "red" second ← "blue" temp ← first first ← second second ← temp DISPLAY(first) DISPLAY(second)This displays blue red.
Answer: It displays blue blue. Use a third variable:
temp ← first, thenfirst ← second, thensecond ← temp. - Step 1: After
Common mistakes
- Reading
←as a link, so that changing one variable changes another. Assignment copies the value at that moment. - Using an old value after it has been replaced. Trace one line at a time and update your table immediately.
- Confusing
=(comparison, gives true or false) with←(assignment).
On the exam
- Many questions are pure tracing: a short sequence of assignments followed by DISPLAY. A variable table makes them quick and reliable.
- A common distractor is the answer you'd get by using a variable's old value. Double-check every line that reuses a variable that has just changed.
Connected topics
Videos
Check yourself
4 questions on 3.1 Variables and Assignments. Pick an answer to see if you got it, and why.
What is displayed when the following code segment is run?
a ← 4
b ← a
a ← 9
b ← b + a
DISPLAY(a)
DISPLAY(b)
The variables x and y hold two different numbers. Which code segment swaps their values, so that x ends up with y's original value and y ends up with x's original value?
A library app stores several facts about each book. Which fact is best stored as a Boolean value?
What are the values of x and y after the following code segment is run?
x ← 2
y ← x * 3
x ← y - x
y ← x + y
0 of 4 answered