Conditional
Conditional statements are central to computer programming. They make it possible to express statements like:
If there is a path to the left, turn left
If score = 100, print “Well done!”
See additional information about conditional statements.
If blocks
The simplest conditional statement is an if block, as shown:
When run, this will compare the value of the variable x to 100. If it is larger, “What a big number!” will be printed. Otherwise, nothing happens.
Note
Do not use the equals (=) operator to compare floating-point numbers!
Floating-point numbers (numbers with decimal points, such as 3.14 or 0.1) cannot be reliably compared using the equals (=) operator due to how computers represent these numbers internally. Small rounding errors can cause two values that appear identical to be considered different.
For example, a calculation might result in 0.30000000000000004 instead of 0.3, causing an equality check to fail unexpectedly.
Instead, use greater than (>) or less than (<) comparisons, or check if the
difference between two values is smaller than a threshold (e.g., check if
|a - b| < 0.0001).
This is a common source of errors, especially for users who are not familiar with programming. If your logic block is not working as expected when comparing decimal numbers, this is likely the cause.
If-Else blocks
It is also possible to specify that something should happen if the condition is not true, as shown in this example:
As with the previous block, “What a big number!” will be printed if x > 100; otherwise, “That’s not very big.” will be printed.
An if block may have zero or one else sections but not more than one.
If-Else-If blocks
It is also possible to test multiple conditions with a single if block by adding else if clauses:
The block first checks if x > 100, printing “What a big number!” if it is. If it is not, it goes on to check if x = 42. If so, it prints “That’s my lucky number.” Otherwise, nothing happens.
An if block may have any number of else if sections. Conditions are evaluated top to bottom until one is satisfied, or until no more conditions are left.
If-Else-If-Else blocks
As shown here, if blocks may have both else if and else sections:
The else section guarantees that some action is performed, even if none of the prior conditions are true.
An else section may occur after any number of else if sections, including zero.
Block Modification
Only the plain if block appears in the toolbox:
To add else if and else clauses, the click on the gear icon, which opens a new window:
Drag else if and else clauses under the if block, as well as reordering and removing them. When finished, click on the gear icon, which closes the window, as shown here:
Note that the shapes of the blocks allows any number of else if subblocks to be added but only up to one else block.
Portions of this documentation are adapted from the Blockly Wiki by Raspberry Pi Foundation, available at https://github.com/RaspberryPiFoundation/blockly/wiki, licensed under the Creative Commons Attribution 4.0 International License (https://creativecommons.org/licenses/by/4.0/). Some modifications have been made to the original content.