← Back to Python Course
πŸ”„ Loops & RepetitionΒ·20 minΒ·Beginner
♾️

While Loops

Keep going until you stop

Learn while loops β€” they keep repeating as long as a condition is true. Perfect for when you do not know how many times to repeat!

πŸ“–

What we are learning

A while loop keeps repeating as long as a condition is True. It is like saying "keep doing this WHILE this is still true." The moment the condition becomes False, the loop stops.

πŸ’‘ Think of it like this:

Think of eating a bowl of cereal. You keep eating WHILE there is still cereal in the bowl. When the bowl is empty, you stop. A while loop works the same way!

🌟

Why it matters

While loops are perfect for situations where you do not know how many times to repeat. Keep asking for a password while it is wrong. Keep playing while the player wants to continue.

Where you see this in real life:

  • βœ“Games keep running while the player has not quit
  • βœ“Alarms keep ringing while the time has not reached the set time
  • βœ“Pumps keep running while the water level is too high
🎯

By the end, you can:

  • 1.Write a while loop with a condition
  • 2.Update the condition inside the loop to avoid infinite loops
  • 3.Understand the difference between for and while loops
πŸ”€

New words

whileconditioninfinite loopcounter
🐍
Name something you keep doing until a condition changes. For example: keep brushing your teeth until they are clean.
πŸ€“

Fun Fact!

An infinite loop is a loop that never stops! It is a common bug β€” but sometimes it is intentional, like in game engines that keep running until you close the game.

♾️ While Loops

A whileloop keeps repeating as long as a condition is True. The moment the condition becomes False, the loop stops.

✨ Your First While Loop

We need a counter variable that changes inside the loop. If the condition never becomes False, we get an infinite loop that never stops!

🐍Try a while loop!
main.py
● Output
count starts at 1 and goes up by 1 each time. When count hits 6, the loop stops.

⬇️ Counting Down

While loops can count down too! Great for rocket launches πŸš€

🐍Countdown timer!
main.py
● Output
Click β–Ά Run to see the output!

⚠️ Beware Infinite Loops!

If you forget to update the counter, the loop runs forever! This is called an infinite loop. Always make sure the condition can become False.

βœ… Safe β€” counter changes

count = 0 while count < 3: print(count) count += 1

❌ Infinite β€” no change!

count = 0 while count < 3: print(count) # forgot to change count!

πŸ”„ for vs while

for loop

Use when you know how many times to repeat

Example: Print "Hi" 10 times

while loop

Use when you do not know how many times

Example: Keep asking until password is correct

🐍
Try this: use a while loop to count from 10 down to 1. Print each number. Then print 'Boom!' at the end!
🧠

Quick Quiz!

+10 XP

What happens if you forget to update the counter variable in a while loop?

🐍
Finished the lesson? Click the button to mark it complete and earn XP!