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!
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. 😄
print("Hello!")
print("Hello!")
print("Hello!")
print("Hello!")
print("Hello!")5 lines — boring!for i in range(5):
print("Hello!")2 lines — genius! 🎉
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!
Your Favourite Song
The chorus plays once. Then repeats again. And again! That’s a loop in music. 🎶
Video Game Enemies
Every few seconds, a new enemy appears. That’s a loop running in the background of every game!
Day and Night
Morning → afternoon → night → morning again. Earth runs on a loop — and it never crashes! 😄
See a Loop in Action!
Press Play and watch the loop count from 1 to 5!
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)
3 Different Kinds of Loops
Each one repeats differently — let’s see how!
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. 😊
print(“Jump!”)
# Prints “Jump!” exactly 10 times!
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! 🍕
while hungry:
eat_pizza()
# Keeps eating until full!
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! 🎉
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. 🛑
while True:
print(“Help! I can’t stop!”)
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!
