What is a loop?
Have you jumped up and down until someone says stop? Maybe you heard a song and listened on repeat for days. Or maybe you just walked to the kitchen and moved your feet back and forth till you walked to a table. When you are doing something over and over again you are creating a loop!
We can think of a loop repeating an action or set of instructions until a condition is met.
Loops in code are pretty cool because maybe we want something to repeat until a condition is met (say play a song on repeat until the user says stop) or maybe the loop will not start until a specific condition is met (read the data, then do a loop analyzing it). Or maybe we want a FOREVER loop, something that will repeat forever unless told to stop.
Loops allow us to change the game. We see loops most often in applications, games, and computers because humans just simply can’t repeat the same steps over and over infinitely without having to rest or eat!
Check it out: Check out this video from code.org to understand a little more about loops and the different types you can use when you code.
Programmed to Dance, with loops!
We can see that loops are a great way to help us create efficient programs. Instead of typing the same line of code each time, we can use a loop to create repetition or signal in the game we want something to continue UNTIL a condition is met.
We can see loops being used in Scratch. There are three loops we will look a little more into forever, repeat, and repeat until.
Forever: These are loops that will go on forever. For example, in a game, you may want a song to play the entirety of the game. This means we want the song to loop forever.
Repeat (number of times): These are loops that will repeat for a set number of times. As the programmer, you can decide how many times that action occurs. For example, you may let a player collect 5 apples from a tree. This means that once the player has collected 5 they will no longer have the ability to collect more in a game.
Repeat until: These are loops that will repeat until some other action is triggered. Typically an event occurs that triggers the change. For example, you are playing a game and a song plays for the level you are in. When you pass the level by completing the challenges, the song stops. The song will only stop once you complete the level.
You can use all these loops in interesting ways in Scratch, be it with a game, music, or story. Remember, coding is about exploring and trying new things out so take your time to play!
Try it out: Go to Scratch and try playing with the loop blocks. Can you make a sprite move in a pattern for 5 seconds? Maybe you can figure out how to program your sprite to dance!
Challenge Yourself: Program your own dance routine! Create two sprites that can dance in a pattern to music. You can use any sprite you would like, just make sure to program music and movement. As a hint, you will need to use loops to help your sprite dance.
Share Your Work: Let us know how you are doing. Share your Scratch project with iamtheCODE by tagging us on social media using the hashtag # VirtualCodeClubs