Runs a sequence of code if the specified condition is true.
Programs often need to check if a condition is true before running code. If the condition is true, the sequence of code inside of the block will run. If the condition is false, the code will be skipped over.
Clicking on the star brings up options to add “else if” and “else” conditions. “Else if” allows multiple conditions to be checked. If the first “if” condition is not true, the program will go down the line and check for the first “else if” condition that is true. If none of the conditions are true, and an “else” section exists, then the code inside of “else” will run.
Examples
Example 1
This simple program checks if a number is greater than another. Click on the gold coin, and the green critter will guess a random number between 1 and 25. If the green critter guesses a number that is higher than the one the princess critter was thinking of, he will get her gold coin!
Example 2
The bear is looking for the perfect sized stew. Help it check the stews until it finds the right one. If the stew is too big or small, the bear will let you know. Else, the stew is the perfect size.
Syntax
controls_if_if
Parameters
Name | Type | Required? | Description |
---|---|---|---|
condition | A sequence of blocks, including logic blocks, that the computer can evaluate as either true or false |
Tips
- Unlike an event, an if statement does not constantly monitor the program to check if the condition is true or false. An if statement is an instruction just like any other that gets executed by line in order from top to bottom.
Found a bug in the documentation? Let us know at support@code.org.