Python provides loop control statements — break, continue, and pass — to control the flow of loops (for or while).
These statements help in skipping, terminating, or doing nothing during iteration.
| Statement | Description | Use Case |
|---|---|---|
break | Terminates the current loop and transfers control to the next statement after the loop. | When you want to exit the loop early based on a condition. |
continue | Skips the current iteration and moves to the next iteration of the loop. | When you want to skip specific iterations but continue looping. |
pass | Does nothing; it’s a placeholder used when a statement is syntactically required but you don’t want to execute any code. | Used as a placeholder for future code or empty blocks. |
🧩 Example 1: Using break
for i in range(1, 6):
if i == 3:
break # Stop the loop when i equals 3
print(i)Output:
1
2(Loop stops as soon as i becomes 3.)
🧩 Example 2: Using continue
for i in range(1, 6):
if i == 3:
continue # Skip the current iteration when i equals 3
print(i)Output:
1
2
4
5(i = 3 is skipped, loop continues for remaining values.)
🧩 Example 3: Using pass
for i in range(1, 6):
if i == 3:
pass # Do nothing, placeholder for future logic
print(i)Output:
1
2
3
4
5(pass simply does nothing and allows the program to continue.)
🧩 Example 4: Using in while Loop
i = 1
while i <= 5:
if i == 3:
i += 1
continue
if i == 5:
break
print(i)
i += 1Output:
1
2
4🧩 Example 5: Using pass as a Placeholder
def future_function():
pass # Function defined but implementation will be added later
class MyClass:
pass # Class placeholderOutput:
# No output, but valid syntax — used for structure definition🔹 In Summary
break→ Immediately terminates the loop.continue→ Skips the current iteration and continues with the next one.pass→ Does nothing, used as a placeholder for empty code blocks.
These control statements help improve loop control, code readability, and logical flow management in Python programs.
