"""
Topic: Control Flow (Loops): break and continue
Purpose: Learn how to control loop execution using break and continue
Output: Shows how loops stop early or skip iterations
"""
# -----------------------------
# 1. Using break
# -----------------------------
# 'break' immediately exits the loop
for i in range(1, 10):
if i == 5:
break
print("Value:", i)
# -----------------------------
# 2. Using continue
# -----------------------------
# 'continue' skips the current iteration
for i in range(1, 6):
if i == 3:
continue
print("Value:", i)
# -----------------------------
# 3. break in a while loop
# -----------------------------
count = 1
while True:
if count > 4:
break
print("Count:", count)
count += 1
# -----------------------------
# 4. continue in a while loop
# -----------------------------
number = 0
while number < 6:
number += 1
if number == 4:
continue
print("Number:", number)
# -----------------------------
# 5. Practical example
# -----------------------------
# Stop searching once the item is found
numbers = [2, 4, 6, 8, 10]
for n in numbers:
if n == 6:
print("Number found!")
break
print("Checking:", n)
# -----------------------------
# 6. Notes
# -----------------------------
# 1. 'break' exits the loop completely.
# 2. 'continue' skips to the next iteration.
# 3. break is often used to stop early.
# 4. continue is useful for ignoring specific cases.
# 5. Use these carefully to keep loops readable.