Unit 3: Programming · Variables

Article

Changing what a variable holds

7 min read

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:

AP pseudocode
count ← 0
count ← count + 1
count ← count + 1
count ← count + 1
DISPLAY(count)
Runs in your browser

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:

  1. Work out the right-hand side, using the values the variables hold right now. (count holds 0, so count + 1 is 1.)
  2. Store the answer in the variable on the left. (count now 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 statementcount
count ← 00
count ← count + 11
count ← count + 12
count ← count + 13

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?

AP pseudocode
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:

AP pseudocode
x ← 4
x ← x * 2
x ← x + 1
DISPLAY(x)
Runs in your browser
After this statementx
x ← 44
x ← x * 28
x ← x + 19

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:

AP pseudocode
a ← 3
b ← 8
a ← b
b ← a
After this statementab
a ← 33(nothing yet)
b ← 838
a ← b88
b ← a88

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

  1. temp ← a

    save a's value (3) in a safe place

    then
  2. a ← b

    a takes b's value (8). Nothing is lost, because 3 is saved

    then
  3. b ← temp

    b takes the saved value (3)

Save the first value, overwrite it, then use the saved copy.
AP pseudocode
a ← 3
b ← 8
temp ← a
a ← b
b ← temp
Runs in your browser
After this statementabtemp
a ← 33(nothing yet)(nothing yet)
b ← 838(nothing yet)
temp ← a383
a ← b883
b ← temp833

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?

AP pseudocode
a ← 4
b ← 9
a ← b
b ← a
DISPLAY(a)
DISPLAY(b)

Worked example

Trace this program and say what it shows.

AP pseudocode
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 linepointsbonus
15(nothing yet)
252
372
476
5136

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 + 1 as 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, bonus is still 2. It only becomes 6 on Line 4.
  • Swapping without a third variable. a ← b followed by b ← a leaves both variables with the same value.
  • Updating a variable that has no value yet. total ← total + 5 fails if total was never assigned.
  • Forgetting that the old value is gone. After x ← x * 2, the original value of x is 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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.