Lesson 3 of 13

Loops: Repeat the Fun! 🔁

What if you could tell the computer to do something 100 times — with just ONE line of code? That’s the magic of loops!

1
2
3
4
5
🔁
🤔 What is a Loop?

Stop Writing the Same Thing Over and Over!

A loop does the repeating for you — automatically!

When I first started coding, I wanted to print “Hello!” five times. So I wrote it five times. My teacher laughed and said — “There’s a much better way!” That better way is called a loop. 😄

😰 Without a Loop
print("Hello!")
print("Hello!")
print("Hello!")
print("Hello!")
print("Hello!")
5 lines — boring!
😎 With a Loop!
for i in range(5):
  print("Hello!")

2 lines — genius! 🎉

🌍 Loops in Real Life

You Use Loops Every Single Day!

You just never called them loops before — until now!

🦷

Brushing Your Teeth

You move the brush back and forth — over and over until your teeth are clean. Then you stop!

while teeth_dirty: brush()
🎵

Your Favourite Song

The chorus plays once. Then repeats again. And again! That’s a loop in music. 🎶

repeat chorus: 3 times
🎮

Video Game Enemies

Every few seconds, a new enemy appears. That’s a loop running in the background of every game!

every 3 seconds: spawn_enemy()
🌅

Day and Night

Morning → afternoon → night → morning again. Earth runs on a loop — and it never crashes! 😄

while earth_spinning: day_night()

🎬 Watch the Loop!

See a Loop in Action!

Press Play and watch the loop count from 1 to 5!

🔁 Loop Counter

This code runs 5 times — watch each step light up!

# This loop counts 1 to 5
for number in range(1, 6):
    print("Step number:", number)
1👟
2👟
3👟
4👟
5🏁
Press Play to run the loop! 🚀

🎯 Types of Loops

3 Different Kinds of Loops

Each one repeats differently — let’s see how!

1️⃣

For Loop — “Do This X Times”

When you know exactly how many times to repeat — use a for loop. It counts for you automatically!

I use this when I want to print something 10 times, or go through a list of names one by one. 😊

for i in range(10):
  print(“Jump!”)

# Prints “Jump!” exactly 10 times!
2️⃣

While Loop — “Keep Going Until…”

When you don’t know how many times to repeat — use a while loop. It keeps going until something changes!

Think of eating pizza — you keep eating while you’re still hungry! 🍕

hungry = True
while hungry:
  eat_pizza()

# Keeps eating until full!
3️⃣

For Each Loop — “Do This for Every Item”

When you have a list of things and want to do something with each one — use for each!

Like saying “Hi!” to every friend at a party — one by one! 🎉

friends = [“Ali”, “Sara”, “Zain”]
for friend in friends:
  print(“Hi”, friend)

# Says Hi to each friend!

⚠️

Watch Out: Infinite Loops!

A loop that never stops is called an infinite loop. It’s like asking a robot to count to infinity — it would go on forever and crash your program! Always make sure your loop has a way to stop. 🛑

# 🚨 This loop NEVER stops!
while True:
  print(“Help! I can’t stop!”)

🧠 Quick Quiz!
🎯 Loops Quiz

4 questions · pick an answer to check!

⭐ Remember This!

🔁

Loops repeat code without writing it many times

🔢

For loops repeat a specific number of times

While loops keep going until something changes

🛑

Always make sure loops can stop! No infinite loops!