Skip to Main Content

Coding for Educators

Loops

One of the best things about computers is that they never complain about being bored. Loops are special pieces of code that make a computer do the same thing over and over again. This is one of the reasons computers are so useful. They love iteration!

Most high-level programming languages have different types of loops. Not all languages offer all loop types. The two most popular are Repeat, For, and While.

Repeat loop  

 

While Loop

FOR Loop

Code our first loop

1) We are going to code the Micro.bit to display the numbers 0 to 9 on the LED screen.

Code for our first loop

2) Edit the loop to display the numbers 0 to 20.

Additional Challenge

Create a program that displays only the even numbers between 2 and 20.

We will decompose this problem

1) How would you do this offline?

2) Do you see a pattern?

3) What type of loop should we use?

4) Switch to the other type of loop and redo your program.