> 

 > 

5. 以積木式編程製作Raspberry Pi智能裝置
迴圈
迴圈

本文件涵蓋了各種迴圈結構,包括重複操作、條件迴圈、從零迭代到最終值、遍歷數組長度、終止迴圈以及進行下一步操作。

多次重複操作

重複次數確實如此

{"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>"}

這用於多次執行某些操作。

參數: 條件:數字(預設值:4)

輸出: N/A
示例代碼:

{"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>"}

輸出:

重複次數 - 輸出

在滿足布爾條件時執行操作

雖然是假的

{"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>"}

這用於多次執行某些操作。

參數: 顯示設定:布爾值 (True/False [Default])

輸出: N/A
示例代碼:

{"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>"}

輸出:

While True - 輸出

在從零到最終取值時執行操作

對於從 0 到

{"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>"}

此塊用於在將值從 0 取到最終數位時執行操作,該數位將乘以 1。

參數:

  • 指數
  • 條件:數字(預設值:4)

輸出: N/A
示例代碼:

{"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>"}

輸出:

對於從 0 到 - 輸出的索引

在陣列長度中取值時執行操作

對於清單的元素值

{"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>"}

此塊用於在陣列中逐個取值直到清單末尾時執行一些操作。

參數:

  • 陣列

輸出: N/A
示例代碼:

{"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>"}

輸出:

對於清單的元素值 - 輸出

中斷循環

中斷

{"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>"}

該模組用於停止中斷循環。

參數: N/A

輸出: N/A
示例代碼:

{"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>"}

輸出:

中斷 - 輸出

執行下一步操作

繼續

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

這用於跳過當前操作並繼續迴圈中的下一個操作。

參數: N/A

輸出: N/A
示例代碼:

{"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>"}

輸出:

繼續 - 輸出

目録