Open In App

What is Iteration in Scratch Programming?

Improve
Improve
Like Article
Like
Save
Share
Report

Scratch is a high-level visual programming language tool that interacts with users through diagrams and blocks that have the basics of a program inbuilt in it. Scratch is used to make interactive programs especially for kids using the block kind of interfaces so that they can easily learn languages and it is free to use. In this article, we will discuss the iteration block present in scratch. 

Scratch iteration block

Iteration is the concept that allows the repetition of commands or a set of lines. It is also known as a loop in the computer programming language. When the code executes(repeated) one time then this repetition is known as one iteration. The Iteration control block basically reduces the number of similar tasks to be performed by grouping the similar tasks in one block. In Scratch, the control block contains iteration tools as shown in the below image:

Now, let’s understand each iteration block in detail:

1. repeat (number n) block

It is a control block. This iteration block iterates the given set of commands ‘n’ number of times. The number n can be any positive integer. Here, n is the stopping condition of the iteration block. If the value of n is infinite then the loop runs forever, if the value n is a non-positive number then this loop will not run, and if the value of n is a decimal number then the number is round up. Let us understand the working of the repeat block with the help of a simple project:

Procedure:

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.

Below is the implementation of the procedure:

2. nested repeat block

A nested repeat block is created by placing a repeat block inside another repeat block. Here, two integer numbers will be used for the stopping condition. Let’s understand this by making a small project:

Procedure:

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.

Below is the implementation of the procedure:

 3. forever block

It is a control block. This iteration block executes a particular set of commands infinitely. There is no stopping condition present in forever block. This kind of block is used to make patterns that are infinite in nature. For example, the square spiral. Let us understand the working of the forever block with the help of a simple project(i.e. square spiral):

Procedure:

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.

Below is the implementation of the procedure:

4. repeat until block

It is a control block. This iteration block executes a particular set of commands until a particular condition is not satisfied. The condition inserted in until is the stopping condition of the loop. This block is used when the stopping condition is known beforehand. Let us understand the working of the forever block with the help of a simple project. In this project, we draw a hexagon and the stopping condition of the hexagon would be 6.

Procedure:

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.

Below is the implementation of the procedure:

5. nested repeat until block

A nested repeat until block is created by placing a repeat until block is placed inside another repeat until block. Here, two conditions will be used for stopping conditions. Let’s understand this by making a small project.

Procedure:

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.

Below is the implementation of the procedure:



Last Updated : 07 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads