Skip to main content

Conditions

Conditional instructions are essential for programming. They make it possible to formulate case differentiations, such as:

  • If there is a path to the left, turn left.
  • If the number of points = 100, press “Good job!”.

Blocks

if blocks

The simplest condition is an if block:

Bild1.PNG

When it is executed, the value of the variable x is compared to 100. If it is larger, then “What a large number!” is output. Otherwise, nothing happens.

if do blocks

It is also possible to indicate that something should happen when the condition is false, as in this example:

Bild2.PNG

As in the previous block, “What a large number!” is output when x > 100. Otherwise “It's not very big’ is output.

An if block may have a do section, but not more than one.

if do else if blocks

It is also possible to test multiple conditions with a single if block, by adding do else clauses:

Bild3.PNG

The block checks first whether x > 100, and outputs “What a large number!” if this is the case. If this is not the case, it then checks whether x = 42. If so, then it outputs “That is my lucky number!”. Otherwise, nothing happens.

An if block can have any number of if do sections. The conditions are evaluated from top to bottom, until one of them is fulfilled, or until there are no more conditions left.

if do else if do else blocks

if blocks can have both if do and else if sections:

Bild4.PNG

The else if section guarantees that an action is executed, even if none of the previous conditions is true.

An else if section can also occur after any number of if do sections, including zero, which would then be a completely normal if do block.

Block modification

Only the simple if block and the if do block appear in the tool list:

image-1638372839221.png

To add if do and else clauses, click the (+) symbol. The (-) symbol can be used to remove else if clauses:

image-1638373059561.png

Note that the shapes of the blocks permit any number of else if sub-blocks to be added, but only up to one if block.