Using if statements in Scratch
Watch the video below to see Bill introducing if statements.
True or false?
Computers decide what to do by asking questions and determining whether the answers are true or false. If the answer is true, the computer might do something, if it’s false, it might do something else. The sort of questions that a program might ask is; Is something equal to something else? Is something bigger than something else? Or the program might check to see if something has happened for example, is the space bar being pressed? The answer to these questions will always be true or false and these types of programming questions are know as CONDITIONS.
It’s very easy to check whether a condition is true or false in Scratch with a simple program like this:
The condition is the green block and you will find them under the ‘Operators’ menu. If you click on the green flag to start, Scratchy the cat will say either true or false. In this case it will be true because 3 obviously equals 3.
The ‘if’ block uses conditions to decide what to do next. Whatever you put inside an ‘if’ block will only run if the condition is true.
Use the blocks shown below to create a program the makes Scratchy do things if a key is pressed.
Extension: Try changing the key from ‘Space’ to a different key. Try and get Scratchy to do different things.
Use the blocks shown below to create a program that uses ‘If … Else and a variable to check if the score is greater than 10. If true, the program should say ‘You have won!’. If false, the program should say ‘Keep going’.
Write a Scratch program that uses ‘If’ statements to detect whether a key has been pressed and then moves Scratchy in a direction. Try to get your program to move Scratchy in all four directions so you will need four ‘If’ statements.