什么是 Scratch 编程中的迭代?
Scratch 是一种高级可视化编程语言工具,它通过内置程序基础的图表和块与用户进行交互。 Scratch 用于制作交互式程序,特别适合使用块式界面的孩子,以便他们可以轻松学习语言并且免费使用。在本文中,我们将讨论从头开始的迭代块。
从头迭代块
迭代是允许重复命令或一组行的概念。它也被称为计算机编程语言中的循环。当代码执行(重复)一次时,这种重复称为一次迭代。迭代控制块基本上通过将类似任务分组到一个块中来减少要执行的类似任务的数量。在 Scratch 中,控制块包含迭代工具,如下图所示:
现在,让我们详细了解每个迭代块:
1.重复(编号n)块
它是一个控制块。这个迭代块迭代给定的命令集'n'次。数字 n 可以是任何正整数。这里,n 是迭代块的停止条件。如果 n 的值是无限的,则循环永远运行,如果 n 的值是非正数,则该循环将不会运行,如果 n 的值是十进制数,则该数字向上舍入。让我们借助一个简单的项目来了解重复块的工作原理:
程序:
Step 1: Click on event block and choose 1st control block denoting the start of the program(drag it to center).
Step 2: Click on add extension and choose pen block for drawing square.
Step 3: Place pen down block below the event block.
Step 4: Now drag the repeat block from the control block and place it down of pen down block. Change the number from 10 to 4.
Step 5: Click on motion block and drag move block and place it inside repeat block. Change the number from 10 to 50.
Step 6: Click on the turn block and place it below the move block inside the repeat block. Change the number from 15 to 90.
Step 7: Now run the program.
下面是程序的实现:
2.嵌套重复块
通过将重复块放置在另一个重复块中来创建嵌套重复块。这里,两个整数将用于停止条件。让我们通过做一个小项目来理解这一点:
程序:
Step 1: Click on the event block and choose the 1st control block denoting the start of the program(drag it to center).
Step 2: Click on add extension and choose pen block for drawing a square.
Step 3: Place pen down block below the event block.
Step 4: Now drag repeat block from control block and place it down to the pen down block. Now change the number from 10 to 2.
Step 5: Now, again drag the repeat block from the control block and place it inside the first repeat block. Now, change the number from 10 to 2.
Step 6: Click on the motion block and drag the move block and place it inside the repeat block. Now, change the number from 10 to 50.
Step 7: Click on the turn block and place it below the move block inside the nested repeat block. Now, change the number from 15 to 90.
Step 8: Now run the program.
下面是程序的实现:
3.永远封锁
它是一个控制块。这个迭代块无限地执行一组特定的命令。永远块中不存在停止条件。这种块用于制作本质上无限的图案。例如,方形螺旋。让我们借助一个简单的项目(即方形螺旋)来了解永久块的工作原理:
程序:
Step 1: Click on event block and choose 1st control block denoting the start of the program(drag it to center).
Step 2: Click on add extension and choose pen block for drawing square spiral.
Step 3: Place pen down the block below the event block.
Step 4: Go to variable block and drag “set my variable to 80”.
Step 5: Drag the forever block from the control block and place it down of pen down block.
Step 6: Click on motion block and drag move block and place it inside the repeat block. Now change the number to “my variable”.
Step 7: Click on turn block(inside motion block) and place it below the move block inside repeat block. Now, change the number from 15 to 90.
Step 8: Go to variable block and drag “change my variable by 5”.
Step 9: Now run the program.
下面是程序的实现:
4. 重复直到阻塞
它是一个控制块。该迭代块执行一组特定的命令,直到不满足特定条件。插入到 until 中的条件是循环的停止条件。当预先知道停止条件时使用该块。让我们借助一个简单的项目来了解永久块的工作原理。在这个项目中,我们画了一个六边形,六边形的停止条件为 6。
程序:
Step 1: Click on the event block and choose the 1st control block denoting the start of the program(drag it to center).
Step 2: Click on add extension and choose pen block for drawing a square spiral.
Step 3: Place pen down block below the event block.
Step 4: Go to variable block and drag “set my variable to 0”.
Step 5: Drag repeat until block from control block and place it down of pen down the block.
Step 6: Go to operator block and drag O = O block and fill in the left blank with “my variable” and right blank with 6. Fix this in until blank.
Step 7: Click on motion block and drag move block and place it inside repeat until block. Now change the number to 50
Step 8: Click on turn block(inside motion block) and place it below the move block inside repeat until block. Now change the number from 15 to 60.
Step 9: Go to variable block and drag “change my variable by 1”.
Step 10: Now run the program.
下面是程序的实现:
5.嵌套重复直到块
通过将重复直到块放置在另一个重复直到块中来创建嵌套的重复直到块。这里,将使用两个条件作为停止条件。让我们通过做一个小项目来理解这一点。
程序:
Step 1: Click on event block and choose 1st control block denoting start of the program(drag it to center).
Step 2: Click on add extension and choose pen block for drawing square.
Step 3: Place pen down block below the event block.
Step 4: Go to variable block and click on “make a variable”, name it a, drag “set a to 0”.
Step 5: Drag repeat until block from control block and insert it in another repeat until block.
Step 6: Go to operator block and drag O=O block and fill the left blank with “a” and right blank with 3. Fix this in until blank.
Step 7: Go to the variable block and drag “set my variable to 0” and place it inside repeat until block.
Step 8: Go to operator block and drag O=O block and fill left blank with “my variable” and right blank with 3. Fix this in the second repeat until blank.
Step 9: Click on motion block and drag move block and place it inside repeat block. Now, change the number from 10 to 50.
Step 10: Go to variable block and drag “change my variable by 1”.
Step 11: Click on the turn block and place it below the move block inside the repeat block. Now, change the number from 15 to 60.
Step 12: Outside the inner repeat block, go to the variable block and drag “change a by 1”.
Step 13: Now run the program.
下面是程序的实现: