Loop Control Statements
Definition: Loop control statements change the execution of a loop from its normal sequence. They allow you to stop a loop early, skip certain parts, or act as a placeholder for future code.
Why: These statements give you finer control over your logic. Instead of just running a loop from start to finish, you can react to specific data or conditions inside the loop in real-time.
1. The Break Statement
Meaning: The break statement is used to terminate the loop entirely. As soon as the program hits break, it exits the loop and moves to the next line of code outside the loop.
breakfor i in range(1, 6):
if i == 3:
break
print(i)
# Output:
# 1
# 2
Key Note: In this example, the loop stops completely as soon as i equals 3. Numbers 3, 4, and 5 are never printed.
2. The Continue Statement
Meaning: The continue statement skips the current iteration (the "round" the loop is currently in) and jumps straight to the next iteration in the sequence.
continuefor i in range(1, 6):
if i == 3:
continue
print(i)
# Output:
# 1
# 2
# 4
# 5
Key Note: Here, when i is 3, the print command is skipped. However, the loop keeps going and prints 4 and 5.
3. The Pass Statement
Meaning: The pass statement is a null operation; it does nothing. It is used as a placeholder when a statement is syntactically required but you don't want to execute any code yet.
passfor i in range(1, 6):
if i == 3:
pass # Placeholder for future logic
print(i)
# Output:
# 1
# 2
# 3
# 4
# 5
Key Note: Use pass when you are sketching out your code and want to avoid an "IndentationError" for an empty block in an if statement, loop, or function.
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute
🏋️ Test Yourself With Exercises
Take our quiz on Loop Control Statements to test your knowledge.
Exercise »