Article
Changing what a variable holds
In a maths class, x = x + 1 can never be true. In a program, x ← x + 1 is one of the most useful statements there is. It adds 1 to a variable. This article shows why that works, and how to swap two values without losing one.
A variable that updates itself
You can use a variable's own value to work out its new value. Here is a counter:
count ← 0 count ← count + 1 count ← count + 1 count ← count + 1 DISPLAY(count)
The program shows 3. In this lesson +, - and * work as they do in maths class. The next lesson covers all the operators.
How does count ← count + 1 work? The computer does it in two steps, always in the same order:
- Work out the right-hand side, using the values the variables hold right now. (
countholds 0, socount + 1is 1.) - Store the answer in the variable on the left. (
countnow holds 1.)
Adding 1 is called incrementing and taking 1 away is called decrementing. Adding a different amount works in the same way. The trace table shows each step:
| After this statement | count |
|---|---|
count ← 0 | 0 |
count ← count + 1 | 1 |
count ← count + 1 | 2 |
count ← count + 1 | 3 |
Python also has a shorter way to write this, count += 1. It is not part of AP pseudocode, so on the exam you will always see the long form.
Check your understanding
What is the value of x after this program runs?
x ← 5 x ← x + 3 x ← x + 3
Right side first
Because the right side is worked out first, the old value is used in the sum and then replaced. Try this one in your head before you run it:
x ← 4 x ← x * 2 x ← x + 1 DISPLAY(x)
| After this statement | x |
|---|---|
x ← 4 | 4 |
x ← x * 2 | 8 |
x ← x + 1 | 9 |
The program shows 9. Every statement uses the value left by the one before it.
A variable must have a value before you use it on the right-hand side. If total has never been assigned, then total ← total + 5 has nothing to add to. Python stops with a NameError, which means "I do not know that name".
Swapping two values
Suppose a is 3 and b is 8, and you want to swap them, so that a is 8 and b is 3. The obvious attempt does not work:
a ← 3 b ← 8 a ← b b ← a
| After this statement | a | b |
|---|---|---|
a ← 3 | 3 | (nothing yet) |
b ← 8 | 3 | 8 |
a ← b | 8 | 8 |
b ← a | 8 | 8 |
Both variables end up 8. The statement a ← b threw away the 3 before anyone saved it. The fix is a third variable, often called temp (short for temporary), which keeps the 3 safe:
Swapping with a temporary variable
temp ← a
save a's value (3) in a safe place
thena ← b
a takes b's value (8). Nothing is lost, because 3 is saved
thenb ← temp
b takes the saved value (3)
a ← 3 b ← 8 temp ← a a ← b b ← temp
| After this statement | a | b | temp |
|---|---|---|---|
a ← 3 | 3 | (nothing yet) | (nothing yet) |
b ← 8 | 3 | 8 | (nothing yet) |
temp ← a | 3 | 8 | 3 |
a ← b | 8 | 8 | 3 |
b ← temp | 8 | 3 | 3 |
Now a is 8 and b is 3. Python has a one-line swap, a, b = b, a, but the exam does not use it, so learn the temp way.
Check your understanding
A programmer tries to swap the values of a and b. What does this program show?
a ← 4 b ← 9 a ← b b ← a DISPLAY(a) DISPLAY(b)
Worked example
Trace this program and say what it shows.
Line 1: points ← 5 Line 2: bonus ← 2 Line 3: points ← points + bonus Line 4: bonus ← bonus * 3 Line 5: points ← points + bonus Line 6: DISPLAY(points)
| After line | points | bonus |
|---|---|---|
| 1 | 5 | (nothing yet) |
| 2 | 5 | 2 |
| 3 | 7 | 2 |
| 4 | 7 | 6 |
| 5 | 13 | 6 |
Line 3 adds the bonus of 2 to 5, making 7. Line 4 triples the bonus, making 6. Line 5 adds the new bonus of 6 to 7, making 13. The program shows 13. Notice that bonus was 2 on Line 3 and 6 on Line 5. Always use the value the variable holds at that moment.
Common mistakes
- Treating
x ← x + 1as a false equation. It is an instruction: work out the right side, then store it. - Using the new value too early. In Line 3 above,
bonusis still 2. It only becomes 6 on Line 4. - Swapping without a third variable.
a ← bfollowed byb ← aleaves both variables with the same value. - Updating a variable that has no value yet.
total ← total + 5fails iftotalwas never assigned. - Forgetting that the old value is gone. After
x ← x * 2, the original value ofxis lost, unless you saved it somewhere.
On the exam
You will see programs where a variable is updated several times, and you must give its final value or what DISPLAY shows. Build a trace table with one column per variable and one row per statement. For a swap, check that a third variable keeps the first value safe. A swap that does not save a value is a common wrong answer.
Key words
Increment: add 1 to a variable (count ← count + 1).
Decrement: take 1 away from a variable.
Temporary variable: an extra variable that holds a value safely for a short time, as in a swap.
Overwrite: replace the value in a variable with a new one.