Making Decisions with If-Then Conditionals
Byte the robot stands at a crossroads in a colorful park, holding an umbrella in one hand and a sunhat in the other, peering up at the cloudy sky while a squirrel watches curiously from a nearby tree.
- Explain what a conditional is and why programs use them.
- Identify the two parts of an if-then statement: the condition and the action.
- Predict what a program will do when a condition is true versus false.
- Compare a real-life decision to an if-then statement written in code.
Key terms
- Conditional
- A program checking something before it acts.
- Condition
- A yes-or-no question the program asks.
- Action
- The thing the program does.
- True or false
- The only two answers a condition has.
The Two Parts
An if-then statement has two parts. The first part is the condition, which is a yes-or-no question like is it raining. The second part is the action, which is what happens, like bring an umbrella. Spotting these two parts helps you understand any if-then rule you read or write.
True Runs, False Skips
When the condition is true, the computer does the action right away. When the condition is false, the computer skips the action and moves on to the next instruction. The action never happens by itself. It only happens when the answer to the condition question is yes.
Spotting Conditions Everywhere
Look for words like if and when. Those words are clues that a conditional is happening. Then ask yourself if the part can be answered yes or no. If it can, it is a condition. If it is a step you carry out, it is the action that follows.
Worked examples
Run: if it is dark, turn on the light.
- Check the condition: is it dark? Say it is dark, so the answer is yes.
- Since the answer is true, do the action.
Answer: The light turns on because the condition is true.
Run: if the cup is full, stop pouring. The cup is not full.
- Check the condition: is the cup full? The answer is no.
- Since the answer is false, skip the action.
Answer: Pouring keeps going because the condition is false.
Activity
Sort each card into the correct box: is it the CONDITION (the check) or the ACTION (what happens)?
Practice
Write an if-then rule about what you do when you are hungry.
Name the condition and the action in your if-then rule.
Common mistakes to avoid
- The action always runsThe action only runs when the condition is true, not always.
- Any sentence is a conditionA condition must be a yes-or-no question, not a step to do.
Check your understanding
In an if-then statement, when does the computer perform the action?
Which of these is the best example of a condition in an if-then statement?
Byte writes: 'if the door is locked: ring the doorbell.' What happens if the door is NOT locked?
Which if-then statement best matches this real-life decision: 'When my homework is finished, I go outside to play'?
Recap
A conditional lets a computer check a yes-or-no condition and then choose what to do. When the condition is true the action runs. When it is false the action is skipped and the program keeps going.
Reflect
What is one if-then choice you made today?