Control Flow
Break and Continue
Loop control statements
Interview: Frequently asked in coding interviews — tests understanding of loop flow control
The break and continue statements give you fine-grained control over loop execution. break exits the loop entirely, while continue skips to the next iteration. Combined with the else clause on loops, they enable elegant search and validation patterns.
Break Statement
break immediately terminates the innermost enclosing loop. Execution continues with the first statement after the loop body.
- Search pattern: Loop through items, break when found — avoids unnecessary iteration
- Input validation: Break out of a prompt loop when valid input is received
- Sentinel detection: Break when a special "stop" value is encountered
- Important: In nested loops, break only exits the innermost loop — there is no built-in multi-level break
Continue Statement
continue skips the rest of the current iteration and jumps to the loop's next iteration check.
- Filtering: Skip items that don't meet a condition (though list comprehensions are often cleaner)
- Guard clause in loops: Skip invalid data early, keeping the main logic unindented
- Caution: In while loops, ensure the loop variable is updated before continue, or you'll create an infinite loop
The for/while...else Clause
Python's unique else on loops runs only if the loop completed without a break. Think of it as "no-break" rather than "else".
- Search idiom: Search with break in the loop; the else block handles "not found"
- Prime checking: Try divisors with break; else block confirms primality
- Validation: Check all conditions with break on failure; else block confirms all passed
Interview Tip
The loop-else pattern is a Python-specific feature that interviewers love to test. Practice the search-with-else idiom: search in a for loop with break on match, and handle "not found" in the else block.
Alternatives to Break/Continue
- any() / all(): Replace search loops with
any(item.is_valid for item in items) - List comprehensions: Replace continue-based filtering with
[x for x in data if x > 0] - Functions with return: Extract loop logic into a function and use return instead of break
- Exception-based exit: For deeply nested loops, raise a custom exception to break out of multiple levels
Best Practice
Prefer extracting loop logic into a function and using return over break. It makes the code testable and avoids the need for flag variables to communicate whether the loop broke early.
Use Cases
Early termination in search and validation loops
Skipping invalid or irrelevant items with continue
Implementing retry logic with maximum attempt limits
Search-with-else idiom for "found vs not found" patterns
Common Mistakes
Forgetting that break only exits the innermost loop in nested loops
Using continue in a while loop without updating the loop variable (infinite loop)
Confusing loop-else: it means "no break", not "loop condition was false"
Using break/continue instead of extracting logic into a function with return