Making Decisions: Conditions
How programs choose between two paths
- say what a condition is,
- explain how if and else pick a path,
- read a simple decision-flow diagram,
- write a decision as an "If … then … else …" sentence.
1. What is a condition?
A condition is a question that can only be answered with true (yes) or false (no). There is no "maybe".
You use conditions every day, even without noticing:
- "Is it raining?" → true or false
- "Is my homework finished?" → true or false
- "Is the traffic light green?" → true or false
A program checks the condition, gets a true or false answer, and then chooses what to do next.
2. The "if it rains" idea
Think about getting ready to go outside. In your head you say:
Otherwise (else), wear sunglasses.
That is exactly how a program decides! The condition is "Is it raining?". The if part runs only when the answer is true. The else part runs only when the answer is false.
Notice something important: only one path runs. You never take an umbrella and wear sunglasses at the same time. The program picks one path and skips the other.