Unit 3: Programming · Variables

Article

Variables and assignment

7 min read

A game shows your score in the corner of the screen. When you win a coin, the score goes up. For the program to do this, it must remember the score somewhere and change it later. The place where a program remembers something is called a variable.

A variable is a labelled box

A variable is a name that stands for one stored value. Think of a box with a label on the front. The label is the variable's name. Whatever is inside the box is its value.

Name (the label)Value (inside the box)
score10
lives3

To put a value into a variable we use an assignment statement. In AP pseudocode it uses an arrow. In Python it uses an equals sign:

AP pseudocode
score ← 10
DISPLAY(score)
Runs in your browser

Read score ← 10 as "score gets 10". The arrow points to the variable that receives the value. The name is always on the left and the value is always on the right.

The second statement shows 10, not the word score. Without quote marks, DISPLAY(score) shows what is inside the box. With quote marks, DISPLAY("score") would show the word.

Try it: change 10 to a different number and run again.

Check your understanding

What does this program show?

AP pseudocode
coins ← 7
DISPLAY(coins)

A variable holds only the latest value

A box holds one thing. If you put something new in, the old thing is gone. The same is true for a variable:

AP pseudocode
score ← 10
score ← 25
DISPLAY(score)
Runs in your browser

The program shows 25. A trace table shows what happened after each statement:

After this statementscore
score ← 1010
score ← 2525

One box, two assignments

  1. score ← 10

    the box named score holds 10

    next statement
  2. score ← 25

    the box now holds 25 and the 10 is gone

    next statement
  3. DISPLAY(score)

    shows 25, the latest value

A variable always has just one value, the most recent one that was assigned.

What can a variable hold?

A variable can hold different kinds of data. Each kind is a data type.

Data typeWhat it holdsExamples
Numberwhole numbers and decimals7, 0, 2.5
Stringtext, inside quote marks"Mia", "hello"
Booleanonly true or falsetrue, false (Python: True, False)

A variable can also hold a list, which you will meet later in this unit. You do not have to say the type in advance. The value you assign decides it:

AP pseudocode
age ← 15
name ← "Mia"
isStudent ← true

You can choose any name you like, but a good name says what the variable is for. score is better than s. In Python, a name can have letters, digits and underscores, it cannot start with a digit, and it cannot have spaces. Capital letters matter: score and Score are two different variables.

Copying with a ← b

What happens when the right-hand side is another variable? The computer takes the value that variable holds right now, and puts a copy of it in the variable on the left:

AP pseudocode
a ← 5
b ← a
a ← 9
DISPLAY(b)
Runs in your browser
After this statementab
a ← 55(nothing yet)
b ← a55
a ← 995

The program shows 5. Statement 2 copied the 5 into b. When a later changed to 9, b did not change. The two boxes are separate. b ← a does not mean "b is always the same as a". It means "copy a's value into b, just this once".

Check your understanding

What does this program show?

AP pseudocode
a ← 2
b ← a
a ← 6
DISPLAY(b)

Worked example

Trace this program and say what it shows.

AP pseudocode
Line 1: lives ← 3
Line 2: coins ← 10
Line 3: lives ← coins
Line 4: coins ← 1
Line 5: DISPLAY(lives)
After linelivescoins
13(nothing yet)
2310
31010
4101

Line 3 copies the value 10 into lives, so the old 3 is gone. Line 4 changes only coins. Line 5 shows 10.

Common mistakes

  • Reading ← as "equals". x ← 5 is a command to store a value, not a statement of fact.
  • Writing it backwards. 5 ← x is not valid. The variable goes on the left.
  • Thinking a variable remembers its old values. It holds only the most recent one.
  • Thinking b ← a links the two variables. It copies the value once. Later changes to a do not reach b.
  • Putting quote marks around a variable name. DISPLAY("score") shows the word score. DISPLAY(score) shows its value.

On the exam

Many questions give a few assignments and ask for the value of a variable at the end, or what DISPLAY shows. Trace it from the top: after each line, write down what every variable holds. Watch for a variable that is assigned twice, and for a ← b statements that copy a value. Remember that in AP pseudocode = compares two values and ← stores one.

Key words

Variable: a name that stands for one stored value.

Value: the data inside a variable.

Assignment: a statement that stores a value in a variable (← in AP pseudocode, = in Python).

Data type: the kind of value, such as number, string or Boolean.

Boolean: a value that is either true or false.

String: text inside quote marks.

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.