In this tutorial we'll go over how to make a "for loop" for our Micro:Bit robot the GiggleBot. We will make the robot move forward and make a rainbow smile four times (or as many times as you like!).
What's a For Loop?
A for loop is a loop that will repeat a section of code a specific number of times. You can think of it as a way to tell a computer or robot to do something over and over and over again . . . so many times.
So where do you find the "repeat" block? You find it under loops!
For Loop Robot Example for the Micro:Bit Robot
In this example, we'll show you how to write a For Loop.
Say we want to drive forward for 1 second and stop and make the GiggleBot smile. But we want to do that 4 times.
Here's the hard way to make that happen:
It takes 8 blocks to make the robot move forward and stop ten times.
How Do You Make a For Loop in MakeCode?
We make make the robot move forward 1 second and stop, but do this a lot more easily. If we use "For Loops" or "repeat" blocks. Again, a for loop on the Micro:Bit shows us how to loop and repeat a section of code or commands a number of times. In this case, we want the robot to "move forward for 1 second and stop", four times.
How Do You Make a For Loop in Python?
Bonus, we can make a for loop in Python for the Micro:Bit as well!