Welcome to Selection in Programs!

Have you ever wondered how a computer game knows when you have won, or how a quiz app knows whether your answer is right or wrong? Computers cannot read our minds, but they can make decisions using something called selection.

In this chapter, you will learn how to give your programs the power to make choices. Learning selection will help you create exciting quizzes, interactive games, and even control physical gadgets! Don't worry if this seems tricky at first — we will break down every idea step by step.


What is Selection?

In computer programming, selection is a construct where the computer decides which path of code to follow based on whether a condition is true or false. It controls the "flow" of your program, allowing it to choose between different actions.

An Everyday Analogy

You use selection in your everyday life without even realising it! Think about getting ready for school in the morning:

If it is raining outside, then you put on your coat.
Else (if it is not raining), you leave your coat at home.

Your brain checks the weather and makes a choice. In the exact same way, selection lets a computer check a situation and choose what to do next!

Key Takeaway

Selection means choosing which code to run based on a decision.


What is a Condition?

At the heart of every decision is a condition. A condition is a check or a question that can only have one of two possible outcomes: strictly true or strictly false.

Let's look at some examples of conditions you might see in a block-based coding program like Scratch:

• \( \text{answer} = \text{"Paris"} \) (Is the typed answer equal to "Paris"?)
touching mouse-pointer? (Is the sprite touching the mouse pointer right now?)
• \( \text{score} > 5 \) (Is the score greater than \(5\)?)

If the condition matches reality, the computer says it is true. If it does not, the computer says it is false.

Did You Know?

A statement that connects a condition to an outcome is called a conditional statement.

Key Takeaway

A condition is a test that always evaluates to either true or false.


Two Ways to Use Selection in Code

In block-based programming like Scratch, we usually build selection using two main types of blocks.

1. Single-Branch Selection (if... then)

This structure has only one branch of code:

• The computer checks the condition.
• If the condition is true, the computer runs the special blocks tucked inside.
• If the condition is false, the computer skips those blocks entirely and carries on with the rest of the program.

Example:
if \( \text{score} > 10 \) then
    say "You get a bonus star!"

2. Two-Branch Selection (if... then... else)

This structure gives the computer two clear pathways to choose from:

• If the condition is true, the computer runs the blocks directly under then.
• If the condition is false, the computer skips the first part and executes the alternate branch under else.

Example:
if \( \text{answer} = \text{"Canberra"} \) then
    say "Correct! Well done!"
else
    say "Sorry, that is incorrect. Try again!"

Key Takeaway

Use if... then for one outcome, and use if... then... else when you need two different outcomes.


Inputs, Outputs, and Quiz Programs

Selection is used all the time when building interactive programs like quizzes. To make a quiz work, we connect inputs and outputs through selection:

Input: Data supplied to the program by the user (for example, typing a response into an ask [ ] and wait block in Scratch). The computer stores this text in a special built-in block called answer.
Selection Check: The computer uses an equality operator to check if \( \text{answer} = \text{correct value} \).
Output: The action or result produced by the computer (for example, a speech bubble saying "Correct!", playing a celebration sound, or changing the costume).

Key Takeaway

Programs take an input, test it using selection, and produce a tailored output.


Planning with Branching Algorithms

Before you start snapping blocks together on screen, it is best to plan out your ideas. An algorithm is a set of step-by-step instructions.

When an algorithm includes decisions, we call it a branching algorithm. You can draw branching algorithms using:

Decision trees: Diagrams that split into branches showing different pathways.
Flowcharts: Shapes connected by arrows showing the flow of control depending on whether an answer is "Yes" (true) or "No" (false).

Planning first helps you spot logical errors before you write your code!

Key Takeaway

A branching algorithm is a plan that visualises decision points and different paths.


Selection in Physical Computing

Selection isn't just for computer screens! We also use it to control physical devices using microcontrollers like the BBC micro:bit or Crumble Controller.

In physical computing:

Inputs can come from physical switches, push buttons, or light sensors.
Selection tests whether a button is pressed (e.g., if button A is pressed).
Outputs trigger physical actions, such as turning on a motor or lighting up colourful sparkle LEDs.

Key Takeaway

Microcontrollers use selection to turn real-world sensor inputs into physical outputs like lights and movement.


The Secret of Continuous Checking

One of the biggest puzzles for young programmers is making selection work continuously while a game is running.

The Trap: The One-Time Check

If you attach an if... then block directly under when green flag clicked, the computer checks the condition once in a tiny fraction of a second right at the start. If you click the mouse a second later, nothing happens because the script has already finished running!

The Solution: Loops

To make the computer check the condition continuously throughout your game, you must place your selection block inside a forever loop (or repetition block). This tells the computer: "Keep checking this condition again and again without stopping!"

Key Takeaway

Interactive conditions (like checking if a character is touching something) must be placed inside a loop to be checked continuously.


Common Mistakes and How to Avoid Them

Don't worry if your code doesn't work right away! Finding and fixing errors is called debugging. Here are four common traps to watch out for:

1. Exact Spelling in Quizzes:
Computers look for an exact match. If your code checks for \( \text{answer} = \text{"London"} \), but the user types "london " with an extra space at the end or a typo like "Lodon", the condition evaluates to false.

2. Confusing Sequence with Selection:
Remember that in an if... then... else block, the computer will never run both branches at the same time. It picks only one branch depending on whether the condition is true or false.

3. Confusing Repetition with Selection:
if [condition] then makes a choice once.
repeat until [condition] creates a loop that runs instructions over and over until something changes.

4. Skipping the Planning Stage:
Jumping straight into coding without designing a branching algorithm first can lead to messy, confusing scripts. Always sketch your decisions first!


Quick Summary Checklist

Before you start your next coding project, review these key ideas:

Selection allows computers to make choices and direct the flow of a program.
• A condition is a check that results in either true or false.
if... then creates a single branch; if... then... else creates two alternative branches.
• Quizzes use inputs (user text), selection (checking the answer), and outputs (feedback).
• Place selection statements inside a forever loop when you need continuous checking in games.