AP Computer Science Principles · Unit 3 lecture · Part 1 of 14 · 8 min

3.1 Variables and assignmentsOne name, one value at a time

By the end: Read, trace and write Exam Reference Sheet pseudocode with variables, expressions, strings, Booleans, conditionals, loops, lists and procedures; use linear and binary search, random values and simulations; and judge an algorithm's efficiency and whether a problem can be solved at all.
Mr. Thompkins
Welcome to Big Idea 3, the biggest part of the AP exam: 30 to 35 percent of the multiple choice. Grab a pencil. In this unit you never trace code in your head. You trace it on paper, one column per variable, every single time.

A variable is an abstraction inside a program that can hold a value. Think of it as a labeled box. The label is the variable's name, and the box holds exactly one value at a time. Each variable has data associated with it in the computer's memory, and the program reaches that data through the name.

The exam writes assignment with an arrow: score ← 10 means "store 10 in the variable score". Read the arrow from right to left. The rule for every assignment is the same two steps: first evaluate everything on the right side, using the values the variables have right now; then store the result in the variable on the left, replacing whatever was there. The value stored in a variable is always the most recent value assigned to it.

score ← score + bonus10score (before)4bonus+1. right side: 1414score (after)2. store it in scorethe 10 is gone
score ← score + bonus. Step 1 computes the right side with the current values. Step 2 stores the result, and the old value is gone.
?Wait, isn't "score ← score + 4" impossible? A number can't equal itself plus 4.
The arrow is not an equals sign. It's an instruction: take the current score, add 4, and put the result back in score. So if score was 10, it becomes 14. In math class that line would be nonsense; in a program it's the most common line there is.

Variables can hold different kinds of data. The course uses four: numbers, Booleans (true or false), strings (text in quotes) and lists (many values in order). Programmers pick meaningful names, like totalCost instead of x, because a good name makes code easier to read and fix, for other people and for you.

TypeExample valueUsed for
Number42, -7, 3.5Counting, measuring, arithmetic
Booleantrue, falseAnswers to yes-or-no questions
String"Ms. Diaz", "42"Text, in quotes
List[88, 92, 75]Many values under one name
The four data types the course uses. Notice "42" in quotes is a string, not a number.

Now let's trace a short program the way you will on the exam. Make a table: one column per variable, one row per line. Fill in a cell only when that variable changes.

1score ← 102bonus ← 43score ← score + bonus14bonus ← score * 225DISPLAY(score)6DISPLAY(bonus)
  1. line 3 Uses the old score (10), stores 14
  2. line 4 Uses the new score (14), stores 28
output › 14 28
Four assignments, then two displays.
after linescorebonus
110
2104
3144
41428
The trace. Line 4 uses the new score, 14, because line 3 already ran.
?Wait, what does DISPLAY do again?
DISPLAY(expression) shows the value of the expression on the screen, followed by a space. Two DISPLAY lines in a row show their values on one line, separated by a space, like 14 28.

One more crucial fact: assignment copies a value, once. After y ← x, y holds a copy of x's value at that moment. If x changes later, y does not follow it. That's why swapping two variables needs a third one to hold a value temporarily.

1left ← "red"2right ← "blue"3temp ← left14left ← right5right ← temp
  1. line 3 Save the old left before it is overwritten
Swapping with a temporary variable.
after lineleftrighttemp
2"red""blue"
3"red""blue""red"
4"blue""blue""red"
5"blue""red""red"
Without temp, line 4 would erase "red" before anyone saved it.
Pause and think
x ← 5, then y ← x, then x ← 9. What is y now?
I thought about it. Show me
Still 5. The assignment y ← x copied the value 5 into y. Changing x afterward doesn't reach back into y.
Trace it on paper

What is displayed when this code segment runs?

a ← 3
b ← a + 4
a ← b * 2
b ← a - b
DISPLAY(a)
DISPLAY(b)
  1. Line 1: a is 3. Line 2: the right side is 3 + 4 = 7, so b is 7.
    Why?
    Each line uses the values from the lines above it, in order. That's sequencing.
  2. Line 3: the right side is b * 2 = 7 * 2 = 14, so a becomes 14. The 3 is gone.
  3. Line 4: the right side is a - b = 14 - 7 = 7, so b becomes 7.
    Why?
    Use the current a (14), not the original a (3). This is exactly where students slip.
  4. Lines 5 and 6 display 14, then 7.

Answer: 14 7

Stuck? Get a hint
  1. Make columns for a and b and fill them in line by line.
  2. After line 3, a is 14, not 3.
  3. Line 4 computes 14 - 7.
Check yourself
What is displayed when this code segment runs?
p ← 2
q ← 6
p ← q
q ← p + q
DISPLAY(p)
DISPLAY(q)
  1. A2 8
  2. B6 12
  3. C6 8
  4. D2 6
Show the answer

B. Line 3 copies 6 into p. Line 4 uses the new p: 6 + 6 = 12. 6 8 is the trap: it uses the old p, 2, in line 4, but p was already overwritten.

  • A variable holds one value at a time: the most recent value assigned.
  • x ← expression: evaluate the right side with current values, then store it in x.
  • Assignment copies a value. Changing one variable later doesn't change another.
  • Data types: numbers, Booleans, strings, lists. Trace with one column per variable.