Category:Logic

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.

Parameters

Name 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 documentation@code.org