> 
 > 
5. Blocks Coding with Raspberry Pi
Loops
Loops

This documentation covers various loop constructs including repeating actions, conditional loops, iterating from zero to a final value, looping over array lengths, terminating loops, and proceeding to the next action.

Repeating Actions in Several Times

Repeat times do

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"controls_repeat_ext\" disabled=\"true\" x=\"23\" y=\"67\"><value name=\"TIMES\"><shadow type=\"math_whole_number\" disabled=\"true\"><field name=\"NUM\">4</field></shadow></value></block></xml>"}

This is used to do some actions in several times.

Parameters: Condition: Number (Default: 4)

Output: N/A
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_repeat_ext\"><value name=\"TIMES\"><shadow type=\"math_whole_number\"><field name=\"NUM\">4</field></shadow></value><statement name=\"DO\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value><next><block type=\"basic_sleep\"><value name=\"ms\"><shadow type=\"math_number\"><field name=\"NUM\">100</field></shadow></value></block></next></block></statement></block></statement></block></xml>"}

The output:

Repeat Times - Output

Doing Actions when Meeting the Boolean Conditions

While false do

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"device_while\" disabled=\"true\" x=\"23\" y=\"202\"><value name=\"COND\"><shadow type=\"logic_boolean\" disabled=\"true\"><field name=\"BOOL\">FALSE</field></shadow></value></block></xml>"}

This is used to do some actions in several times.

Parameters: Display setting: Boolean (True/False [Default])

Output: N/A
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=\"device_while\"><value name=\"COND\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow></value><statement name=\"DO\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value><next><block type=\"basic_sleep\"><value name=\"ms\"><shadow type=\"math_number\"><field name=\"NUM\">1000</field></shadow></value></block></next></block></statement></block></statement></block></xml>"}

The output:

While True - Output

Doing Actions when Taking Values from Zero to Final

For index from 0 to

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>index</variable></variables><block type=\"pxt_controls_for\" disabled=\"true\" x=\"23\" y=\"337\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\" disabled=\"true\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\" disabled=\"true\"><field name=\"NUM\">4</field></shadow></value></block></xml>"}

This block is used to do action when taking the values from 0 to the final number which will count by 1.

Parameters: 

  • Index
  • Condition: Number (Default: 4)

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=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">4</field></shadow></value><statement name=\"DO\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><shadow type=\"math_number\"><field name=\"NUM\">5</field></shadow></value><next><block type=\"basic_sleep\"><value name=\"ms\"><shadow type=\"math_number\"><field name=\"NUM\">1000</field></shadow></value></block></next></block></statement></block></statement></block></xml>"}

The output:

For Index from 0 to - Output

Doing Actions when Taking Value in the Length of Array

For element value of list

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>index</variable><variable>value</variable><variable>list</variable></variables><block type=\"pxt_controls_for_of\" disabled=\"true\" x=\"23\" y=\"472\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\" disabled=\"true\"><field name=\"VAR\">value</field></shadow></value><value name=\"LIST\"><shadow type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></shadow></value></block></xml>"}

This block is used to do some actions when taking value in the array one by one until the end of list.

Parameters: 

  • Value
  • Array

Output: N/A
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>value</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"ADD1\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value><value name=\"ADD2\"><shadow type=\"math_number\"><field name=\"NUM\">2</field></shadow></value></block></value><next><block type=\"pxt_controls_for_of\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">value</field></shadow></value><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">It will repeat 3 times.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}

The output:

For Element Value of List - Output

Terminate the Current Loop

Break

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>value</variable></variables><block type=\"break_keyword\" disabled=\"true\" x=\"23\" y=\"607\"/></xml>"}

This block is used to stop the current loop.

Parameters: N/A

Output: N/A
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=\"device_while\"><value name=\"COND\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">It will show once only.</field></shadow></value><next><block type=\"break_keyword\"/></next></block></statement></block></statement></block></xml>"}

The output:

Break - Output

Doing Next Action

Continue

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"continue_keyword\" disabled=\"true\" x=\"23\" y=\"697\"/></xml>"}

This is used to skip the current action and continue with the next action in the loop.

Parameters: N/A

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=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">4</field></shadow></value><statement name=\"DO\"><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=\"continue_keyword\"/></statement><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></next></block></statement></block></statement></block></xml>"}

The output:

Continue - Output

Table of Content