Fall 2024 - P3
3.8.8 Breaking loops by checking for conditions
Learn to break loops using conditional statements, and the continue function.
Basic Overview:
This lesson introduces the use of break
and continue
in loops to control the flow of iteration. The break
statement is used to exit a loop prematurely when a specific condition is met, while the continue
statement skips over certain iterations without terminating the loop. The lesson also covers handling errors in loops, such as division by zero or operations on incompatible data types, using try
and except
blocks.
#numbers ← [1, 2, 3, 4, 5]
#FOR EACH num IN numbers:
# IF num EQUALS 3:
# CONTINUE
# IF num EQUALS 5:
# BREAK
# DISPLAY num
#END FOR
# Example 8: For loop with continue and break
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
continue # Skip the number 3
if num == 5:
break # Exit the loop when 5 is encountered
print(num)
1
2
4
#Possible homework answer:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
continue # Skip the number 3
if num == 5:
break # Exit the loop when 5 is encountered
print(num)
Homework Hack
You are given a list of numbers, and your task is to write a Python script that iterates through the list and applies the following rules:
Skip over the number 3 and do not print it. Stop the loop completely once the number 5 is encountered. Print the remaining numbers that follow these rules. Requirements:
Use a for loop to iterate through the list. Use an if condition with continue to skip the number 3. Use an if condition with break to exit the loop when the number 5 is encountered. Print the numbers that are not skipped or cause the loop to break. Input: The given list is: [1, 2, 3, 4, 5]
Expected Output: You should print all numbers except 3 and stop printing after 5.
What’s Happening:
In the first part of the code, the for
loop iterates over the list [1, 2, 3, 4, 5]
. The continue
statement skips the number 3, so it doesn’t get printed, and the break
statement stops the loop when the number 5 is encountered, preventing any further iteration. As a result, only the numbers 1 and 2 are printed before the loop breaks.
In the second part of the code, the for
loop processes each element in the numbers
list, attempting to divide 10 by each element. If the element is a number, the division occurs. If the element is 0, it triggers a ZeroDivisionError
, and the program prints “Division by zero.” If the element is a string, such as “three” or “five,” it raises a TypeError
, and the program prints “Type error.”
Additional Notes:
- Breaking Loops: The
break
statement allows for early exit from a loop when a specific condition is met, such as encountering a certain value in the loop. - Skipping Iterations: The
continue
statement skips the current iteration and moves to the next one, useful when you want to bypass certain values or conditions in a loop. - Error Handling: Using
try
andexcept
blocks in loops ensures that even if an error occurs (e.g., division by zero or a type mismatch), the program won’t crash, and the error can be handled gracefully. - Common Use Cases:
break
andcontinue
are often used in cases where you want more control over the loop’s flow, such as skipping unnecessary steps or stopping the loop under specific conditions. Error handling in loops is useful when processing user input or handling data from diverse sources.