Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Teaching Programming in Primary Schools. Join the course to learn more.

What is Selection?

So far, the programs you’ve written have performed exactly the same task each time they are run. Selection involves your program asking a yes/no question, and using the answer to decide what to do next.

Here are a few examples of selection in everyday life.

When you leave for work each morning, you’ll probably take a look out of the window and ask yourself the question “Is it raining?”. If you’re really organised you may even take a look at the weather forecast and ask yourself the similar question “Is it likely to rain today?”.

Either way, you will use the answer to this question to decide what to do next. If the answer is “yes” you will grab your umbrella before you leave the house. If the answer is “no” you’ll leave it at home.

Grabbing an Umbrella

When playing a game of “Simon Says”, each time an instruction is given to you, you’ll ask yourself the question “Did the instruction start with the words ‘Simon says’?”. If the answer is “yes”, you will follow the instructions given to you, otherwise you will do nothing.

If you’ve ever painted by numbers, you’ll know that you’re constantly using information to decide what colour each section should be painted.

Paint by Numbers

The decisions made will look something like this:

  • if the number is 1 then paint the section red
  • if the number is 2 then paint the section blue
  • if the number is 3 then paint the section yellow
  • etc.

As in the previous examples, you are using the answer to a question (such as “Is the number 1?”) to select your next action.

Share this article:

This article is from the free online course:

Teaching Programming in Primary Schools

Raspberry Pi Foundation