KeyStage

Key Stage 2 computing · Algorithms and programming

KS2 if and else in coding: how programs make decisions

Selection is how a program makes a decision. It checks whether something is true, such as whether a score is high enough or a key has been pressed, and then chooses what to do. In Key Stage 2, children use if and if/else instructions, write conditions with comparisons, and test their programs with different inputs to make sure every choice works.

Selection: if and else, explained

Selection is one of the three building blocks of programming in the Key Stage 2 programme of study, alongside sequence and repetition. Without it a program would do exactly the same thing every time it ran. With it, a program can react: a game can tell the difference between a correct answer and a wrong one, a character can stop when it touches a wall, and a quiz can say “Well done” only when a player has earned it. Children meet selection through the words if and else, and it often links to the idea of input, because many decisions depend on what a user types or presses.

Every selection depends on a condition: a statement that is either true or false. “Score is more than 10” is true or false at any moment. “The answer equals 12” is true or false. Children learn to build conditions with comparisons such as equal to, greater than and less than, and sometimes to join two with “and” or “or”. An if instruction runs its blocks only when the condition is true. An if/else instruction has two paths: one that runs when the condition is true and a different one that runs when it is false. Exactly one path is followed, never both.

Children use selection in many ways at this age: checking an answer in a quiz, deciding whether a character has reached the edge of the screen, or changing a message depending on a score. Older pupils may chain decisions, for example “if the score is over 20 say Gold, else if it is over 10 say Silver, else say Try again”. Some programs also have to keep checking, because a single check only looks at one moment. That link to repetition is a common stumbling block: a game that needs to notice at any time when a sprite touches a wall must check inside a loop.

Testing matters more with selection than with a simple sequence, because there are now several paths through the program. Children are taught to test each path, and especially the boundary: the values just above, on and just below the cut-off. A program that gives the right message for 3 and for 50 may still be wrong for exactly 10. Using logical reasoning to predict the outcome for chosen test values, then running the program to check, is how children detect and correct errors in programs and in algorithms.

A good question at home is “What would happen if…?” Choose a value, ask your child to predict, then try it. It builds exactly the reasoning the curriculum looks for.

Words your child will hear

Selection
A choice in a program about which instructions to run, based on a condition.
Condition
A statement that is either true or false, such as score > 10.
If
An instruction that runs a set of blocks only when its condition is true.
Else
The instructions that run when the condition in an if is false.
Comparison
A test of two values, such as equal to, greater than or less than.
Input
Information given to a program, for example typed in or from a key press.
Boundary
A value right at the cut-off in a condition, which is where mistakes often hide.

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
  • Use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

Objectives from the National Curriculum in England, © Crown copyright, used under the Open Government Licence v3.0.

Try it: testing the boundary

A game should say “Level up!” to a player who scores 10 or more, and “Keep trying” to everyone else. A child writes: if score > 10 then say “Level up!” else say “Keep trying”. Does it work?

  1. Say what the program is meant to do. Scores of 10 or more should earn “Level up!”, and scores below 10 should get “Keep trying”. The key phrase is “10 or more”, which includes 10 itself.
  2. Predict for some test values. For a score of 15 the condition 15 > 10 is true, so it says “Level up!”, which is correct. For a score of 4 the condition is false, so it says “Keep trying”, which is also correct.
  3. Test the boundary. For a score of exactly 10, the condition 10 > 10 is false, because 10 is not more than 10. The program takes the else path and says “Keep trying”, but a score of 10 should earn “Level up!”. There is a bug.
  4. Fix it. Change the condition to score ≥ 10 (“greater than or equal to”). Some tools only offer greater than, less than and equal to, so children write score > 9 instead, which works because scores are whole numbers. Both include 10.
  5. Retest all three kinds of value: below the boundary (4), on it (10) and above it (15). Now 4 gives “Keep trying”, and 10 and 15 give “Level up!”.

The first version does not work for a score of exactly 10: score > 10 is false there, so the player is wrongly told to keep trying. Using score ≥ 10 fixes it, and testing below, on and above the boundary shows that all cases are right.

Common mistakes and how to help

Getting the boundary wrong (more than versus at least)

In everyday speech, “over 10” and “10 or more” get mixed up, and children copy that into conditions.

What helps: Ask, “Does 10 count?” every time. Always test the exact boundary value and the values either side.

Expecting an if to keep watching for something

Children assume that once an if is in the program, it will notice the moment something happens. In fact an if checks its condition once, at the moment it runs, and then the program moves on.

What helps: Explain that a program only looks when it is told to. If the game needs to notice something at any time, the if must be inside a loop that keeps checking.

Believing that both the if and the else can run

Both appear on the screen in the program, so it is natural to read them one after the other like a sequence.

What helps: Use a signpost picture: at a fork, you take one path or the other. Trace a program with a true condition, then a false one, and colour in the path followed each time.

Testing only one value

If the program works for the first value tried, it is tempting to stop. But there are now at least two paths, and a bug can hide in the other one.

What helps: Make a habit of a small test table: one value for each path, plus the boundary. Tick them off one at a time.

How to help at home

  • If this, then that, at home. Make up daily rules as if/else sentences: “If it is raining, then take a coat, else take sunglasses.” Ask your child to invent odd ones and decide what would happen in each case.
  • Play a decision game. Think of a number between 1 and 20 and let your child guess. Respond only with “higher”, “lower” or “got it”. Then ask them to write the rules as if/else statements, which is the same logic a program would use.
  • Hunt for boundaries. Pick a rule such as “children under 12 get a cheaper ticket”. Ask what happens at 11, 12 and 13. The answer to the middle question is the one that matters, and it is where programs often go wrong.
  • Predict, then test. When your child shows you a program, choose a value and ask them to predict what it will do. Pick the awkward ones, such as exactly on a cut-off.
  • Sort with a flowchart. Draw a simple flowchart with a yes/no question, such as “Is the cat hungry?”, and two branches. Walk through it with different answers. It shows that selection chooses one path.

Leads on to

Everything in Key Stage 2 computing

Updated .