dev.rean.me
python

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

Share:

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 + C to stop a loop that run forever.
  • while True: make loop run forever. Use break to stop it.
  • break stop the whole loop. continue only skip one round.
  • Check your condition. Make sure it can become False one day.
  • Use break for stop early when you find what you need.
  • Use continue for skip value you don't want.
  • Use for loop when you know how many time to repeat. Use while loop when you don't know.

โ“ Any Question?

Tell us in comment below! ๐Ÿ‘‡

#Python #WhileLoop #Break #Continue #Loop #LearnPython #Beginner


๐Ÿ“š Related Post

โ† Back to python
Share: