Loops - Avoid Repeated Work

📊 Workshop Slides

What are Loops?

Loops are a way to repeat code multiple times without having to write the same instructions over and over again. Think of loops like a recipe that says “repeat steps 3-5 three times” - instead of writing those steps three separate times, you just write them once and tell the computer to repeat them.

basic.show_string("Hello!")
basic.show_string("Hello!")
basic.show_string("Hello!")
basic.show_string("Hello!")
basic.show_string("Hello!")

This is repetitive and boring to write!

What if…?

Writing the same code over and over would take FOREVER! 😴

Loops to the rescue! A loop repeats code multiple times automatically.

Types of Loops

There are two main types:

For Loops: The Basics

for i in range(5):
    basic.show_string("Hello!")

This shows “Hello!” 5 times!

Let’s break it down:

Pop Quiz!

How many times will this show “Coding is fun!”?

for i in range(3):
    basic.show_string("Coding is fun!")
Answer

3 times!

range(3) creates: 0, 1, 2 (that’s 3 numbers!)

“I” Am a Counter Variable

The variable i counts each loop:

for i in range(3):
    basic.show_string("Loop number:")
    basic.show_number(i)

This will show:

Counting from 1 to 10

for i in range(1, 11):
    basic.show_number(i)

range(1, 11) means start at 1, stop before 11 So it counts: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Remember: the second number is NOT included!

Your Turn!

What should go in the question marks to count from 5 to 8?

for i in range(??, ??):
    basic.show_number(i)
Answer
for i in range(5, 9):
    basic.show_number(i)
Start at 5, stop before 9 → gives us 5, 6, 7, 8 ✨

Loops with Variables

We can use variables in our loops too!

name = "Emma"
for i in range(3):
    basic.show_string("Hi " + name + "!")

This shows “Hi Emma!” three times. The + combines (concatenates) strings together!

Forever Loops

Sometimes we want code to repeat forever:

while True:
    basic.show_string("I never stop!")

Be careful! This really will run forever until you reset the micro:bit!

Building Our Beating Heart 💓

Let’s make a heart that beats forever using what we’ve learned:

We’ll need:

Step 1: Set Up

  1. Navigate to makecode.microbit.org
  2. Click on “New Project”
  3. Change to Python
  4. Delete the current code

Ready to code! 👩‍💻

Step 2: Create the Forever Loop

We want our heart to beat forever. To accomplish this we will need a while loop like we discussed earlier.

while True:
    # Our heart beating code will go here

The # creates a comment - notes for humans that the computer ignores!

Step 3: Show Heart Icons

while True:
    basic.show_icon(IconNames.HEART)
    basic.pause(300)
    basic.show_icon(IconNames.SMALL_HEART)
    basic.pause(300)

Complete Beating Heart Code

while True:
    basic.show_icon(IconNames.HEART)
    basic.pause(300)
    basic.show_icon(IconNames.SMALL_HEART)
    basic.pause(300)

Try it out! Your micro:bit should show a beating heart that never stops! 💓

Pop Quiz!

What would happen if we removed the basic.pause(300) lines?

Answer

B. The heart would beat very fast

Without pauses, the micro:bit switches between hearts so quickly you might not see the animation!

Customize Your Heart

Try changing these values:

Extra Challenges

Try these challenges:

What We Learned Today

✅ Loops repeat code automatically
✅ for loops repeat a specific number of times
✅ while loops can repeat forever
✅ Indentation is important in Python
✅ range() helps us count
✅ We can combine loops with variables and strings

References and Acknowledgements