Variables and Data

⏱ 8 min✏️ Quiz at the end

Algorithms Need Memory

Follow this conversation: "Think of a number. Double it. Add 4. What do you have now?"

To answer, you had to remember the number at each step. Algorithms are the same β€” they need memory to keep track of things while they run. That memory comes in the form of variables.

A Variable Is a Labelled Box

A variable has two parts:

  • a name β€” the label on the box, like score
  • a value β€” what is inside right now, like 10
   score          lives          playerName
  β”Œβ”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚  10  β”‚      β”‚   3  β”‚       β”‚  "Alex"  β”‚
  β””β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Two rules make variables behave:

  1. One value at a time β€” putting in a new value throws away the old one
  2. Reading doesn't empty the box β€” you can look at the value as often as you like

Setting and Changing Values

In pseudocode:

SET score = 0        (put 0 in the box)
ADD 5 TO score       (score is now 5)
ADD 3 TO score       (score is now 8)
SET score = 100      (old value replaced β€” score is now 100)

Trace tables track variables while you trace an algorithm β€” one column per variable, one row per step:

Stepscore
SET score = 00
ADD 55
ADD 38

The most important pattern in all of programming is the running total:

SET total = total + cost

Read it right-to-left: take the current total, add the cost, store the result back in total. Every checkout, scoreboard, and step counter works this way.

Types of Data

Different kinds of information need different data types:

TypeHoldsExamples
Numberquantities42, 3.14, βˆ’7
Stringtext"Hello", "Alex", "abc123"
Booleantrue or falsetrue, false

Booleans connect directly to conditions: the answer to "is score > 10?" is a Boolean, and it can be stored β€” SET gameOver = true.

Why do types matter? Because 7 + 7 should give 14, but joining the strings "7" and "7" gives "77". The computer needs to know which one you mean.

Naming Variables Well

x, thing, and stuff2 are legal names β€” and terrible ones. Compare:

IF x > 3 THEN ...              IF numberOfLives > 3 THEN ...

The second version explains itself. Good names are a form of abstraction: the name summarises what the value means, so readers don't have to work it out.

Worked Example β€” Lives in a Game

SET lives = 3
(player is hit)   SUBTRACT 1 FROM lives    β†’ lives is 2
(player is hit)   SUBTRACT 1 FROM lives    β†’ lives is 1
IF lives = 0 THEN
    OUTPUT "Game Over"

The variable lives is set once, changed by events, and read by a condition. Storing, changing, testing β€” that is the entire life of a variable.

Scope: Where a Variable Can Be Seen

One more idea matters once programs grow beyond a few lines: scope β€” where in a program a variable can be used. A variable created inside one function is typically local: it exists only while that function is running, and code outside the function cannot see or change it. A variable created for the whole program to share is global: any part of the program can read or change it.

This sounds like a technicality, but it solves a real problem. If every variable in a large program were global, two unrelated functions could accidentally use the same name (total, count, i) and silently overwrite each other's values β€” a notoriously hard bug to track down, because the code looks correct in both places. Keeping variables local, and only sharing what genuinely needs sharing, is the variable-level version of abstraction: each part of a program manages its own memory, and the rest of the program does not need to know or worry about it.

Worked Example β€” Tracing a Shopping Total

Trace this pseudocode by hand, filling in a table as you go β€” exactly the discipline used in a proper trace table:

SET total = 0
SET total = total + 4.50    (a chocolate bar)
SET total = total + 12.00   (a book)
SET discount = total > 10
IF discount THEN
    SET total = total - 2.00
ENDIF
Steptotaldiscount
SET total = 00
+ 4.504.50
+ 12.0016.50
SET discount = total > 1016.50true
IF discount β†’ subtract 2.0014.50true

Two data types are working side by side here: total is a number, accumulating like the running-total pattern above, while discount is a Boolean β€” the stored result of a comparison, later used to control a decision. Storing a condition's answer in a variable, rather than recalculating it, is a small habit that appears constantly in real code once conditions get more complex.

Key Words

  • Variable β€” a named storage box whose value can change
  • Value β€” what a variable currently holds
  • Data type β€” the kind of value: number, string, or Boolean
  • Trace table β€” a table tracking each variable's value step by step