Show more complex examples.

QuestionsQuestions8 SkillsProPython Control StructuresOct, 01 2025
094

Sure! Here are more complex examples demonstrating the use of continue and break in nested loops and with more intricate logic.

Example 1: Using continue in a Nested Loop

This example finds prime numbers within a specified range and skips non-prime numbers using continue.

# Function to find prime numbers in a given range
def find_primes(start, end):
    for num in range(start, end + 1):
        if num < 2:  # Skip numbers less than 2
            continue
        for i in range(2, int(num**0.5) + 1):
            if num % i == 0:  # If num is divisible by i, it's not prime
                continue  # Skip to the next number
        print(num)  # Print the prime number

find_primes(1, 20)

Output

2
3
5
7
11
13
17
19

Explanation

  • The outer loop iterates through numbers from start to end.
  • The inner loop checks if the number is divisible by any number from 2 to its square root.
  • If a divisor is found, continue skips to the next iteration of the outer loop, effectively skipping non-prime numbers.

Example 2: Using break in a Loop

This example searches for a specific number in a list and exits the loop once the number is found using break.

# Function to search for a number in a list
def search_number(numbers, target):
    for index, number in enumerate(numbers):
        if number == target:
            print(f"Number {target} found at index {index}.")
            break  # Exit the loop when the target is found
    else:
        print(f"Number {target} not found.")

numbers_list = [10, 20, 30, 40, 50]
search_number(numbers_list, 30)  # Searching for 30
search_number(numbers_list, 60)  # Searching for 60

Output

Number 30 found at index 2.
Number 60 not found.

Explanation

  • The loop iterates through the numbers list using enumerate to get both the index and the number.
  • When the target number is found, it prints the index and uses break to exit the loop.
  • The else block of the loop executes only if the loop completes without hitting a break, indicating that the target was not found.

Example 3: Combining continue and break

This example demonstrates both continue and break in a single loop to filter and find specific values.

# Function to filter and find specific values
def filter_and_find(numbers):
    for number in numbers:
        if number < 0:
            continue  # Skip negative numbers
        if number == 100:
            print("Found 100! Exiting the loop.")
            break  # Exit if 100 is found
        print(f"Processing number: {number}")

numbers_list = [-10, 20, 30, -5, 100, 50]
filter_and_find(numbers_list)

Output

Processing number: 20
Processing number: 30
Found 100! Exiting the loop.

Explanation

  • The loop processes each number in the list.
  • Negative numbers are skipped using continue.
  • If the number 100 is found, it prints a message and exits the loop using break.

These examples illustrate how continue and break can be used in more complex scenarios to control loop behavior effectively. If you have any further questions or need additional examples, feel free to ask!

0 Comments

no data
Be the first to share your comment!