Key Stage 2 computing · Algorithms and programming
KS2 programming: variables, input and output explained
In Key Stage 2 computing, children move beyond simple commands and start writing programs that remember things and respond to the person using them. A variable is a named place where a program keeps a value that can change, such as a score, a name or the number of lives left. Input is what goes into a program (a key press, a click, a sensor reading) and output is what comes out (text, sound, a light or a moving robot).
Variables, input and output, explained
The easiest way to picture a variable is as a labelled box that holds one thing. The label is the variable’s name, such as score, and what is inside is its value. A program can put a starting value in the box, look at what is inside, or swap it for something new. Games rely on this all the time: lives, timers, speed and high scores are all variables. Because the box holds only one value at once, putting in a new value replaces the old one.
Input and output are how a program talks to the world. Input can be typed text, a mouse click, a touch on a screen, a spoken word, a camera image or a reading from a sensor that measures light, sound, temperature or a button press. Output can be words on a screen, a sound, a picture moving, a light switching on or a motor turning. Many schools use a small programmable device or a robot so that children see their code control something physical, such as a set of traffic lights, or simulate it on screen.
Variables only become useful when they are combined with the other building blocks children already know. Sequence puts instructions in order, selection (if, then, else) lets the program choose what to do, and repetition (a loop) repeats instructions. A program might repeat a question three times, and use selection to add one to the score only when the answer is right. Children are also taught to decompose a big problem into smaller parts: a quiz is a question part, a checking part, a scoring part and a results part.
Most children first meet this in a block-based language, where instructions snap together like jigsaw pieces. Schools decide which language to use, and some move on to a text-based language by Year 5 or Year 6. Whatever the tool, the process is the same: plan on paper what the inputs, outputs and variables will be, build the program in small pieces, test it with different inputs and then debug it. Children are expected to design and write programs that do a specific job, not just copy a set of steps.
Words your child will hear
- Variable
- A named place in a program where a value is stored and can change, such as score.
- Value
- What is stored in a variable at a given moment. If score holds 3, its value is 3.
- Input
- Data given to a program, for example typing a name, clicking a button or a sensor reading.
- Output
- What a program produces, such as a message on screen, a sound or a light switching on.
- Initialise
- Set a variable to its starting value, usually at the start of the program.
- Selection
- A choice in a program: if something is true do one thing, else do another.
- Repetition
- Repeating instructions, using a loop, a set number of times or until something happens.
What the National Curriculum says
In Key Stage 2, pupils should be taught to:
- Use sequence, selection, and repetition in programs; work with variables and various forms of input and output
- Design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts
Objectives from the National Curriculum in England, © Crown copyright, used under the Open Government Licence v3.0.
A worked example: a quiz with a score
Plan and trace a program that asks three times-tables questions, keeps a score and tells the player how they did.
- Decompose the job into parts: set up the score, ask a question, check the answer, repeat for three questions, then show the result. The one variable we need is score.
- Initialise first. At the very start, set score to 0. Then repeat three times: ask a question (this is the input, stored as the player’s answer), and use selection to check it. If the answer is correct, change score by 1. Otherwise, say “Not quite”.
set score to 0 repeat 3 times ask question, store answer if answer = correct answer change score by 1 else say Not quite - Finish with output: say the score joined to some text, such as “You scored ” then the value of score, then “ out of 3”.
- Trace it with these questions: 6 × 7, 8 × 4 and 9 × 5. The player types 42, 34 and 45.
Start score = 0 Q1 42 (right) score = 1 Q2 34 (wrong) score = 1 Q3 45 (right) score = 2
- Test it again to catch a classic bug. If “set score to 0” were missing, a second game would start from 2, because the variable keeps its old value until the program changes it.
The program says “You scored 2 out of 3”. Setting score to 0 at the start is what makes every new game begin fairly.
Common mistakes and how to help
Thinking a variable remembers every value it has held
Children often imagine a list of scores building up. A variable holds only its current value, so each new value replaces the one before.
What helps: Use a real jar with a sticky note. To change the score, the old note comes out and a new one goes in. Then show what a list would look like instead.
Mixing up “set to” and “change by”
Both blocks put a number into the variable, but “set score to 1” always gives 1, while “change score by 1” adds one to whatever was there. A score that never gets above 1 is the usual sign.
What helps: Ask your child to trace the code on paper, writing the value of the variable after every line. The mistake shows up straight away.
Forgetting to initialise the variable
The program works the first time, so it feels finished. On the second run the score, timer or number of lives carries on from where the last game ended.
What helps: Make a habit of asking, “What is this variable when the program starts?” and of testing the program twice in a row.
Typing the variable’s name instead of using the variable
Writing the word score into a message prints the word, not the number held inside it. Children are used to words standing for themselves.
What helps: Show two outputs side by side, one with the word and one with the variable block or name, and let your child spot the difference. In a text language this is often the difference between a quoted word and a bare name.
How to help at home
- Be the variable. Play any family game and ask your child to keep the score on paper. Explain that the score is a variable: it starts at 0, changes by different amounts and only the latest number matters. Try “set” and “change by” out loud.
- Spot inputs and outputs. Look around the house and on a walk for devices that sense something and respond: a doorbell, an automatic door, a thermostat, a pedestrian crossing button. Ask what the input is, what the output is and what rule joins them.
- Predict, then run. When your child shows you a program, ask what will happen if one number changes, such as the speed or the number of repeats. Let them guess first, then test it. Predicting is a core skill and it is more useful than spotting the answer afterwards.
- Plan on paper first. Before a project, ask your child to sketch what the player will type or click, what the program will show, and which values need to be remembered. Planning the variables first prevents many bugs.
Related topics
Builds on
- Key Stage 1: What is an algorithm?
- Key Stage 1: From algorithms to programs
- Key Stage 1: Creating simple programs
- Key Stage 1: Debugging simple programs
- Key Stage 1: Predicting what a program does
Leads on to
- Key Stage 3: Searching algorithms
- Key Stage 3: Sorting algorithms
- Key Stage 3: Comparing algorithms
- Key Stage 3: Text-based programming
- Key Stage 3: Lists, tables and arrays
- Key Stage 3: Functions and procedures
More on algorithms and programming
- Key Stage 2: Controlling and simulating physical systems
- Key Stage 2: Decomposition: breaking problems down
- Key Stage 2: Designing, writing and debugging programs
- Key Stage 2: Debugging algorithms and programs
- Key Stage 2: Explaining how algorithms work
- Key Stage 2: Repetition and loops
- Key Stage 2: Selection: if and else
- Key Stage 2: Sequence in programs
Updated .