Welcome to Loop Land! Today, we’re going to learn about something super cool in coding called the “times loop.” This loop helps you repeat actions in your code without having to write them over and over again. Imagine if you had a monkey that needed to do the same thing several times, like picking bananas. The times loop can help with that!
Using the times loop is easy! First, decide how many times you want your code to repeat. Let’s say you want it to repeat three times. You would write it like this: for x times ->
. The arrow ->
shows where the loop starts. Underneath, you write the commands you want to repeat, but make sure to indent them. You can do this by pressing the tab key or using four spaces on your keyboard.
Let’s see how this works with a fun example. Imagine we have a monkey that needs to collect three bananas. Here’s what we would do without a loop: first, the monkey turns left and takes 15 steps to get the first banana. Then, it turns left again and takes 15 steps to get the second banana. Finally, it turns left one more time and takes 15 steps to get the last banana.
Now, instead of writing all those steps three times, we can use a times loop! We just need to write the turn left and step 15 commands once and put them inside the loop. By default, the loop is set to repeat three times, which is perfect for our banana-collecting mission!
When using the times loop, it’s important to indent all the commands you want to repeat. If you only indent the turn left command and not the step 15 command, the monkey will turn left three times and then take 15 steps just once. Sometimes, you might want this, but for our banana mission, we want both actions to repeat together. So, make sure both commands are indented!
Now, when we run the code with both commands indented, the monkey will turn left and step 15 three times, collecting all the bananas. Hooray! We did it and earned three stars!
Now it’s your turn to try using the times loop. Have fun and good luck!
Banana Collection Game: Create a simple game using toys or paper cutouts. Set up a small course where a toy monkey needs to collect three bananas. Use a piece of paper to write down the steps the monkey needs to take, such as “turn left” and “move forward 15 steps.” Instead of writing each step three times, use a loop by writing “repeat 3 times” at the top and indent the steps underneath. Act out the steps with your toy monkey to see how the loop works in real life!
Loop Dance Party: Have a dance party where you create a short dance routine with two or three moves. For example, “clap hands,” “spin around,” and “jump.” Write these moves on a board and use a loop to repeat them three times. Invite your friends or family to join and follow the looped dance routine. Notice how using a loop makes it easier to remember and perform the dance!
Loop Observation Walk: Go on a walk around your neighborhood or a park with a notebook. Look for patterns or actions that repeat in nature or everyday life, like a bird chirping or a swing moving back and forth. Write down what you observe and think about how a loop could describe these repeating actions. Share your observations with a friend or family member and discuss how loops are everywhere around us!
Sure! Here’s a sanitized version of the YouTube transcript:
—
[Music] Welcome to Loop Land! Let’s learn about the times loop. The times loop repeats code for you. Isn’t that cool? Use it when the monkey will be repeating the same actions over and over again.
This is how you write the times loop: first, you have to decide how many times you want to loop your code. Whatever that number is, you put it in `for x times ->`. You see how the `->` looks like an arrow? Then, underneath, you put all the commands you’d like to loop on. Make sure you indent those commands by using the tab key on your keyboard or by using four spaces.
Let’s look at the times loop in action. Here’s a challenge that probably looks familiar to you. We want the monkey to collect these three bananas, and it looks like there’s a sort of pattern here. If we were to code it sequentially, we would: first, have the monkey turn left and then step 15. That gets us to the first banana. Next, we would turn left again and step 15 to get to the second one, and then we’ll turn left and step 15 to get to that last banana.
Okay, let’s make sure this works. [Music]
It works, but we want to use a loop instead. So, we’ve repeated the turn left and step 15 three times. What we could do is just have one occurrence of that pattern and put that one occurrence into a times loop. By default, it’s giving us three, which is exactly what we want. But if we wanted to loop, let’s say, five times, we would just change that to five. However, we want it to loop three times.
Now, you see how the turn left is indented, but the step 15 isn’t? What would happen if we ran the code like that? Ah, the monkey turned left three times and then ran the step 15. Now, sometimes you want code like that; you want to have a loop and then do something afterward. That’s how you would do it. But in this case, we want to do both of those commands in the loop, so I want both commands indented.
Now, if I run it, it will turn left and step 15 three times. Very good! And we get our three stars. Yay!
Okay, very nice! So that’s the times loop. Now it’s your turn to use the times loop. Good luck! [Music]
—
Let me know if you need any further modifications!