Working with Blocks from the Loops Category¶
There are processes in nature that are repeated constantly or from time to time. As in nature, in programming, sometimes it is necessary to repeat certain parts of the program several times. We have already mentioned that repeating one or more commands (blocks) is a powerful concept in programming. When some of the program commands are repeated several times, we say that the program contains loops. We have used this concept several times before.
Repeating commands is a very common occurrence in programming.
MakeCode has three types of blocks into which other blocks, whose execution needs to be repeated, can be inserted. Blocks can be repeated:
a certain number of times:
We should use this block when we know the exact number of repetitions in advance (they are also called iterations).
an infinite number of times (continuously, until the user stops the program):
This is one of the most frequently used blocks. The running of this block is stopped when the user clicks the stop button ().
until a certain condition is fulfilled:
We should use this block when we do not know how many times blocks inserted into the repetition block should be executed, and therefore, we want them to run until a certain condition is fulfilled. Blocks, which need to be repeated, are dragged and inserted into repetition blocks.
The block, which will repeat other blocks an exact number of times, can be used to reproduce the tone C three times.
The look of the code:
The block for repeating commands will be executed an infinite number of times. The running will never stop on its own. We can stop the running by clicking on the stop button ().
We used the blocks for repeating in our previous lessons when we displayed the animation of a square.
- New blocks can be inserted into the block itself, so there is no need to continue the script.
- It is a bug in MakeCode. The block that repeats commands an infinite number of times must have the option to continue stacking the script.
- The answer is not correct!
- Adding further blocks would be pointless since they would never be executed. :feedback_a: The answer is correct!
- The answer is not correct!
Q-9: Analyze the look of the blocks. You can see that the forever block used for repeating commands indefinitely does not have the option for connecting to other blocks, other blocks cannot be added onto it. Why?
The block which repeats other blocks (commands) runs until a specific condition is fulfilled. The blocks within this block are executed based on checking whether the condition is fulfilled or not. We use this block when we do not know how many times we need to repeat the commands inside it, and we want them to run until a specific condition is fulfilled.
- The block that repeats commands a certain number of times.
- The answer is not correct!
- The block that repeats commands an infinite number of times.
- The answer is not correct!
- The block that repeats commands until a certain condition is fulfilled.
- The answer is correct!
Q-10: You want to create a program, which continuously turns on the LED on the screen with coordinates (2,2) until the light level drops below a certain value. Which blocks will you use for repeating commands?
We will demonstrate the use of this block by creating a program that plays a tune as long as the light level is lower than 120.
For the purposes of this program, we will define the variable , which will store the value of the light level reading. As long as the light level value is less than 120, a sound signal will be emitted. In the figure below you can find our suggestion for the code, along with comments which explain it. Programmers consider commenting on scripts and explaining what each block does to be useful. Commenting makes it easier for other programmers to understand and upgrade programs we created. You can add a comment by right-clicking on the script and selecting the option Add comment.
To test the program, we will run it in the simulator by clicking the button.