Skip to content

Execute actions based on conditions

bartbutenaers edited this page Aug 15, 2018 · 2 revisions

Often it is required to execute some (conditional) statements when a condition is fullfilled:

IF a condition evaluates to true, we will DO some things.

Optionally other statements can be executed when the condition is not fullfilled:

IF a condition evaluates to true, we will DO some things. ELSE we will DO other things.

The following animation shows how we can have our actions depend on the payload of the input message (msg):

  1. When the payload contains 'true', we will log a warning 'true received'.
  2. When the payload contains another value (e.g. 'false'), we will log a warning 'false received'.

blockly_condition

It is also possible to do some things when multiple conditions are fullfilled. The following animation shows how to execute statements if myVariable contains '_value _1' or 'value2':

blockly_condition_or

You can add as much ELSE statements as you want. Moreover an ELSE IF can be added if another condition should be fullfilled before executing the statements in that ELSE part. However the result might become messy if the same input data is checked multiple times. For example:

image

In that case it might be better to use the switch block, where the input value needs to be specified only once. Depending on the different values, different actions will be taken:

blockly_switch