Python break-continue-pass
Table of Contents
Description
These are loop control statements in Python used to modify the flow of execution inside loops.
break: Immediately exits the loop.
continue: Skips the current iteration and proceeds with the next.
pass: Does nothing—acts as a placeholder for future code.
Prerequisites
- Knowledge of loops (for, while)
- Understanding of conditional statements
- Basic grasp of Python syntax and indentation
Examples
Here's a simple program in Python:
# Using break: Exit loop when number is 3 for i in range(1, 6): if i == 3: break # Loop stops at i=3 print(i) # Output: 1, 2 # Using continue: Skip number 3 for i in range(1, 6): if i == 3: continue # Skips printing 3 print(i) # Output: 1, 2, 4, 5 # Using pass: Placeholder that does nothing for i in range(1, 4): if i == 2: pass # No operation performed print(f"i = {i}") # Output: 1, 2, 3
Real-World Applications
Break:
Stop searching when the item is found.
Exit from an infinite loop on condition.
Continue:
Skip processing bad/invalid data.
Skip unnecessary calculations (e.g., odd numbers only).
Pass:
Placeholder for unimplemented logic.
Used in try-except blocks where no action is needed.
Where topic Can Be Applied
Game logic: Ending games (break) or skipping enemy turns (continue)
Data processing: Skipping null/missing data fields
Template design: Using pass for empty functions or classes
Web crawling: Skip irrelevant links (continue) or exit early if max depth is reached (break)
Backend systems: Manage infinite server loops with controlled exits
Resources
Topic video source
A comprehensive video
Python pdf
pdf on topic
Interview Questions
What does the break keyword do inside a loop?
How is continue different from break?
Can pass be used in if or function blocks? Why?
Write a loop to print all even numbers from 1 to 10 using continue.
Give a scenario where using pass is more appropriate than doing nothing.