In Python, break, continue, and pass are used to control the flow of loops. They allow you to alter the normal sequence of execution in a loop.
The break statement is used to exit the current loop prematurely when a specified condition is met.
# Using break to stop the loop for i in range(10): if i == 5: break print(i) # Output: 0, 1, 2, 3, 4
The continue statement is used to skip the current iteration of the loop and proceed with the next iteration.
# Using continue to skip the current iteration for i in range(10): if i == 5: continue print(i) # Output: 0, 1, 2, 3, 4, 6, 7, 8, 9
The pass statement is a null operation. It serves as a placeholder in a block where code is syntactically required but you don't want to execute anything.
# Using pass when no action is needed for i in range(10): if i == 5: pass # No action performed for i == 5 print(i) # Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
break
statement ends the loop entirely.
- The continue
statement skips the rest of the current iteration and moves to the next one.
- The pass
statement does nothing and is often used as a placeholder in loops, functions, or classes.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!