The for loop you just learned works great when you know exactly how many times something needs to repeat, five numbers, every character in a word, every item in a list. But sometimes you don’t know that in advance. You just want something to keep happening until a condition changes. That’s exactly what a while loop is for.
What a while loop actually does?
A while loop keeps running its block of code for as long as a condition stays True. The moment the condition becomes False, the loop stops.
count = 1
while count <= 5:
print(count)
count += 1Output:
1
2
3
4
5Walk through it slowly: Python checks count <= 5, sees it’s true, prints count, then adds 1 to it. It keeps repeating that cycle until count becomes 6, at which point the condition fails, and the loop stops on its own.
The mistake almost everyone makes early on
Forget to update the variable inside the loop, and you get an infinite loop. Your program just hangs forever because the condition never changes.
count = 1
while count <= 5:
print(count)
# forgot count += 1 here!Run this and Python prints 1 forever, or until you force-stop it. This isn’t a rare beginner mistake, it happens to experienced programmers too when they’re moving fast. Always double-check that whatever your condition depends on actually changes somewhere inside the loop.
while loops with user input
This is where while loops genuinely shine, situations where you don’t know ahead of time how many times something needs to repeat.
password = ""
while password != "python123":
password = input("Enter the password: ")
print("Access granted!")You have no idea how many times the user will type the wrong password. Could be once, could be ten times. A for loop can’t handle that naturally, but a while loop does it without any extra effort.
break and continue work here too
Same rules as the for loop.
count = 1
while True:
if count == 4:
break
print(count)
count += 1Output:
1
2
3Notice while True here, a condition that’s always true, which would normally create an infinite loop on purpose. The only thing stopping it is the break statement once count hits 4. This pattern shows up a lot in real programs, menus that keep asking “do you want to continue?” until the user says no.
count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)Output
1
2
4
5continue skips just that one print statement when count is 3, but the loop keeps going regardless.
for loop vs while loop: when to use which
If you already know how many times to repeat something, or you’re going through every item in a sequence, reach for a for loop. If you’re waiting for something to happen- a correct password, a specific input, a condition becoming true- use a while loop instead. There’s no strict rule stopping you from using either one in most situations, but picking the right tool makes your code easier to read.
Python While Loop Practice Questions
1. Write a while loop that prints numbers from 10 down to 1.
2. What’s wrong with this code? Fix it.
x = 1
while x < 5:
print(x)3. Write a program using a while loop that keeps asking the user to enter a number until they enter 0.
4. What is the output of the following code?
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)5. Write a while loop that uses break to stop as soon as it finds the first number divisible by 7 between 1 and 100.
6. Explain, in your own words, when you’d choose a while loop over a for loop.
Frequently Asked Questions
While loop
