Vitaly is the Digital Marketing Manager at CodeMonkey. His master's degree in Computer Science, combined with his experience as a father of two sons, helps him create content focused on coding education.
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.
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.
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.
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.
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.
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.
Stay Up To Date on The Latest NEWS AND LEARN MORE ABOUT CODING FOR KIDS.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. However you may visit Cookie Settings to provide a controlled consent.
This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Cookie
Type
Duration
Description
_codemonkey_session_rails_4
1
1 year
_ga
persistent
2 years
This cookie is installed by Google Analytics. The cookie is used to calculate visitor, session, campaign data and keep track of site usage for the site's analytics report. The cookies store information anonymously and assign a randomly generated number to identify unique visitors.
cookielawinfo-checkbox-advertisement
persistent
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Advertisement".
cookielawinfo-checkbox-analytics
persistent
11 months
This cookies is set by GDPR Cookie Consent WordPress Plugin. The cookie is used to remember the user consent for the cookies under the category "Analytics".
cookielawinfo-checkbox-functional
persistent
11 months
cookielawinfo-checkbox-necessary
persistent
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-non-necessary
0
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Non Necessary".
cookielawinfo-checkbox-other
persistent
11 months
cookielawinfo-checkbox-performance
persistent
11 months
This cookie is used to keep track of which cookies the user have approved for this site.
JSESSIONID
1
1 year
Used by sites written in JSP. General purpose platform session cookies that are used to maintain users' state across page requests.
viewed_cookie_policy
persistent
1 hour
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
XSRF-TOKEN
persistent
1 year
The cookie is set by Wix website building platform on Wix website. The cookie is used for security purposes.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
Cookie
Type
Duration
Description
rlas3
persistent
11 months
The cookie is set by rlcdn.com. The cookie is used to serve relevant ads to the visitor as well as limit the time the visitor sees an and also measure the effectiveness of the campaign.
UserMatchHistory
persistent
1 month
Linkedin - Used to track visitors on multiple websites, in order to present relevant advertisement based on the visitor's preferences.
This cookie is set by Google Analytics and is used to distinguish users and sessions. The cookie is created when the JavaScript library executes and there are no existing __utma cookies. The cookie is updated every time data is sent to Google Analytics.
__utmb
persistent
30 minutes
The cookie is set by Google Analytics. The cookie is used to determine new sessions/visits. The cookie is created when the JavaScript library executes and there are no existing __utma cookies. The cookie is updated every time data is sent to Google Analytics.
__utmc
persistent
1 year
The cookie is set by Google Analytics and is deleted when the user closes the browser. The cookie is not used by ga.js. The cookie is used to enable interoperability with urchin.js which is an older version of Google analytics and used in conjunction with the __utmb cookie to determine new sessions/visits.
__utmt
persistent
10 minutes
The cookie is set by Google Analytics and is used to throttle request rate.
__utmz
persistent
6 months
This cookie is set by Google analytics and is used to store the traffic source or campaign through which the visitor reached your site.
_gat
persistent
1 minute
This cookies is installed by Google Universal Analytics to throttle the request rate to limit the colllection of data on high traffic sites.
YSC
1
1 year
This cookies is set by Youtube and is used to track the views of embedded videos.
This cookie is used by Google Analytics to understand user interaction with the website.
_gid
persistent
1 day
This cookie is installed by Google Analytics. The cookie is used to store information of how visitors use a website and helps in creating an analytics report of how the wbsite is doing. The data collected including the number visitors, the source where they have come from, and the pages viisted in an anonymous form.
GPS
persistent
30 minutes
This cookie is set by Youtube and registers a unique ID for tracking users based on their geographical location
MR
persistent
5 months
This cookie is used to measure the use of the website for analytics purposes.
MUIDB
1
1 year
This cookie is used by MicroSoft and collects data about users of visits on multiple website. This data is used to analyze the efficiency of advertisements on websites.
This cookie is set by Facebook to deliver advertisement when they are on Facebook or a digital platform powered by Facebook advertising after visiting this website.
bscookie
1
2 years
This cookie is a browser ID cookie set by Linked share Buttons and ad tags.
fr
1
3 months
The cookie is set by Facebook to show relevant advertisments to the users and measure and improve the advertisements. The cookie also tracks the behavior of the user across the web on sites that have Facebook pixel or Facebook social plugin.
IDE
1
2 years
Used by Google DoubleClick and stores information about how the user uses the website and any other advertisement before visiting the website. This is used to present users with ads that are relevant to them according to the user profile.
MUID
persistent
1 year
Used by Microsoft as a unique identifier. The cookie is set by embedded Microsoft scripts. The purpose of this cookie is to synchronize the ID across many different Microsoft domains to enable user tracking.
pxrc
persistent
2 months
The purpose of the cookie is to identify a visitor to serve relevant advertisement.
rlas3
persistent
11 months
The cookie is set by rlcdn.com. The cookie is used to serve relevant ads to the visitor as well as limit the time the visitor sees an and also measure the effectiveness of the campaign.
skimCSP
persistent
11 months
This cookie is set by SkimResources. The cookie is used to record data on the visitor to optimize the relevance of the advretisements served to the users.
TapAd_DID
persistent
2 months
The cookie is set by tapad.com. The purpose of the cookie is to track users across devices to enable targeted advertising
TapAd_TS
persistent
2 months
The cookie is set by Tapad.com. The purpose of the cookie is to track users across devices to enable targeted advertising.
TDCPM
persistent
1 year
The cookie is set by CloudFare service to store a unique ID to identify a returning users device which then is used for targeted advertising.
TDID
persistent
1 year
The cookie is set by CloudFare service to store a unique ID to identify a returning users device which then is used for targeted advertising.
test_cookie
persistent
15 minutes
This cookie is set by doubleclick.net. The purpose of the cookie is to determine if the users' browser supports cookies.
VISITOR_INFO1_LIVE
1
5 months
This cookie is set by Youtube. Used to track the information of the embedded YouTube videos on a website.