- Computers and coding
- Classes 6-8
- 5 min read
Variables: storing values
A variable is a labelled box in memory that holds a value; learn to set it, change it and trace it on paper step by step.
Let us begin
The big idea
When a computer does a sum or keeps a game score, it must remember numbers and words while it works. It keeps them in variables. Think of a variable as a labelled box that can hold one value at a time.
A variable has a name and a value. The name is the label on the box, such as score or age. The value is what is inside, such as 10 or "Asha". We write "score = 10" to mean: put 10 in the box called score.
The value can change; that is why it is called a variable. If we later write "score = score + 5", the computer reads the old value (10), adds 5, and puts the new value (15) back into the same box. The old value is gone.
Good names say what the box is for: total, count, name, length. A name like x works, but a name like marks is easier to understand when you read the plan later.
To trace a program on paper, draw a table with one column for each variable. After every line, write the new value in the table. This is exactly what the computer does in its memory, and it is the best way to find mistakes.
Worked examples
See how it works
A simple trace
Lines: a = 4, b = 7, c = a + b, a = a + 1. Trace: after line 1, a is 4. After line 2, b is 7. After line 3, c is 11. After line 4, a is 5 and c is still 11, because c was worked out before a changed.
Swapping two values
We want to swap x = 2 and y = 9. If we write x = y first, x becomes 9 and the 2 is lost. So we use a third box: temp = x (temp is 2), then x = y (x is 9), then y = temp (y is 2). Now x is 9 and y is 2.
Counting with a variable
count = 0, then repeat 3 times: count = count + 2. Trace: 0, then 2, then 4, then 6. The final value of count is 6.
Try it at home
Variable boxes with bottle caps
You will need
- Three small bowls or cups
- Sticky notes or paper slips for labels
- About 20 bottle caps, beans or coins
- Paper and a pencil for the trace table
What to do
- Label the bowls a, b and total. Put 3 caps in a and 5 caps in b. This is a = 3, b = 5.
- Do "total = a + b": count a and b, and put that many caps (8) into total. Do not take the caps out of a and b; the computer copies values, it does not move them.
- Do "a = a + 2": add 2 caps to bowl a. Now a holds 5. Does total change? No, it still holds 8.
- Write a trace table on paper with columns a, b, total and fill in a row after each step.
- Make up three new lines of your own and let someone else carry them out with the bowls.
Practice sheet
Check what you learned
Practice sheet: Variables: storing values (Classes 6-8, Computers and coding)
- What two things does every variable have?
- After "marks = 40" and then "marks = marks + 10", what is in marks?
- Trace: p = 6, q = 2, r = p * q. What is r?
- In the trace above, if we then write p = 1, does r change?
- Why does swapping x and y need a third variable?
- Which is a better variable name for the number of students: n or students? Why?
- count = 1; repeat 4 times: count = count + 3. What is count at the end?
- In "total = a + b", do the values in a and b get used up?
- What is a trace table used for?
- After a = 5, b = a, a = 8, what are a and b?
Answer key
Answers: Variables: storing values
- A name and a value.
- 50
- 12, because 6 times 2 is 12.
- No, r stays 12; it was calculated before p changed.
- Writing x = y first overwrites x, so the old value of x would be lost. temp keeps a copy.
- students, because it tells the reader what the value means.
- 13: the trace is 1, 4, 7, 10, 13.
- No, the values are copied, not moved; a and b keep their values.
- Following a program line by line on paper, writing each variable value after every step, to see what happens and find mistakes.
- a is 8 and b is 5, because b copied the value 5 before a was changed.
