Loops are commonly used in codes to repeat an action again and again. In Scratch, we have different types of loops.
In this blog, I am going to write about how to create a breakable loop in Scratch Programming. Before learning how to create a breakable loop, let us know about the loops.
Table of contents
Types of Loops in Scratch
In Scratch, there are three different types of loops. They are “repeat”, “repeat until”, and “forever” loops. These loop blocks are found in the “Contol drawer”.
Usage of Loops in Scratch
In this section, we are going to see how loops are being used in Scratch.
- Forever loop: Forever loop is used to act continuously. The loop will not stop anytime. For instance look at the code given in the image below.
The output is, the cat sprite will move 10 steps and turn back if it touches the edges. This process continues forever, endlessly. To view the output, click this link
- Repeat loop: A repeat loop is used to perform an action n number of times. The loop will stop after the nth time. For example, if you want to do an action 10 times you can make use of this block. Let us see an example to demonstrate this.
If you execute the above code, the dragon will glide to a random position repeatedly for 5 times. After that, it will stop gliding.
- Repeat until loop: Repeat until loop is used to repeat the process until the given condition is met. Look at the following code
If you run the above code, the cat will glide to a random position until it touches the edges. Once it touches any edge it will go to the center or origin which is (0,0)
How to Create a Breakable Loop in Scratch
Now let us see how you can create a breakable loop in Scratch. There are numerous methods for breaking a loop in between. I’ll go over the most common methods here.
- Using If condition
First, we are going to see how to break a loop using the if condition. Have a look at the below code.
Here the cat will go to a random position 5 times because of the repeat loop. But if you touch the cat with the mouse pointer it will disappear.
So the loop gets terminated because of the “if” condition. Check the output here.
In the same way, you can break a forever loop using the If condition. Try this code and check the output
2. Using stop the script
Another way of breaking the loop is by using the “stop the script” block
In the above code, the loop is getting terminated using the “stop the script” block. Run this code and see how to break the forever loop using stop the script.
3. Using broadcast
We can also use a broadcast block to break a loop. You can see this block in the Events drawer.
In the following code, we use a broadcast block to create a breakable loop.
When the green flag is clicked, the cat glides to a random position and the forever loop gets terminated when the stop message is broadcast. When the stop message is received the cat goes to the center and says ”Hello”.Click the link to view the output.
4. Using define block
Now I am going to explain how to create a breakable loop using the block “Define”.You can create your block and define it. You can make this block in the drawer “My blocks”
Now let us see an example of how we can use the define block to break a loop.
In this code, I have created a block called break and defined it. So When a break is encountered the forever loop will be stopped and the cat goes to the center. Check it out here!
Conclusion
Loops are commonly used in programming to repeat an action over and over. There are three types of loops in Scratch: repeat, repeat until, and forever loops.
The Contol drawer contains these loop blocks. Use this concept in your next project now that we’ve learned how to make a breakable loop in Scratch.
Learn Scratch at BrightChamps with its specially designed curriculum that makes learning programming easy for students in Grade 1-12.
BrightChamps also teaches a variety of other programs that help kids build a foundation in computer programming through activities, interactive lessons, and other means.
Frequently Asked Questions (FAQs)
If you want to repeat an action more than once, loops will help you do it.
The instructions inside the forever loop will be executed always without stopping. But the instructions inside the repeat () loop will be executed for the given number of times inside the loop.
In some programs, we need to terminate the loop earlier without running through all the iterations. Using the methods discussed here, we can exit from the loop. Breakable loops help to make the code simpler