MyLeoNes™

If–then choices in programs — Technology, 7–10 years

A program can check what is happening and choose what to do next. This makes instructions react instead of always doing exactly the same thing.

A program can make a choice

A conditional is a choice with a test: if something is true, do one action; otherwise, do another. For example, a game can say, “If the player has a key, open the door; if not, keep it closed.”

Why choices are needed

A fixed list of instructions cannot react to rain, a button press or a player’s move. Programmers needed a way to describe different paths through one program. If–then choices solve that problem by letting the program notice a fact and respond to it.

A door in a game

The rule is: if keys equals 1, open the door; otherwise, show “Find a key”. Mia has 0 keys, so the test is false and the message appears. She picks up one key, the test becomes true, and the door opens.

The easy-to-miss boundary

A common mistake is checking “score is greater than 10” when the rule should include 10 too. It is reasonable because everyday language often treats “more than 10” and “10 or more” as almost the same. Computers do not; one symbol changes the result.

Choices all around us

Automatic doors can open if a sensor notices someone, and a washing machine can rinse if clothes still contain soap. Games, traffic lights and weather apps also use conditions. The program is not guessing; it is following tests written by people.

Keep exploring

Other languages

Loading MyLeoNes™…