For loop for Kids

 How for loops works



I am preparing to teach kids how to code. One of the significant concepts of coding is loops, especially for loops. In this post, I will teach children the for loop and how it works.

Hey kid, do you like jumping on the trampoline with your friends. 
If you and your friends are using the same trampoline and agree that each of you uses it for 15 jumps, so you will start doing the task of jumping while your friend - the counter - starts counting from 1 and shouts your current jumping count loudly, and after you jump, he adds one to the previously estimated number, and tell you how many times you have jumped. Finally, when you reach the last jump in your turn, 15 in my example, you will stop doing the task of jumping, and your friend "the counter" will stop counting at the same time. 

If this seems reasonable and easy to understand, you will get how the for loops work. 

So, If I want to translate the playing of the trampoline to a for loop, it will be like this :

for ( counter =1; counter<=15;counter++)
{
  //do the task 
  console.log('I am jumping on the trampoline');
}

In this example, the computer starts counting at 1 (counter =1). The loop will keep running if the counter is less than or equal to 15 (counter<=15). Each time the loop runs, the counter will increase by 1 (counter++). Finally, the loop's body is the console.log('I am jumping on the trampoline'), which tells the computer to print "I am jumping on the trampoline" each time the loop runs.


That's it! Now you know how a for loop works. It's like playing on the trampoline with your friend, but now the computer is your friend, and it knows exactly how many times to repeat the action. Try writing your own for loop to count from 1 to 20  to print out your favourite phrase 10 times. Have fun!

Comments

  1. What a clear and detailed explanation! The kids (and I) are very grateful 😁 Thank you for this creative approach!

    ReplyDelete

Post a Comment

Popular posts from this blog

Valuing Parenting for Future Parents