We have already mentioned that repeating one or more commands (blocks) is a powerful concept in programming. When some of the commands run multiple times in a program, we say that the program contains loops. We have used this concept several times before.
In Scratch, we have 3 types of blocks that repeat commands:
The block that repeats commands a specific number of times;
The block that repeats commands an infinite number of times;
The block that repeats commands until a specific condition is fulfilled.
We will insert the blocks we need to repeat into one of these blocks.
The block that repeats commands a specific number of times¶
We used the block that repeats commands a specific number of times in the project Farm (you can find more information in the lesson Looks).
The chick sprite in the program was simulating the pecking motion by changing costumes a, b and c 10 times. After this simulation, the chick increased in size by 10. Our goal was to increase the size of the chick sprite by 50, so we repeated the part of the code that increased the size of the sprite 5 times. To achieve the desired behavior, we used the blocks and .
In the figure below, we shrank the part of the script related to the repetition of commands, and we made the same program, but this time without using the repetition blocks. The script we got was really long, so for clarity, we had to divide it into five columns.
Create the program Farm without using the blocks for repetition. Compare your code with ours. We believe that you can see that stacking identical groups of blocks makes the program longer, harder to understand and upgrade.
The block that repeats commands an infinite number of times¶
This block for repeating commands runs an infinite number of times. The running of this block never stops on its own. We have to stop it by clicking the button, which stops the program (the red button next to the green flag) or by using one of the following blocks / / , from the category Control.
We used the block that repeats commands an infinite number of times in the project The cat is chasing the mouse (you can find more information in the lesson Motion).
The block that repeats commands until a specific condition is fulfilled¶
This block for repeating commands runs until a certain condition is fulfilled. The scripts within this block are executed based on a test, which determines whether the condition placed in the block is true or not. We use this block when we do not know how many times we need to repeat commands within the block for repetition, and therefore, we want them to run until a certain condition is fulfilled.
We will demonstrate how a block that repeats commands until a specific condition is fulfilled works - we will create a program that functions as a timer, i.e. it counts down seconds from the entered value to zero.
For this program, we will create the variable , which will store the value of the remaining seconds while the timer is counting down. We will ask the user to enter a certain number of seconds. Then, we will start the countdown. After the entered time has elapsed, we will hear a sound signal.
The figure below contains our suggestion of the program’s code with comments, which serve as an explanation. Programmers find it helpful to comment on scripts and explain what certain blocks do. Commenting makes it easier for other programmers to understand and upgrade the programs we create. You can add a comment by right-clicking on the script, and selecting the option Add comment.
The program Timer can be found on the link https://scratch.mit.edu/projects/326420353. Upgrade it by adding a clock ticking sound, which will be heard as the timer counts down.