Learning Guides
While Loops in Python: Complete Guide with Examples
Quick answer: Learn Python while loops including syntax, break and continue, infinite loops, the while-else clause, and common interview questions.
Basic While Loop Syntax
count = 1
while count <= 5:
print(count)
count += 1
The condition is checked before each iteration. If it is false from the start, the loop body never runs at all.
Infinite Loops and How They Happen
count = 1
while count <= 5:
print(count)
# forgot to increment count -- this never stops
The most common cause of an accidental infinite loop is forgetting to update the variable the condition depends on.
Break and Continue in a While Loop
count = 0
while True:
count += 1
if count == 5:
break # exits the loop
print(count)
count = 0
while count < 10:
count += 1
if count % 2 == 0:
continue # skips even numbers
print(count)
The while-else Clause
Like a for loop, a while loop can have an else clause that runs only if the loop finishes normally, without a break.
count = 0
while count < 5:
count += 1
else:
print("Loop completed normally") # this runs
A Practical Example: Input Validation
while True:
age = input("Enter your age: ")
if age.isdigit() and int(age) > 0:
age = int(age)
break
print("Please enter a valid positive number")
This pattern, an infinite loop with an internal break, is common when you need to keep asking until valid input is received.
While vs For: When to Use Each
Use a for loop when iterating over a known sequence. Use a while loop when the number of repetitions depends on a condition that can only be evaluated as the program runs, such as waiting for valid user input or processing until a value crosses a threshold.
Common Interview Questions
When is a while loop preferred over a for loop?
When the number of iterations is not known in advance and depends on a runtime condition, rather than a fixed, known sequence.
How would you deliberately create an infinite loop with a controlled exit?
Use while True combined with a break statement triggered by a specific internal condition, a common pattern for input validation or event loops.
FAQ
Frequently Asked Questions
When should you use a while loop instead of a for loop?
When the number of iterations is not known in advance and depends on a condition evaluated at runtime, such as waiting for valid user input.
What is the most common cause of an infinite while loop?
Forgetting to update the variable that the loop's condition depends on, so the condition never becomes false.
What does while True with a break do?
It creates a loop that runs indefinitely until a specific internal condition triggers the break statement, commonly used for input validation.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin