If < > Then ... Else ...
-
Prerequisite
Description
There are two “If <> then … else …” blocks in the control category. One is a C block and the other is a Reporter block.
How do these blocks work?
The C Block
The C block checks if the condition is true or false.
If the condition is “true”, it plays the blocks which come inside the first C shape.
If the condition is “false”, it plays the blocks which come inside the second C shape.
The Reporter Block
The reporter block also checks if the condition is true or false.
If the condition is “true”, it takes the value of the first input.
If the condition is “false”, it takes the value of the second input.
Nested If <> then, else Blocks
If you put one “If then, else” block inside the other, they become “Nested”.
You can use nested if <> then, else blocks to check additional conditions based on whether the first condition is true or false.For example, as shown below, the first “if” block checks if the score is greater than or equal to 20. If it’s true, then the backdrop changes to Level 2. If it is not, then the second “if” block checks if the score is greater than or equal to 10. if it’s true, the sprite says “Good job! keep going!”, or else it says “Hurry up”.
Multiple If <> then, else Blocks
You can stack multiple “If then, else” blocks together to check multiple conditions one by one.
For Example, as shown below, If the sprite touches the Red ball, it says “I found the red ball” or else it moves to a random position. And then if the sprite touches the Yellow ball, it says “I found the yellow ball” or else it moves 10 steps.
Checking the condition repeatedly
The “If then, else” block checks its boolean condition only once. You can use a Repeat or Forever Loop if you want to repeatedly check the conditions in a game or project.