Skip to main content

Logic

if, else if, else

The if block is a core decision-making block. It is used to check if a condition is true or false. The blocks inside the if block will only run if the condition is true.

Logic comparisons

The logic comparison block takes two values and an operator. The operator compares the two values according to the symbol used:

  • == (equal to) — Checks if the values on both sides are exactly the same.
  • != (not equal to) — Checks if the values on both sides are different.
  • < (less than) — Checks if the value on the left is smaller than the value on the right.
  • > (greater than) — Checks if the value on the left is bigger than the value on the right.
  • <= (less than or equal to) — Checks if the value on the left is either smaller than or equal to the value on the right.
  • >= (greater than or equal to) — Checks if the value on the left is either bigger than or equal to the value on the right.

The output of the block is always either true or false (a boolean value).

  • If the comparison holds true according to the operator, the block outputs true.
  • If the comparison doesn't hold true, the block outputs false.

Logic operations

The logic operation block takes two true or false inputs, often from comparison blocks or other logic operations. It performs the chosen operation (AND or OR) on those inputs and outputs a single true or false result.

AND operation

  • Result is true only if both input values are true.

OR operation

  • Result is true if at least one of the input values is true.

Here's a table summarising the truth table for AND and OR operations:

Left InputRight InputAND ResultOR Result
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

Logic negation

The negation block flips the value of the input. If the input is true, the output is false, and vice versa.

Boolean true/false

The boolean block provides the basic true and false values. Use the dropdown to select the desired value.

Behind the scenes, true and false are represented as the numbers 1 and 0, respectively. This is because computers store boolean values as binary numbers, where 1 represents true and 0 represents false.

Boolean HIGH/LOW

The boolean HIGH/LOW block provides the basic HIGH and LOW values, which represent the state of a digital pin.

Digital signals can only be in one of two states: on or off. We use the terms HIGH and LOW to represent these two states.

  • HIGH corresponds to the on state.
  • LOW corresponds to the off state.

The terms HIGH and LOW are really just aliases for true and false, respectively. This means you can use them in the exact same places as regular boolean values, for example, in if blocks.

  • HIGH is equivalent to true (or 1).
  • LOW is equivalent to false (or 0).

Null

The null block provides the null value, which represents the absence of a value.

Ternary operator

The ternary operator block is a compact version of an if/else block. It takes three inputs:

  • test — A true or false value.
  • if true — The value to output if the test is true.
  • if false — The value to output if the test is false.

The block evaluates the test value, and outputs either the if true or if false value, depending on the result.

Switch case

The switch case block is an advanced decision-making block. It is used to check a single numerical value against multiple cases.

The block has two parts:

  • value — The value to check against the cases.
  • cases — A list of cases to check against the value.

The block checks the value against each case in order. If the value matches a case, the blocks inside that case will run. If the value doesn't match any case, no blocks will run.

Adding/removing cases

You can add or remove cases by clicking the + or - buttons on the block.

warning

After a match is found, the block will run the blocks inside the matching case and all subsequent cases. You can use the break block to exit the switch block after a match.

In most cases, you should add a break block at the end of each case to prevent this behaviour.

Switch case (with default)

The switch case block with default is similar to the regular switch case block, but with an additional default case.

The default case is a catch-all case that runs if the value doesn't match any of the other cases.

Break out of switch

The break block is used to exit a switch case block early. When the block is run, the switch case block will stop running and continue with the blocks after the switch case block.