You will have seen computers making decisions.
To make a decision the code running on the computer has to check if a certain condition has been met.
The construct which does this is known as a conditional.
There are a number of locusts in this game.
Each locust has the script shown below.
The more locusts the frog catches, the more points you earn.
Q22. What is the variable in this script?
Q23. What is the condition in this script?
Q24. What will happen when the condition is been met?
Q25. How many locusts did the frog catch (take care when you answer this question?
Watch the video to see how conditions are programmed.
The if else block is used as an either / or condition in a program.
If the condition has not been met, then the program will do something else.
This is a common construct in programming.
It is possible to select one of two different alternatives using if else.
A condition can be used to test for a particular event, for example:
If one sprite touches another
If a sprite touches a colour
If a key has been pressed
or it can be used with a comparator to check for specific data, for example:
If lives = 0
If score > 100
If volume < 25
You will learn more about comparators in the next lesson.
You can test a whole series of conditions, one after the other.
Have a look at the image below. It is from a Scratch game.
The ball has to move clockwise round the track between the red and blue lines.
Read the script below and work out what the script will do when the program is run.
Q26. What will the ball do when it touches the red line?
Q27. What will the ball do when it touches the blue line?
The ball will move but stay inside the track created by the red and blue lines.
Q28. How long will the ball move for?