What are conditionals?
Have you played a game and earned extra points? Maybe you looked out the window, noticed it was raining, and then picked up an umbrella. In all these instances you were most likely working with conditions. You go about your day or play a game as usual. If something happens or when some condition is met, you change your behavior, this is a conditional.
Programs can do pretty complicated tasks, and many times they do this with conditional statements.
Conditional statements are pieces of code that ask “if, else if, and else” questions to decide if another set of code should be used. These questions are answered by doing determining if the “if, else if, and else” questions are “true” or “false”
For example you walk into a room and need to decide if the lights are off and you need to turn them on.
if the lights are off when you enter a room, turn them on
else, walk into the room with the lights already on
In the above example, we are checking to see if lights on is true or false
If lights are off (true), turn them on
If lights are on (false). walk into the room
Check it out: Check out this video from Kano to understand a little more about conditionals and the different types you can use when you code.
Making Consequences in a Game!
Conditionals are a great way for us to make interactive programs with efficient code. That is super cool! But what will we actually make? Why not a game that has consequences? For example, you can lose or gain points. Or maybe there is a way to officially win!
Before we open Scratch, let’s look at a few blocks
The above blocks are different conditions we can set in a game: if then, if then else, wait until, and repeat until.
if then: If something is either true or false (check the condition) then do the next piece of code. For example, if your sprite touches the end of the screen, then bounce
if then, else: If something is either true or false (check the condition) then do the next piece of code. ELSE, do the following piece of code. For example, For example, if your sprite touches the end of the screen, then bounce. ELSE move forward
wait until: Wait until a condition is met. For example, Wait until the broadcast block announces the game is over
repeat until: Repeat until a condition is met. For example, the background is blue until 10 seconds pass.
Try it out: Go to Scratch and try creating your own click-based game with conditions. Can you make your game with two or more conditions? Hint! One way to use conditionals would be to earn points and lose points.
Challenge Yourself: Can you make conditions that will help you announce if you won or lost a game?
Share Your Work: Let us know how you are doing. Share your Scratch project with iamtheCODE by tagging us on social media using the hashtag # VirtualCodeClubs