Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 Beginner🔵 Advanced
Why Loops?For Loops BasicsWhile Loops BasicsLoop ControlIterating ListsLoop PatternsNested LoopsDebugging LoopsBest Practices
Python/Loops/Loop Control

🎛️ Loop Control — Break and Continue

Two statements give you power to control exactly when loops stop or skip iterations.


🎯 Break Statement

`break` exits the loop immediately:

for i in range(10):
    if i == 5:
        break  # Exits loop when i is 5
    print(i)
# Output: 0, 1, 2, 3, 4

Real example: Search and exit

for number in [1, 3, 5, 7, 9, 12]:
    if number == 12:
        print("Found even number!")
        break
    print(f"Checking {number}")

💡 Continue Statement

`continue` skips the rest of the current iteration:

for i in range(5):
    if i == 2:
        continue  # Skips to next iteration
    print(i)
# Output: 0, 1, 3, 4

Real example: Skip unwanted items

for number in [1, 2, 3, 4, 5]:
    if number % 2 == 0:
        continue  # Skip even numbers
    print(f"Odd: {number}")
# Output: Odd: 1, Odd: 3, Odd: 5

🎨 Practical Examples

Example 1: User menu with break

while True:
    choice = input("Enter 'quit' to exit: ")
    if choice == "quit":
        break
    print(f"You entered: {choice}")

Example 2: Filtering data with continue

# Process only valid scores
scores = [95, -5, 87, 100, -10, 88]
for score in scores:
    if score < 0:
        continue  # Skip invalid scores
    print(f"Valid score: {score}")

📊 Break vs Continue

StatementEffectUse When
`break`Exit entire loopFound what you need
`continue`Skip to next iterationWant to skip this item

🔑 Key Takeaways

ConceptRemember
breakCompletely exits the loop
continueSkips rest of iteration
IndentationBoth must be inside loop
NestingOnly breaks/continues innermost loop

🔗 What's Next?

Iterate through collections with Iterating Lists!


Ready to practice? Challenges | Quiz


Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn