> 

 > 

4. 以积木式编程制作网络应用程式
回圈
回圈

本文件解释了各种回圈结构,用于重复操作、条件执行、遍历范围和数组以及终止回圈。

多次重复操作

重复 4 次

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

这用于多次执行某些操作。

参数: 条件:数字(默认值:4)
输出: N/A
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">Hello</field></shadow></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

重复 4 次 - 输出

在满足布尔条件时执行操作

虽然是假的

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

此块用于在满足条件时运行相同的操作。

参数: 显示设置:布尔值 (True/False [Default])
输出: N/A
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"device_while\"><value name=\"COND\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">FALSE</field></shadow></value><statement name=\"DO\"><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">Showing text after 5 seconds.</field></shadow></value></block></value><next><block type=\"Task_sleep\"><value name=\"ms\"><shadow type=\"math_number\"><field name=\"NUM\">5000</field></shadow></value></block></next></block></statement></block></next></block></next></block></statement></block></xml>"}

虽然为 false,但输出:

虽然 false do - 输出

虽然为 true,但输出:

虽然为 true do - 输出

在从零到最终取值时执行操作

对于从 0 到 4 的索引

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

此块用于在将值从 0 取到最终数字时执行操作,该数字将乘以 1。

参数:

  • 指数
  • 条件:数字(默认值:4)

输出: N/A
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">It will repeat 5 times.</field></shadow></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

对于从 0 到 4 的索引 - 输出

在数组长度中取值时执行操作

对于列表的元素值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt_controls_for_of\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">value</field></shadow></value><value name=\"LIST\"><shadow type=\"variables_get\"><field name=\"VAR\">list</field></shadow></value></block></xml>"}

此块用于在数组中逐个取值直到列表末尾时执行一些操作。

参数:

  • 阵列

输出: N/A
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><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\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">It will repeat 3 times.</field></shadow></value></block></value></block></statement></block></next></block></next></block></next></block></statement></block></xml>"}

输出:

对于列表的元素值 - 输出

中断循环

中断

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"break_keyword\"/></xml>"}

该模块用于停止电流环路。

参数: N/A
输出: N/A
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"device_while\"><value name=\"COND\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow></value><statement name=\"DO\"><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">It will show once only.</field></shadow></value></block></value><next><block type=\"break_keyword\"/></next></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

中断 - 输出

执行下一步操作

继续

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"continue_keyword\"/></xml>"}

这用于跳过当前操作并继续循环中的下一个操作。

参数: N/A
输出: N/A
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"text_join\"><mutation items=\"2\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">The number is </field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\"/></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></value></block></next></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

继续 - 输出

目录