Understanding For Loops in Coding – Explained for Kids

For loops are like magic wands in the world of coding! Imagine you’re a wizard and you want to cast a spell multiple times without saying it over and over again. That’s what for loops do in coding. They let you repeat actions a certain number of times or for each item in a list, making tasks like drawing patterns, solving math problems, or even creating games much easier and quicker. So, instead of writing the same code again and again, you can use a for loop to do it all in one go, saving time and making your code super powerful!

Watch this CodeMonkey video to learn all about For Loops in Computer Coding:

Read the transcript below:

Hello, welcome to the For Forest! Let’s learn about For Loops.

Use a For Loop to repeat code for each item in an array. So for example, if you had an array called Bananas and there were 10 bananas in the array, if you say for b in bananas, this for loop is going to repeat for each banana in the array, so it’s going to loop 10 times, and each time this variable b will hold a given banana.

for loops 1

So the first time through, b is going to be the banana with index zero; the second time, it will be the banana with index one, and so on, all the way through all the bananas. However, if bananas just had three bananas in it, it would loop three times.

Let’s look at an example.

for loops 2

Here’s a challenge where we have six banana objects, and each of these bananas is in an array called bananas. Here we have bananas with index zero, so each of these bananas has a different index, and since there’re six of them, the index is 0 through 5.

Now if we look at the code, we know we want the monkey to pick up all those bananas. So if we look at the code, we’re using a for loop for b in bananas. So this is the name of the array, and because there are six bananas in this array, this for loop will repeat six times.

Each time, b, which is a variable, will store a different banana. So the first time through, b is bananas with index zero; the next time through, b is bananas with index one, and so on, all the way to bananas with index five. So it repeats six times, and the first time through when we say “turn to b,” we’re really saying “turn to bananas with index zero and step the distance to bananas with index zero.” And then the next time through, we will be turning to bananas with index one and stepping the distance to bananas with index one.

Now one point I want to make is b is only valid within this for loop. If I use b outside of this for loop, it would not work; the program would not recognize b. It’s only valid within the loop.

Let’s look at it. Let’s go ahead and run this first and then we’ll look at another example.

for loops 3

Here’s a challenge with turtles. We could see in the challenge that we want the monkey to step onto the turtle and then have the turtle move to the banana, and then the monkey to step onto this turtle and have the turtle move to the banana, and then have the monkey step onto the last turtle and then have the turtle go to the banana.

So we have three turtles and three bananas. You have to decide sometimes which array you want to loop on, and we have the starter code that tells us we are looping on the turtles.

There’re three turtles so how many times will this for loop repeat? Three times, and each time t is going to be a different turtle. So instead of saying “turtles with index 0,” instead I’m going just to say “step the distance to t” because t will change each time through the loop.

And then we’re going to have the turtle that the loop is working on step 20. Let’s just make sure that is 20. Ah, it’s actually 20. So let’s put 20 in here instead. Okay, let’s see how this works.

for loops 4

Okay, t is turtles with index zero; now t is turtles with index one, and now t is turtles with index two. You see how flexible that is.

Now that we have reviewed for loops, it’s your turn. Good luck.

Want to watch other Coding Basics Videos?

Visit the CodeMonkey YouTube channel.

ABOUT THE AUTHOR

Pam Rissmann is a middle school STEM teacher who teaches computer science, robotics, and product design. She has been teaching for 15 years, and in addition to teaching, she coaches her school’s competitive math club and STEM Girls club. Pam is a recipient of the Santa Clara County/Texas Instruments STEM teacher award and California League of Middle School Teacher of the Year for her school. Pam was awarded by Project Lead the Way the Outstanding Gateway Teacher award in 2019.

Try CodeMonkey at home or at school!

free trial

or

for your school / district

More to explore:

Table of Contents

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to CodeMonkey's blog

Stay Up To Date on The Latest NEWS
AND LEARN MORE ABOUT CODING FOR KIDS.