Skip to content
Programming Programming basics
All classes Beginner About 12 min Free

Variables are labelled boxes

Buy notebooks at a shop with a small program: put in the price, how many and the money you pay, then step through it and watch each variable's box fill and change until the total and the change are printed.

Core concept

A variable is a named box that holds one value; an assignment works out the right-hand side and puts the answer in the box, replacing what was there, and the program uses the latest value

Topic: Programming basics

A program needs somewhere to keep numbers while it works, like a shopkeeper writing figures on a slip of paper. That place is a variable: A named box that holds a value the program can read and change. All key words: a box with a name on it, such as price or total. INPUT puts a value the user gives into a box. A line such as total ← price × quantity is an assignment: the computer works out the right-hand side first, then puts the answer into the box on the left, replacing whatever was in it before. OUTPUT shows a value to the user. Input, processing and output are the three parts of almost every program.

Key words explained

What you will learn

  • Explain what a variable is and what an assignment does.
  • Trace a short program and say what each variable holds after each line.
  • Tell input, processing and output apart.
  • Change the inputs and predict the output.

Before you start

  • Multiply and subtract whole numbers.

Materials

  • A notebook to trace the variables

Equipment

  • None

Procedure

  1. Step through the program one line at a time and watch the boxes.
  2. Buy 5 notebooks and run it again.
  3. Pay with too little money and see what the program prints.
  4. Answer the practice challenges.