Beginner Coding with Blocks
Chapter
>
Level
Conditions and Decisions
Basic if statement
There are times you may wish to add conditions to certain actions you take, this would allow your code to be flexible and perform different actions depending on the situation. For this we use if statements in combination with comparison operators to test certain conditions and perform appropriate actions.
In this first exercise, collect eggs and use the equals (=) and not equals (≠) comparison blocks to check if certain values are the same or different from one another. The if block with an else section allows you to run different code based on whether a condition is true or false.

Objective
Collect eggs and compare them using conditions in an
ifblock.
Walk through the field and collect all the eggs. Then use conditions to compare the egg types you collected.
Use the if block with an else section to make decisions in your code. The if block checks if a condition is true or false, and runs different code based on the result.
Walk to the first X mark and use an if block with an else section. In the condition, use the equals comparison block (=) to compare two text blocks: "blue egg" and "red egg". If the condition is true (they are equal), use the speak block to say "This statement is True!". Otherwise, use the speak block in the else section to say "This statement is False!".
Since "blue egg" and "red egg" are different, the condition will be false, so the code in the else section will run.
Walk to the second X mark and use another if block. This time, use the not equals comparison block (≠) to compare "blue egg" with "blue egg". Since they are the same, the condition will be false, and the code in the else section will run.
Walk to the third X mark and use an if block to compare a variable with the constant. Create a variable named collected_egg and set it to "blue egg". Then use the equals block to compare the collected_egg variable with the egg_type constant. Since both are "blue egg", the condition will be true.