Thinking in Steps

Making decisions in programs

Test whether a condition is true, and work out which branch of an if-otherwise step a program takes.

So far every program has done exactly the same thing every time. That changes the moment a program can ask a question. A condition is a statement that is either true or false — 8 is more than 3 is true, 5 is less than 2 is false — and a program uses one to decide which of two paths to take.

More than, less than, at least

More than 10 does not include 10. Less than 5 does not include 5. At least 40 does include 40. These edges are where nearly every wrong answer lives, so when a number is exactly equal to the one in the condition, slow down and read the words again.

Worked example

A program says: if the age is at least 13, print Teen; otherwise print Child. The age is 13. What does it print?

  1. Write down the condition and the value: at least 13, and the age is 13.

    Separating the rule from the value stops you testing the wrong number.

Try it together

Now work through this one: if the score is more than 50, print Win; otherwise print Try again. The score is 50.

Test the condition first, then say which branch runs.

    1.Is 50 more than 50? Answer yes or no.

    Have a go

    Have a go on your own. A program says: if the number is less than 20, print "Small"; otherwise print "Large". The number is 12. What does the program print?

    Ready to practice?

    Eight questions on what you have just read. Nothing is timed, and you can play as many times as you like.

    Print a worksheet