Table of contents

Break and continue : A Comprehensive Guide

The break and continue statements are powerful tools for controlling the flow of loops in Python. They allow you to alter the normal sequence of execution and provide greater flexibility when working with loops. This comprehensive guide will explore the break and continue statements in detail, with examples to illustrate their usage.

The break statement is used to exit a loop prematurely. When the break statement is encountered, the loop terminates immediately, and control is transferred to the statement following the loop.

for variable in sequence: if condition: break # code to execute in the loop

In this example, the loop prints numbers from 0 to 4 and exits when i equals 5.

for i in range(10): if i == 5: break print(i)

This loop prints numbers from 0 to 4 and exits when count equals 5.

count = 0 while count < 10: if count == 5: break print(count) count += 1

The break statement is useful when you need to search for an element in a list and exit the loop as soon as you find it.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] target = 5 for number in numbers: if number == target: print(f"Found the target: {target}") break else: print("Target not found.")

In this example, the loop exits as soon as the target number is found.

The continue statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration. This allows you to bypass certain conditions without terminating the loop.

for variable in sequence: if condition: continue # code to execute in the loop

This loop prints only odd numbers from 0 to 9 by skipping even numbers.

for i in range(10): if i % 2 == 0: continue print(i)

This loop prints only odd numbers from 0 to 9 by skipping even numbers.

count = 0 while count < 10: count += 1 if count % 2 == 0: continue print(count)

The continue statement is useful for skipping unwanted or invalid input in a loop.

user_inputs = ["apple", "banana", "", "cherry", None, "date"] for item in user_inputs: if not item: continue print(f"Processing: {item}")

In this example, the loop skips empty strings and None values.

Write a program that prints only the even numbers from a given list using the continue statement.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for number in numbers: if number % 2 != 0: continue print(number)

Write a program that prints numbers from 1 to 10 but stops if it encounters the number 7 using the break statement.

for i in range(1, 11): if i == 7: break print(i)

Write a program to find and print the first prime number greater than 50 using the break statement.

def is_prime(n): if n <= 1: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True number = 51 while True: if is_prime(number): print(f"The first prime number greater than 50 is {number}.") break number += 1

Write a program that prints each letter in a string except for vowels using the continue statement.

input_string = "Hello, World!" for char in input_string: if char.lower() in 'aeiou': continue print(char, end="")

In this guide, we’ve explored the break and continue statements in Python, including their syntax, usage, and practical examples. These control statements provide powerful ways to manage the flow of loops and handle specific conditions within your code. Practice these concepts with the provided examples and exercises to enhance your understanding and improve your programming skills. In the next section, we will delve into functions, which help you write modular and reusable code. Happy coding!