3.1 Variables and assignmentsOne name, one value at a 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.
?Wait, isn't "score ← score + 4" impossible? A number can't equal itself plus 4.
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.
| Type | Example value | Used for |
|---|---|---|
| Number | 42, -7, 3.5 | Counting, measuring, arithmetic |
| Boolean | true, false | Answers to yes-or-no questions |
| String | "Ms. Diaz", "42" | Text, in quotes |
| List | [88, 92, 75] | Many values under one name |
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)
- line 3 Uses the old score (10), stores 14
- line 4 Uses the new score (14), stores 28
| after line | score | bonus |
|---|---|---|
| 1 | 10 | |
| 2 | 10 | 4 |
| 3 | 14 | 4 |
| 4 | 14 | 28 |
?Wait, what does DISPLAY do again?
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
- line 3 Save the old left before it is overwritten
| after line | left | right | temp |
|---|---|---|---|
| 2 | "red" | "blue" | |
| 3 | "red" | "blue" | "red" |
| 4 | "blue" | "blue" | "red" |
| 5 | "blue" | "red" | "red" |
I thought about it. Show me
What is displayed when this code segment runs?
a ← 3 b ← a + 4 a ← b * 2 b ← a - b DISPLAY(a) DISPLAY(b)
- 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. - Line 3: the right side is b * 2 = 7 * 2 = 14, so a becomes 14. The 3 is gone.
- 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. - Lines 5 and 6 display 14, then 7.
Answer: 14 7
Stuck? Get a hint
- Make columns for a and b and fill them in line by line.
- After line 3, a is 14, not 3.
- Line 4 computes 14 - 7.
p ← 2 q ← 6 p ← q q ← p + q DISPLAY(p) DISPLAY(q)
- A2 8
- B6 12
- C6 8
- 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.