While Loops - Keep Going Until...

6-8 minutes Beginner Level

Understanding the Concept

While loops are like saying 'keep doing this until something changes!' It's perfect for when you don't know exactly how many times you need to repeat something. Think of it like washing dishes - you keep washing while there are dirty dishes left. The loop keeps running as long as your condition stays true!

Code Example

# Count from 1 to 5
count = 1
while count <= 5:
    print("Count is:", count)
    count = count + 1  # Don't forget to update!

print("Done counting!")

# A fun guessing game concept
secret = 7
guess = 0
while guess != secret:
    print("Keep guessing!")
    break  # We'll break here for demo
💡 Explanation: Always make sure your condition will eventually become false, or your loop will run forever! Update your variable inside the loop.
Quick Actions

Your Turn to Practice

Write a while loop that counts down from 10 to 1, then prints 'Blast off!' Try it yourself!

Code Sandbox
Ready to run your code! 🚀
Pro tip: Write your solution in the sandbox above and click "Run Code" to test it! Ask our Python tutor if you need help!

Knowledge Check

Key Topics
while loop repeat continue condition iterate cycle until
Learning Path
Getting Started
Variables
Data Types
Python Syntax
Comments
Numbers
Operators
Type Casting
Strings
Booleans
User Input
If...Else
While Loops
For Loops
Lists
Dictionaries
Functions