Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

If-then decisions (conditions)

Learn how computers make choices using if, then and else, with true or false conditions and flowcharts.

Get the whole book: Computers and coding for classes 6 to 8 (8 lessons, free to print)

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.

A decision flowchartA diamond asks Is it raining. An arrow labelled Yes goes down-left to a box saying Take an umbrella. An arrow labelled No goes down-right to a box saying Wear a cap. Is it raining? YES NO Take an umbrella Wear a cap The computer follows ONE path, depending on the answer.
The computer follows only one path.

Worked examples

See how it works

  1. 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.

  2. 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.

  3. 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.

  4. 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

  1. Write numbers from 1 to 10 on slips of paper and fold them.
  2. Make a rule: if the number is even, clap. Else, stomp.
  3. Pick a slip and follow the rule. Your friend checks if you are correct.
  4. Make a new rule, like: if the number is more than 5, jump. Else, spin.
  5. 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

Write your answers in a notebook, or print this sheet and write on it. When you finish, mark your work with the answer key.

Practice sheet: If-then decisions (conditions) (Classes 6-8, Computers and coding)

  1. Is 7 > 5 true or false?
  2. Complete the rule: If it is hot, then ____.
  3. What does else do in an if-then-else rule?
  4. Rule: if a number is greater than 10, say Big, else say Small. What do we say for 10?
  5. Rule: if marks are 33 or more, Pass. Does 33 pass?
  6. Ticket rule: under 5 free, 12 or less half price, else full price. What does a 12-year-old pay?
  7. Play cricket if it is Sunday AND it is not raining. It is Sunday and it is raining. Do we play?
  8. Take a bag if you are going shopping OR going to school. You are only going to school. Do you take a bag?
  9. Rule: if the last digit is 0 or 5, the number divides by 5, else it does not. Use it for 1,203.
  10. Write an if-then-else rule for crossing a road with a signal.
  11. Is 2,345 divisible by 5? Use the last-digit rule and check by dividing.

Answer key

Answers: If-then decisions (conditions)

Do the practice sheet first, then check here. If an answer is different from yours, read the lesson again and try once more.

  1. True.
  2. Sample: wear a hat (or drink water).
  3. It gives the other action to do when the condition is not true.
  4. Small, because 10 is not greater than 10.
  5. Yes, because 33 is 33 or more.
  6. Half price, because 12 is not under 5 but is 12 or less.
  7. No, because AND needs both parts to be true and it is raining.
  8. Yes, because with OR one true part is enough.
  9. The last digit is 3, so it is not divisible by 5.
  10. Sample: If the signal is green and the road is clear, then cross, else wait.
  11. Yes. The last digit is 5, and 2,345 / 5 = 469 with nothing left over.
WhatsApp us