If-Then Decisions: How Programs Choose
Byte the friendly robot walks down a glowing hallway toward a single door labeled THEN, pausing to read a yes-or-no sign before deciding whether to open it or walk past.
- Define a condition as a yes-or-no question a program asks.
- Identify that the then-action runs when a condition is true and is skipped when the condition is false.
- Predict whether a program will take the then-action given a true or false condition.
- Build a simple if-then rule using everyday situations.
Key terms
- Condition
- A yes-or-no question a program asks.
- Then-action
- What the program does when true.
- True
- The yes answer to a condition.
- False
- The no answer to a condition.
Asking a Yes-or-No Question
When a program reaches a choice, it asks a yes-or-no question called a condition. The answer is either true or false. The condition might be is it raining or is the cup empty. The answer to that one question decides whether the next special step happens or gets skipped along the way.
When True, Do the Action
If the condition is true, the program does the then-action right away. For example, if it is raining is true, then grab an umbrella. The then-action is the one special step that runs only when the answer is yes. So a true condition makes the action happen.
When False, Just Move On
If the condition is false, the program skips the then-action and keeps going. Nothing extra happens. There is no hidden second action waiting on the false side. A bare if-then has just one special action, and that action runs only when the answer to the question is yes.
Worked examples
Run: IF the light is green, THEN walk.
- Ask the condition: is the light green? Say yes, so it is true.
- Because it is true, do the then-action.
Answer: You walk across because the condition is true.
Run: IF it is cold, THEN wear a coat. It is warm.
- Ask the condition: is it cold? The answer is no, so it is false.
- Because it is false, skip the then-action.
Answer: You do not wear a coat, and the program moves on.
Activity
Match each yes-or-no condition to the then-action that runs only when the answer is true.
Practice
Write an if-then rule about crossing the street safely.
Tell what happens to a then-action when the condition is false.
Common mistakes to avoid
- The then-action always runsThe then-action runs only when the condition is true, not always.
- A false condition runs a hidden stepA bare if-then has no extra step, so it just moves on.
Check your understanding
What is a condition in a program?
The rule says: IF it is cold, THEN wear a coat. It is NOT cold today. What does the program do?
An if-then rule says: IF the battery is low, THEN charge the phone. The battery is NOT low. What happens to the charge step?
Recap
A condition is a yes-or-no question a program asks. When the answer is true, the program does the then-action. When the answer is false, the program skips the then-action and simply keeps going to the next step.
Reflect
What yes-or-no question could start one of your own rules?