Learn Python, Microsoft 365 and Google Workspace
These statements modify the behavior of loops.
break: Terminates the loop entirely. continue: Skips the current iteration and moves to the next one. pass: Does nothing, often used as a placeholder.
break
Exits the loop prematurely.
for item in sequence:
if some_condition:
break # exit the loop
continue
Skips the current iteration and proceeds to the next iteration of the loop.
for item in sequence:
if some_condition:
continue # skip the rest of the code in this iteration
# code to execute if some_condition is False
pass
A null statement, used as a placeholder.
if condition:
pass # do nothing
for x in range(3):
if x == 1:
break
# Search for the number 5 and exit the loop when found
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
print(f"Checking {num}...")
if num == 5:
print("Number 5 found! Exiting the loop.")
break # Exit the loop
Output:
Checking 1...
Checking 2...
Checking 3...
Checking 4...
Checking 5...
Number 5 found! Exiting the loop.
while
loop# Keep asking for a password until the correct one is entered
correct_password = "python123"
while True:
user_input = input("Enter the password: ")
if user_input == correct_password:
print("Access granted!")
break # Exit the loop
else:
print("Wrong password. Try again!")
continue
StatementSkips the current iteration and moves to the next loop cycle.
# Print only odd numbers (skip even numbers)
for num in range(1, 11):
if num % 2 == 0:
continue # Skip even numbers
print(num)
Output:
1
3
5
7
9
Stop monitoring if temperature exceeds a safe limit.
temperatures = [25, 30, 32, 28, 45, 29, 33] # Sensor readings
for temp in temperatures:
if temp > 40:
print(f"ALERT: Temperature {temp}°C is unsafe! Shutting down.")
break # Exit immediately
print(f"Temperature {temp}°C is safe.")
Output:
Temperature 25°C is safe.
Temperature 30°C is safe.
Temperature 32°C is safe.
Temperature 28°C is safe.
ALERT: Temperature 45°C is unsafe! Shutting down.
continue
StatementSkip invalid entries when processing a dataset.
user_ages = [20, 15, "unknown", 30, -5, 25] # Some invalid data
print("Valid ages:")
for age in user_ages:
if not isinstance(age, int) or age < 0 or age > 120:
continue # Skip invalid entries
print(f"- {age} years old")
Output:
Valid ages:
- 20 years old
- 15 years old
- 30 years old
- 25 years old
break
StatementSimulate a coffee machine that stops serving when a drink is out of stock.
["latte", "cappuccino", "espresso", "mocha", "out_of_stock", "latte"]
Serving latte...
Serving cappuccino...
Serving espresso...
Serving mocha...
Out of stock! Machine stopping.
Task: Skip Negative Numbers
Calculate the sum of positive numbers in a list, ignoring negatives.
[5, -2, 10, -8, 3]
continue
to skip negative values.Sum of positive numbers: 18
Task: Simple Calculator with Exit using continue
and break
Create a loop that:
continue
).break
).Enter a number (or 'quit'): 5
Enter a number (or 'quit'): ten
Invalid input!
Enter a number (or 'quit'): 3
Enter a number (or 'quit'): quit
Total: 8
[25, -5, 30, 105, 40]
Valid age: 25
Skipped invalid age.
Valid age: 30
VIP detected! Stopping sales.
["reserved", "reserved", "available", "reserved"]
Seat 1: Reserved.
Seat 2: Reserved.
Seat 3: Available! Booked successfully.