While loop
Working with while loop, continue, break keywords.
Working with while loop, continue, break keywords.
In the previous lesson we used for to repeat code a known number of times. But what if you don't know in advance how many iterations you need? That's where while comes in.
The loop checks the condition before each iteration. As soon as it becomes False, the loop stops.
This does the same thing as for i in range(1, 6), but notice that we manage the counter ourselves — we initialize i before the loop and increment it inside. The for loop handles all of that automatically, which is why you should prefer for when the number of iterations is known.
A very common pattern — keep adding numbers until some condition is met:
while is natural when you need to process a number digit by digit. The loop continues as long as there are digits left:
For n = 12345, the loop runs 5 times, each time removing the rightmost digit via //= 10.
breakIf the condition never becomes False, the loop runs forever — this is called an infinite loop. Sometimes this is actually intentional, and you exit the loop with break:
while True is a common Python pattern for "keep running until we explicitly stop". break immediately exits the loop regardless of the condition.
Another example — searching for the first number divisible by both 7 and 13:
continuecontinue skips the rest of the current iteration and jumps back to the condition check:
while vs forUse for when you know the number of iterations upfront — iterating over a range or a sequence.
Use while when the exit condition depends on something that changes during execution — user input, a value converging, a search terminating:
Always make sure the condition will eventually become False or that you have a break. A forgotten update to the loop variable is the most common mistake:
If you accidentally run an infinite loop, press Ctrl+C in the terminal to stop it. With the right structure, while is a clean and powerful tool — use it whenever the number of iterations isn't known in advance.
while condition:
# code executed as long as condition is True
i = 1
while i <= 5:
print(i, end=' ')
i += 1
# Output: 1 2 3 4 5
i = 10
while i >= 1:
print(i, end=' ')
i -= 1
# Output: 10 9 8 7 6 5 4 3 2 1
total = 0
i = 1
while i <= 100:
total += i
i += 1
print(total) # 5050 — sum of 1 to 100
n = int(input())
digit_count = 0
while n > 0:
digit_count += 1
n //= 10 # remove the last digit
print("Number of digits:", digit_count)
while True:
n = int(input())
if n < 0:
break
print("You entered:", n)
print("Done")
n = 1
while True:
if n % 7 == 0 and n % 13 == 0:
print(n) # 91
break
n += 1
i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue
print(i, end=' ')
# Output: 1 3 5 7 9
# Read numbers and accumulate sum until user enters 0
total = 0
while True:
n = int(input("Enter a number (0 to stop): "))
if n == 0:
break
total += n
print("Current sum:", total)
print("Final sum:", total)
# BAD — infinite loop, i never changes
i = 1
while i <= 5:
print(i)
# GOOD
i = 1
while i <= 5:
print(i)
i += 1