3.3.1 Premature Loop Exit with break

Unit: Unit 3: Loops • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Search for a target customer ID in an unsorted batch
target_id = 42
ids = [10, 25, 42, 67, 88, 99]

for current_id in ids:
    print("Inspecting ID:", current_id)
    if current_id == target_id:
        print("Target found! Halting search immediately.")
        break  # Exit the loop early without checking remaining elements

print("Search concluded.")
Open in Live Interactive Python Sandbox →