12 How to Use While Loop with Break and Continue in Python + Tips (Video in Khmer)
Learn Python while loop with break and continue statements. Simple example, exercise and tips. Good for beginner!
2026-10-22 ยท 4 min read
Hello friend! ๐ while loop run code again and again. It run while condition is True. When condition is False, loop stop.
break stop loop now. continue skip this round, go to next round.
In this post, I show you while loop with break and continue in Python. Easy and short. ๐
๐ While Loop
Check condition first. If True, run code. Then check again.
i = 1
while i <= 3:
print(i)
i += 1
Output:
1
2
3
Careful: You must change the value inside loop (i += 1). If not, loop never stop.
๐ฅ Video: Using While Loop in Python (Khmer)
๐ฅ Video: Example of Sum Values Using While Loop in Python (Khmer)
โ Example: Sum with While Loop
total = 0
n = 1
while n <= 10:
total += n
n += 1
print("Total:", total) # Total: 55
โ Break
break stop the loop right away. Code after loop still run.
count = 0
while True: # run forever
count += 1
if count == 5:
break # stop loop here
print(count)
print("Done")
Output:
1
2
3
4
Done
๐ฅ Video: Using Break with For and While Loop in Python (Khmer)
โญ๏ธ Continue
continue skip the rest of this round. Loop go to next round.
n = 0
while n < 5:
n += 1
if n == 3:
continue # skip 3
print(n)
Output:
1
2
4
5
๐ฅ Video: Using Continue with For and While Loop in Python (Khmer)
โ๏ธ Exercise: Break and Continue Together
Print 1 to 10. Skip 5. Stop when number is 8.
n = 0
while n < 10:
n += 1
if n == 5:
continue
if n == 8:
break
print(n)
Output:
1
2
3
4
6
7
โ Common Error
Forget to change value. Loop run forever:
i = 1
while i <= 3:
print(i) # i never change, infinite loop!
Fix: add i += 1 inside loop.
Put continue before the change. Loop run forever again:
n = 0
while n < 5:
if n == 3:
continue # n never change, stuck at 3!
n += 1
Fix: change n before continue.
๐ก Tips
- Press
Ctrl + Cto stop a loop that run forever. while True:make loop run forever. Usebreakto stop it.breakstop the whole loop.continueonly skip one round.- Check your condition. Make sure it can become
Falseone day. - Use
breakfor stop early when you find what you need. - Use
continuefor skip value you don't want. - Use
forloop when you know how many time to repeat. Usewhileloop when you don't know.
โ Any Question?
Tell us in comment below! ๐
#Python #WhileLoop #Break #Continue #Loop #LearnPython #Beginner