Recap
- A variable is a named abstraction that holds one value at a time (number, Boolean, string or list).
a ← expressionevaluates the expression, then stores a copy of the result ina, replacing the old value.- A variable always holds the most recent value assigned. Earlier values are lost.
b ← acopies the value once. Later changes toado not changeb.- Assigning a list to a list variable stores a copy of the list.
- Meaningful names (
secondsLeft, nots) help readers; they do not change behaviour.
Key relationships
| Idea | Notation or rule | Example |
|---|---|---|
| Assignment | variable ← expression |
total ← 0 |
| Update using itself | Right side uses the old value | count ← count + 1 |
| Equality test | a = b evaluates to true or false |
score = 10 |
| Swap | Save the overwritten value first | temp ← a, a ← b, b ← temp |
| List copy | aList ← bList copies the list |
Later edits to bList do not alter aList |
Assumptions
- Statements run top to bottom, one at a time.
DISPLAYshows a value followed by a space.- On the exam, a variable that has not been given a value is not used before assignment.
Mistakes to avoid
- Using an old value when a variable has since been reassigned.
- Reading
a ← bbackwards. The variable on the left is the one that changes. - Swapping with two assignments and no temporary variable, which leaves both with the same value.
- Treating
←as a lasting link (“b follows a”). - Confusing
←(assignment) with=(comparison).
Method for “what is displayed?” questions
- List every variable across the top of a small table.
- Run one line at a time. Work out the whole right-hand side using the current values, then store it.
- Put the new value in a new row so that old values stay visible.
- Only when the last line has run, read off what
DISPLAYshows (remember each value is followed by a space). - Look at the answer options last. Wrong options are built from the typical slips above.
Worked mini-example
a ← 2
b ← a * 5
a ← b - a
b ← a + b
DISPLAY(b)
Output: 18
Row 1: a = 2. Row 2: b = 2 × 5 = 10. Row 3: a = 10 − 2 = 8. Row 4: b = 8 + 10 = 18 (the new a). A student who kept using the original a = 2 on line 4 would wrongly get 12.
Quick self-check
- After
x ← 6,y ← x,x ← 2, what doesyhold? - After
n ← 3,n ← n * n + 1, what doesnhold? - Why does
a ← bthenb ← afail to swap?
Answers
-
- The copy was made before
xchanged.
- The copy was made before
- 10 (3 × 3 + 1, using the old n on the right).
- The first line overwrites
a, so the second line copies the newaback intob; both hold the originalb.
Where marks are won and lost
Typical questions give a short code segment and ask what it displays or what a variable holds at the end. Marks go to candidates who evaluate the right-hand side with current values and who remember that assignment copies. Questions about naming or types ask you to pick the most meaningful name or the most suitable type. In the Create task, expect to identify variables or lists that represent data in your own program and explain what they store.
Back to the full study guide for worked examples and explanations.