Learning how computers use logic to make choices using 'If-Then' statements.
How does your phone know to turn on 'Night Mode' exactly when the sun sets, or how does a video game know you've lost a life when your health hits zero?
In computer science, selection is like a fork in the road. It is a programming tool that allows a computer to choose between different paths based on whether a condition is True or False. Without selection, computers would just follow a straight line of instructions every single time, like a music box that only plays one song. With selection, computers become 'smart' because they can react to different situations. We call these decision points conditional statements.
Quick Check
In your own words, what does 'selection' allow a computer to do?
Answer
It allows a computer to make a decision and choose between different paths based on a condition.
The most basic form of selection is the If-Then statement. It works like this: IF a condition is met, THEN do a specific action. If the condition is not met, the computer simply skips that action. For example, think about an alarm clock. The computer checks: 'Is it 7:00 AM?' If that statement is True, it plays the alarm sound. If it is False, it stays quiet and checks again a second later.
1. Look out the window. 2. IF it is raining, THEN grab an umbrella. 3. Walk to school.
Sometimes we want the computer to do something else if the condition is False. This is where we use If-Then-Else. It provides a 'Plan B.' The structure looks like this: - IF (Condition is True) - THEN (Action A) - ELSE (Action B)
In math terms, we can think of this as a comparison. If we have a variable , we might say: IF , THEN print 'Big Number', ELSE print 'Small Number'.
Imagine a player's health is represented by the variable . 1. IF 2. THEN Keep playing the game. 3. ELSE Show the 'Game Over' screen.
Quick Check
What part of the statement runs if the 'IF' condition is False?
Answer
The 'ELSE' part of the statement.
Computers don't understand 'maybe.' Every condition must result in a Boolean value, which means it is either True or False. We use comparison operators to create these conditions, such as: - Equal to: - Greater than: - Less than:
When you log into a website, the computer checks: IF , THEN let you in, ELSE show an error message.
Let be the current temperature in degrees Celsius. 1. IF 2. THEN Turn on the heater. 3. ELSE IF 4. THEN Turn on the air conditioner. 5. ELSE Turn off all systems.
Which keyword is used to provide a 'Plan B' when the first condition is False?
What is the result of a Boolean condition?
In the statement 'IF THEN print Hello', the word 'Hello' will be printed.
Review Tomorrow
Tomorrow morning, try to identify one 'If-Then-Else' decision you make during your breakfast routine.
Practice Activity
Write a short algorithm for a 'Smart Fridge' that decides if you need to buy more milk. Use the variable for the number of cartons left.