Skip to main content

MicroBit: While and For Loops

Objective

Roboteers will use loops to create a hot potato game using the MicroBit.

Essential keywords

While Loops

For Loops

Forever loops


Attention (5 Min.)

Loops

Up until now, we hav e been writing code that only runs once, or runs whenever we provide an input such as pressing a button or shaking the MicroBit. In these past examples, once the final line of code runs, it stops until it is asked to repeat again. Loops allow us to run a section of code for as many times as we need. As soon as the last block of code runs, it jumps back to the start of the stack and begins processing code in a downwards fashion. There are different types of loops that that have different ways of determining how long or how many times the code is repeated. Namely, we will be focusing on forever loops, and while loops.

Forever Loops

You might be familiar with the forever loop block because it is automatically populated when you first open the code editor. Forever loops do exactly what their name says, and run code forever. There is no way to stop code inside of a forever block from running, so be careful whenever you use a forever block.

Here is an example of a forever block that counts up forever.

For Loops

On the other hand, the other loops do have conditions under which the code stops looping. For example, a for loop is used when you know exactly how many times you want the code to run. For example, let's say you want to display the numbers from 0 to 5 and then stop. You can do this by using a for loop. For loops make use of variable often called the "index". This index counts how many times the code has been run and increases by one every time the loop repeats. You can use this index to change what the program does every time it loops. Take a look at this code that counts from 0 to 4 and then stops.

You can even nest loops within each other. For example, this code will count up to 4, then go back to 0 and start counting back up, forever.

While Loops

While loops are used when you want to run the code to run until a condition is true or until a condition is not true. You can think of it as a conditional statement deciding whether the code should run again or not. Once the last block of code is processed, it checks whether the condition is true, and if it is, it jumps back to the start, and if it isn't, it doesn't run the code. This code uses a while loop within a forever block to display a heart if button A is pressed, or an X If it is not pressed.

Hot Potato

Lets used our Microbit to make a hot potato game. Hot potato is a school game where you pass an object around until a person who has their eyes closed yells stop! The person who is holding the object when stop is called loses. We can use a Microbit to set a random timer and display an icon when it runs out. The person who is holding the Microbit when the icon changes is the loser.

3.  Explore (25 - 30 Min.)

Try to make the same game, but instead of using a while loop, use a for loop!

4. Share (Min.)

Peer review! Students can share their work with peers for constructive feedback and then use this feedback to revise and improve their work

5. Evaluate

Students reflect on their performance. Give positive and constructive feedback on each student

Essential Questions 

When would you use a for loop versus a while loop?

What do loops allow you to do?