Junior
What is the difference between the pass, continue, and break operators in Python?
sobes.tech AI
Answer from AI
pass- a placeholder statement that does nothing. It is used when the syntax requires a statement but no action is needed (for example, in an empty if block, function, or class).continue- skips the remaining part of the current loop iteration and proceeds to the next iteration.break- completely terminates the execution of the nearest enclosing loop (for or while).
Examples of usage:
# Example of using pass
if True:
pass # Do nothing, just a placeholder for future code
def dummy_function():
pass # Empty function
# Example of using continue
for i in range(5):
if i == 2:
continue # Skip the iteration when i=2
print(i) # Will print 0, 1, 3, 4
# Example of using break
for i in range(5):
if i == 3:
break # Exit the loop when i=3
print(i) # Will print 0, 1, 2
Summary table:
| Operator | Action | Context |
|---|---|---|
pass |
Does nothing | Any code block |
continue |
Moves to the next iteration | Loops (for, while) |
break |
Completely terminates the loop | Loops (for, while) |