- Computers and coding
- Classes 6-8
- 4 min read
If-then decisions (conditions)
Learn how computers make choices using if, then and else, with true or false conditions and flowcharts.
Let us begin
The big idea
If it is raining, take an umbrella. Otherwise, wear a cap. You make choices like this every day. Computers do it too, using conditions that are either true or false.
A condition is a question that has only two answers: true or false. For example, 7 > 5 is true. 3 > 8 is false. Is it raining? is a condition too.
An if-then rule says: if the condition is true, then do this. We can add else: if it is not true, do something different. So a computer follows one path or the other, never both.
We can join conditions. AND means both must be true. OR means at least one must be true. For example, play cricket if it is Sunday AND it is not raining.
We can also chain rules: if the first is not true, check the next one. A flowchart draws this using a diamond for the question and arrows for the yes and no paths.
Worked examples
See how it works
Pass or fail
Rule: if marks are 33 or more, then Pass, else Fail. For 45: is 45 >= 33? Yes, so Pass. For 28: is 28 >= 33? No, so Fail. For exactly 33: 33 >= 33 is true, so Pass.
Even or odd
Rule: if the number can be divided by 2 with nothing left over, say even, else say odd. For 14: 14 / 2 = 7, no remainder, so even. For 7: 7 / 2 leaves 1, so odd.
Ticket price
Rule: if age is less than 5, free. Else if age is 12 or less, half price. Else, full price. Age 4: free. Age 10: not under 5, but 12 or less, so half price. Age 30: neither, so full price.
Using AND
Rule: play cricket if it is Sunday AND it is not raining. On a Sunday that is raining, the second part is false, so AND fails and we do not play.
Try it at home
Even-odd clapping game
You will need
- paper slips
- pencil
- a friend or family member
What to do
- Write numbers from 1 to 10 on slips of paper and fold them.
- Make a rule: if the number is even, clap. Else, stomp.
- Pick a slip and follow the rule. Your friend checks if you are correct.
- Make a new rule, like: if the number is more than 5, jump. Else, spin.
- Write your rule as an if-then-else sentence.
Ask a grown-up: An adult can help make up extra rules and check the answers. Keep the movements gentle.
Practice sheet
Check what you learned
Practice sheet: If-then decisions (conditions) (Classes 6-8, Computers and coding)
- Is 7 > 5 true or false?
- Complete the rule: If it is hot, then ____.
- What does else do in an if-then-else rule?
- Rule: if a number is greater than 10, say Big, else say Small. What do we say for 10?
- Rule: if marks are 33 or more, Pass. Does 33 pass?
- Ticket rule: under 5 free, 12 or less half price, else full price. What does a 12-year-old pay?
- Play cricket if it is Sunday AND it is not raining. It is Sunday and it is raining. Do we play?
- Take a bag if you are going shopping OR going to school. You are only going to school. Do you take a bag?
- Rule: if the last digit is 0 or 5, the number divides by 5, else it does not. Use it for 1,203.
- Write an if-then-else rule for crossing a road with a signal.
- Is 2,345 divisible by 5? Use the last-digit rule and check by dividing.
Answer key
Answers: If-then decisions (conditions)
- True.
- Sample: wear a hat (or drink water).
- It gives the other action to do when the condition is not true.
- Small, because 10 is not greater than 10.
- Yes, because 33 is 33 or more.
- Half price, because 12 is not under 5 but is 12 or less.
- No, because AND needs both parts to be true and it is raining.
- Yes, because with OR one true part is enough.
- The last digit is 3, so it is not divisible by 5.
- Sample: If the signal is green and the road is clear, then cross, else wait.
- Yes. The last digit is 5, and 2,345 / 5 = 469 with nothing left over.
