Spex3
    

Loop Control Statements in Python: break, continue, pass 🚀


    

    

Loop control statements help manage the flow of loops by stopping them (break), skipping an iteration (continue), or doing nothing (pass).

1️⃣ break Statement 🛑

The break statement stops a loop when a condition is met.

🔹 Example: Breaking a for Loop

for num in range(1, 10):
if num == 5:
break # Stop the loop when num is 5
print(num)
Output:

1
2
3
4


🔹 Example: Breaking a while Loop

count = 0
while count < 10:
if count == 3:
break # Exit the loop when count is 3
print(count)
count += 1

Output:


0
1
2


2️⃣ continue Statement 🔄
The continue statement skips the current iteration and moves to the next one.

🔹 Example: Skipping an Iteration in a for Loop

for num in range(1, 6):
if num == 3:
continue # Skip when num is 3
print(num)

Output:

1
2
4
5


🔹 Example: Skipping an Iteration in a while Loop


count = 0
while count < 5:
count += 1
if count == 3:
continue # Skip printing when count is 3
print(count)

Output:


1
2
4
5

3️⃣ pass Statement ⏭️

The pass statement does nothing—it’s a placeholder used when a statement is required syntactically but you don’t want any code to execute.

🔹 Example: Using pass in a Loop


for num in range(5):
if num == 2:
pass # Placeholder (does nothing)
print(num)

Output:


0
1
2
3
4

🔹 Example: Using pass in Function Definitions

def my_function():
pass # Placeholder for future implementation

Comparison Table: break vs. continue vs. pass
Statement Effect
break Exits the loop completely
continue Skips the current iteration and moves to the next
pass Does nothing (used as a placeholder)


    Date: 2025-03-22 00:00:00.000000