LEARN Python - 3.3 Break and Continue When Enough Is Enough - zerotopyhero.com

Learn Python – 3.3: Break and Continue – When Enough Is Enough

Teaching Python When to Stop (or Skip Ahead)

Loops are powerful.

Sometimes too powerful.

You’ve already seen that loops can repeat code many times — maybe even forever if you’re not careful.
So Python gives you two simple tools to stay in control:

  • break → stop the loop completely

  • continue → skip the current round and move on

They’re small words with very big impact.

Why We Need Loop Control

Imagine this situation:

  • You’re looping

  • Something special happens

  • You want to stop early

Or maybe:

  • You want to ignore certain cases

  • But keep the loop going

Without control, Python would just keep looping no matter what.

That’s not always what you want.

break – Stop Everything Right Now

break tells Python:

“We’re done here. Leave the loop.”

The moment Python hits break, the loop ends immediately.

Example:

				
					while True:
     answer = input("Type 'quit' to stop: ")

     if answer == "quit":
        break

     print("Still running...")
				
			

What happens?

  • Python keeps looping

  • The moment the user types quit

  • break runs

  • The loop ends

No more checks.
No more repetitions.

Python moves on.

A Simple For Loop Example with break

				
					for i in range(10):
     print(i)

     if i == 3:
        break

				
			

Output:

				
					0
1
2
3

				
			

Even though the loop could go to 9, it stops early.

break always exits the loop completely.

continue – Skip This Round, Keep Going

continue is different.

It tells Python:

“Skip the rest of this loop iteration and jump back to the top.”

The loop does not stop.

Example:

				
					for i in range(5):
     if i == 2:
        continue

     print(i)

				
			

Output:

				
					0
1
3
4
				
			

When i is 2:

  • Python hits continue

  • Skips print(i)

  • Moves on to the next number

Everything else runs normally.

A While Loop Example with continue

				
					number = 0

while number < 5:
    number += 1

    if number == 3:
        continue

    print(number)
				
			

Output:

				
					1
2
4
5

				
			

The loop still runs five times, it just skips one print.

Break vs Continue (In Plain English)

Think of it like this:

  • break → “We’re leaving the loop.”

  • continue → “Skip this one, try again.”

Both are useful.
Both are intentional.

A Common Beginner Mistake

Using continue without changing anything.

Example:

				
					
while x < 10:
    if x == 5:
        continue
    x += 1
				
			

What happens?

  • When x is 5

  • continue runs

  • x never changes

  • Infinite loop

Rule of thumb:
Make sure something still changes even when you use continue.

When to Use Each

Use break when:

  • You found what you were looking for

  • The user wants to quit

  • Continuing no longer makes sense

Use continue when:

  • You want to skip invalid input

  • You want to ignore certain cases

  • You want to move to the next round cleanly

What You’ve Learned

You now know how to:

  • Stop loops early

  • Skip loop iterations safely

  • Control loop flow

  • Avoid unnecessary repetition

  • Prevent runaway behavior

This is what turns loops from “dangerous” into “useful”.

Mini Quiz

Try these:

  1. What does break do to a loop?

  2. What does continue do?

  3. Will continue end the loop? Why or why not?

  4. What will this print?

				
					
for i in range(6):
    if i == 4:
        break
    print(i)

				
			

     5. What about this?

				
					
for i in range(6):
    if i == 4:
        continue
    print(i)


				
			

Coming Up Next

You’ve learned how to repeat things
and how to control that repetition.

Next up, we combine loops inside loops.

Yes, really.

ZeroToPyHero