Skip main navigation

What is selection in programming?

In programming, you can use conditions to determine whether or not to run a section of code. This is known as selection – read more here.

In the previous steps, you have considered what conditions are, and looked at how you can use conditions in programming to control loops. In this step, you’ll look at another way that you can use conditions in programming — selection.

Using selection

In programming, selection allows you to choose whether or not to run a section of code. It opens up a wide range of possibilities as it means the commands that are or aren’t run can be selected based on a condition being met. This could be some input from a user, or a sensor, for example:

  • A sprite that changes direction when it reaches the edge of the screen, without the programmer having to work out when that will be
  • A user answering a question that changes what happens in a story
  • A sensor that detects a low-moisture level, causing the program to open a valve to water a plant

What selection looks like in programs

In its simplest form, selection in programming is an if … then … statement that looks like this:

If <condition> then <code to run>

A Scratch block 'if my condition = true then', containing another block 'code to run'.

The code to run is enclosed inside the if … then … statement. If the condition is met (it is true), then the code enclosed will run. If the condition is not met, this code will not run. After the enclosed code has been run or skipped, the program will continue.

You can write an if … then … statement to represent the examples of selection in programming that I gave you earlier in this step.

  • If the sprite is at the edge of the screen, then change direction
  • If a user responds with the correct answer, then display “Well done”
  • If the moisture level is below an acceptable level, then open the valve to water the plant

If … then … else

Sometimes you will want to tell your program to take one of two different actions, depending on whether a condition is met or not. Selection allows you to do this using an if … then … else statement.

Look at the following example of selection in the real world, which uses an if … then … statement:

If it is sunny, then play football outside.

In this example, the conditional statement being checked is, “Is it sunny?” If true, the action commences: playing football outside. However, what if the condition were false, and it were not sunny? You can add in ‘else’ and another action to take.

If it is sunny, then play football outside, else play a board game inside.

By using an if … then … else statement, there are two possible (but exclusive) outcomes. If the condition is true, then play football outdoors, and if the condition is false, then play a board game indoors.

Another example of conditional statements using if … then … else

Which shoes should I wear?

If the temperature is over 25°C, then put on sandals, else put on trainers.

In this example, the conditional statement is, “Is the temperature > 25°C?” If this condition is satisfied as true, then you put on sandals, but if the condition is not satisfied (the temperature is 25°C or below), then you put on trainers.

You can represent the above example as a flowchart:

A flowchart starting with two boxes, the first saying "What shoes should I wear?" and the second saying "Is the temperature greater than 25 degrees?" Two paths come out from the second box. One path is labelled True and leads to a box saying "Then: Put on sandals". The other path is labelled False and leads to a box saying "Else: Put on trainers".

This illustrates a decision being made at the conditional statement, which determines the flow through the diagram. If the condition is met, then you carry on down the left branch of the flowchart, and take the action “Put on sandals”. If the condition is not met, you take the right branch and perform the action “Put on trainers”. The flow goes down one side or the other — you ignore the action on the other side.

Else “carry on”

When you are thinking about selection, you should consider if you actually need to use ` … else in your statement at all. If you want a behaviour just to continue if a condition isn't met you don't need to use … else`. In this example the else part isn’t needed:

If the alarm goes off then
wake up
else
carry on sleeping

Using else allows you to choose two different actions: if the condition is met then do something, else do something different.

Task

In the comments section, share some real-life examples of if … then … else statements. Make sure your ` … else` part is not just continuing on with an action!

This article is from the free online

Introduction to Programming with Scratch

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now