> 
 > 
5. Blocks Coding with Raspberry Pi
Logic
Logic

This documentation covers logical operations including conditionals, comparisons, boolean logic, and actions based on true/false evaluations, such as continuing actions, handling equality, and dealing with always true or false scenarios.

Conditionals

Continue Steps If It is Correct

If true then

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>index</variable></variables><block type=\"controls_if\" disabled=\"true\" x=\"23\" y=\"112\"><value name=\"IF0\"><shadow type=\"logic_boolean\" disabled=\"true\"><field name=\"BOOL\">TRUE</field></shadow></value></block></xml>"}

This block is used to check if the value is true, then do some actions.

Parameters: Logic (Comparison)

Output: N/A
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">index</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"controls_if\"><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">EQ</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">The number of index is 3.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}

The output:

If True Then - Output

Checking True or False and Do some Related Actions

If true then … else …

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>index</variable></variables><block type=\"controls_if\" disabled=\"true\" x=\"-428\" y=\"113\"><mutation else=\"1\"/><value name=\"IF0\"><shadow type=\"logic_boolean\" disabled=\"true\"><field name=\"BOOL\">TRUE</field></shadow></value></block></xml>"}

This block is used to check the value is true or false. If it is true, then do the first action. Otherwise, do the second action.

Parameters: Logic (Comparison)

Output: N/A
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">number</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">4</field></shadow></value><next><block type=\"controls_if\"><mutation else=\"1\"/><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">EQ</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">number</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">The number is same as 3.</field></shadow></value></block></statement><statement name=\"ELSE\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">The number is not same as 3.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}

The output:

If True Then Else - Output

Comparison

When They are the Same

0 = 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"logic_compare\" disabled=\"true\" x=\"-113\" y=\"427\"><field name=\"OP\">EQ</field><value name=\"A\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value><value name=\"B\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This block is used to return true if two values are the same.

Parameters: Condition:

  • Number (Default: 0)
  • Operator: =(Default)/≠/</≤/>/≥

Output: Boolean (True/False)
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">number</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value><next><block type=\"controls_if\"><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">EQ</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">number</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">The number is 1.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}

The output:

0=0 - Output

When the First Number is Smaller than the Second Number

0 < 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"logic_compare\" disabled=\"true\" x=\"-22\" y=\"337\"><field name=\"OP\">LT</field><value name=\"A\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value><value name=\"B\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This block is used to return true if the first value is smaller than the second value.

Parameters: Condition:

  • Number (Default: 0)
  • Operator: =(Default)/≠/</≤/>/≥

Output: Boolean (True/False)
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">number</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><next><block type=\"controls_if\"><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">LT</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">number</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">The number is smaller than 1.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}

The output:

Smaller Than 1 - Output

When Two Text are the Same

Text = Text

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"logic_compare\" disabled=\"true\" x=\"23\" y=\"383\"><field name=\"OP\">EQ</field><value name=\"A\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\"/></shadow></value><value name=\"B\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\"/></shadow></value></block></xml>"}

This block is used to return true if two text are the same.

Parameters: Condition:

  • Number (Default: 0)
  • Operator: =(Default)/≠/</≤/>/≥

Output: Boolean (True/False)
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"text\"><field name=\"TEXT\">hello</field></block></value><next><block type=\"controls_if\"><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">EQ</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">text</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"text\"><field name=\"TEXT\">hello</field></block></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">Two texts are the same.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}

The output:

Same Text - Output

Boolean

Continuing the Action if Two Comparisons are True

… and …

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text</variable></variables><block type=\"logic_operation\" disabled=\"true\" x=\"-22\" y=\"517\"><field name=\"OP\">AND</field></block></xml>"}

This block is used to return true if two comparison is true.

Parameters:

  • Logic (Comparison)
  • Condition: and (Default)/or

Output: Boolean (True/False)
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">number</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"controls_if\"><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_operation\"><field name=\"OP\">AND</field><value name=\"A\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">LT</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">number</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">4</field></shadow></value></block></value><value name=\"B\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">GT</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">number</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">2</field></shadow></value></block></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">The number is smaller than 4 and larger than 2.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}

The output:

And - Output

Continuing the Action if At Least One Comparison is True

… or …

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"logic_operation\" disabled=\"true\" x=\"-22\" y=\"562\"><field name=\"OP\">OR</field></block></xml>"}

This block is used to return true if at least one of the comparisons is true.

Parameters:

  • Logic (Comparison)
  • Condition: and (Default)/or

Output: Boolean (True/False)
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">number</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"controls_if\"><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_operation\"><field name=\"OP\">OR</field><value name=\"A\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">LT</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">number</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">4</field></shadow></value></block></value><value name=\"B\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">NEQ</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">number</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value></block></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">The number is smaller than 4 and larger than 2.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}

The output:

Or - Output

Return True If Comparison is Incorrect

Not

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"logic_operation\" disabled=\"true\" x=\"-22\" y=\"562\"><field name=\"OP\">OR</field></block></xml>"}

This is used to return true if the comparison is wrong.

Parameters:

  • Logic (Comparison)
  • Condition: and (Default)/or

Output: Boolean (True/False)
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">number</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"controls_if\"><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_negate\"><value name=\"BOOL\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"logic_compare\"><field name=\"OP\">EQ</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">number</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">4</field></shadow></value></block></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">The number is not same as 4.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}

The output:

Not - Output

Always True

True

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable></variables><block type=\"logic_boolean\" disabled=\"true\" x=\"-22\" y=\"652\"><field name=\"BOOL\">TRUE</field></block></xml>"}

This is used to return true.

Parameters: Condition: true (Default)/false

Output: Boolean (True/False)
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"controls_if\"><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">This text will appear.</field></shadow></value></block></statement></block></statement></block></xml>"}

The output:

False - Output

Always False

False

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"logic_boolean\" disabled=\"true\" x=\"-22\" y=\"697\"><field name=\"BOOL\">FALSE</field></block></xml>"}

This is used to return false.

Parameters: Condition: true/false (Default)

Output: Boolean (True/False)
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"controls_if\"><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">FALSE</field></shadow></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">This text will not appear.</field></shadow></value></block></statement></block></statement></block></xml>"}

The output:

False - Output

Table of Content