Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

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.

Get the whole book: Computers and coding for classes 6 to 8 (8 lessons, free to print)

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.

Memory boxes before and after an instructionThree labelled boxes, score, lives and name, hold the values 10, 3 and Asha. An arrow labelled score = score + 5 points to a second row of the same boxes where score now holds 15 and the others are unchanged.Before score 10 lives 3 name "Asha" score = score + 5 After score 15 lives 3 name "Asha"
Each variable is a labelled box. An instruction changes only the box it names; the others keep their values.

Worked examples

See how it works

  1. 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.

  2. 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.

  3. 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

  1. Label the bowls a, b and total. Put 3 caps in a and 5 caps in b. This is a = 3, b = 5.
  2. 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.
  3. Do "a = a + 2": add 2 caps to bowl a. Now a holds 5. Does total change? No, it still holds 8.
  4. Write a trace table on paper with columns a, b, total and fill in a row after each step.
  5. Make up three new lines of your own and let someone else carry them out with the bowls.

Practice sheet

Check what you learned

Write your answers in a notebook, or print this sheet and write on it. When you finish, mark your work with the answer key.

Practice sheet: Variables: storing values (Classes 6-8, Computers and coding)

  1. What two things does every variable have?
  2. After "marks = 40" and then "marks = marks + 10", what is in marks?
  3. Trace: p = 6, q = 2, r = p * q. What is r?
  4. In the trace above, if we then write p = 1, does r change?
  5. Why does swapping x and y need a third variable?
  6. Which is a better variable name for the number of students: n or students? Why?
  7. count = 1; repeat 4 times: count = count + 3. What is count at the end?
  8. In "total = a + b", do the values in a and b get used up?
  9. What is a trace table used for?
  10. After a = 5, b = a, a = 8, what are a and b?

Answer key

Answers: Variables: storing values

Do the practice sheet first, then check here. If an answer is different from yours, read the lesson again and try once more.

  1. A name and a value.
  2. 50
  3. 12, because 6 times 2 is 12.
  4. No, r stays 12; it was calculated before p changed.
  5. Writing x = y first overwrites x, so the old value of x would be lost. temp keeps a copy.
  6. students, because it tells the reader what the value means.
  7. 13: the trace is 1, 4, 7, 10, 13.
  8. No, the values are copied, not moved; a and b keep their values.
  9. Following a program line by line on paper, writing each variable value after every step, to see what happens and find mistakes.
  10. a is 8 and b is 5, because b copied the value 5 before a was changed.
WhatsApp us