Microbits - Loops
Computers repeat many executable statements, over, over , and over again. Rather than writing if statements over and over again we can put them into a loop.
While loop
A while loop is a statement that allows code to be executed repeatedly based on a given condition.
while (true) ⟵ while the statement is considered ‘true’
{do this} ⟵ then execute the commands in this bracket, and return to the ‘while’ statement
For Loop
A for loop is a statement that allows code to be executed a certain amount of times based on a given condition. This loop is a little strange, but it lets the program know in which iteration the loop is in.
Max number ⟵ the number of time the loop executes
Index (i) ⟵ the index will gain a value of 1 each time the loop runs
For (i < Max number) ⟵ while the statement is considered ‘true’, while index (i) is less than the number set to execute.
{do this} ⟵ then execute the commands in this bracket, increase index (i) by one, then return to the beginning of the ‘for’ statement
Check out the microbit page on loops here
Challenge 1
Program the microbit to count down from 5 to 0 using a while loop
Challenge 2
Program the microbit to count up from 0 to 5 using a for loop
Challenge 3
Program the microbit to count from 5 down to 0 if button ‘a’ is pressed, if button ‘b’ is pressed then the microbit will count up from 0 to 5
Creating a Reaction Time Program
Please watch the following video to program and assemble a reaction time robot.
You will need:
Tinfoil sheet
3 x Alligator Clips
Microbit
Microbit battery
Use the following video for coding the Microbit: