Logic

Decisions or branching in programmes are among the most important control structures in programming. They allow you to adapt your program to different conditions.

If, do relationship

The if, do block has two free block sections ‘if’ and ‘do’.

/images/2020-04-08-blockly-entscheidungen/blockly-logic-1.svg - Logo

The actions linked to the free block section ‘do’ are only executed if the condition linked to the block section ‘if’ is fulfilled.

For example, the built-in LED on the senseBox MCU should be switched on when the built-in button is pressed.

/images/2020-04-08-blockly-entscheidungen/blockly-logic-2.svg - Logo

Commands added after the ‘if, do’ block will be executed in any case, even if the condition was not fulfilled before.

else

With a click on the small cogwheel in the top left corner of the if, do block you can add a ‘else’ block section to the block. Commands stored in this block section will only be executed if the condition wasn’t fulfilled before. If the condition was filfilled, they are skipped.

For example, here the built-in LED 1 should only light up when the built-in button is pressed else the built-in LED 2 should be switched on.

/images/2020-04-08-blockly-entscheidungen/blockly-logic-3.svg - Logo

else if

With a click on the small cogwheel in the top left corner of the if, do block, you can add a ‘else if’ block section to the block. Now first the top ‘if’ condition is checked, if it is not fulfilled the following ‘else if’ condition is checked. This way only those commands are executed whose associated condition is true.

For example, depending on the temperature an LED should be switched on. At temperatures below 0°C LED 1 should be switched on, at exactly 0°C LED 2 and at temperatures above 0°C LED 3.

/images/2020-04-08-blockly-entscheidungen/blockly-logic-4.svg - Logo

Operators

In Blockly you have the possibility to use different logical operators to compare measured values. With the help of these operators you can also formulate more complex logical expressions by logical operations.

Logical comparisons

With the block for logical comparisons values can be compared.

/images/2020-04-08-blockly-entscheidungen/blockly-logic-5.svg - Logo

Logical operations

With the block for ‘logical operations’ more complex logical Asudrucks can be formulated. For example, a lamp that only lights up when two conditions apply.

/images/2020-04-08-blockly-entscheidungen/blockly-logic-6.svg - Logo

The block offers a choice between the operators ‘and’ and ‘or’.

and links two conditions together, so that both must be met for the entire expression to be evaluated as true.

Or combines two conditions so that only one of them must be true for the entire expression to be true.

not block

With the ‘not’ block, expressions can be negated. A condition is true if the input value is false and false if it is true.

/images/2020-04-08-blockly-entscheidungen/blockly-logic-7.svg - Logo

Test block

With the ‘test’ block, a condition can be checked and actions can be defined which are executed depending on the result.

/images/2020-04-08-blockly-entscheidungen/blockly-logic-8.svg - Logo
×

Subscribe

The latest tutorials sent straight to your inbox.