TalentJR

🐍 Python for kids · Class 6 · Lesson 7 of 18 · Free

Break and Continue – Controlling Loops

⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Stop a loop early with break as soon as you find what you need.
  • Skip one round of a loop with continue and keep going.
  • Build a while True loop that ends when the user types quit.

You are searching your school bag for your geometry box. Once you find it, you stop searching. You do not empty the whole bag! Python loops can do the same with break.

What does break do in a Python loop?

Normally a loop runs every round it was given. break changes that.

When Python reaches break, it leaves the loop at once. It jumps to the first line after the loop.

Below, the loop could count to 9. But break stops it the moment n becomes 5.

🎬 How Python thinks — step 1 of 18Python is about to run line 1
▶for n in range(1, 10):
2 if n == 5:
3 print("Found 5, stopping!")
4 break
5 print(n)
6print("Loop is over.")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
1
2
3
4
Found 5, stopping!
Loop is over.

💡 break always sits inside an if. Without an if, the loop would stop in the very first round.

How do I search a list and stop when I find something?

Searching is the most common use of break. Checking more items after a match only wastes time.

Here we look for the first mark below 35 in a class list. When we find it, we remember it and stop.

The variable failed starts as -1. That value means "not found yet", because no real mark is -1.

Look at the trace. The marks 85 and 30 are never checked, because the loop already ended.

🎬 How Python thinks — step 1 of 18Python is about to run line 1
▶marks = [78, 91, 64, 28, 85, 30]
2failed = -1
3for m in marks:
4 print("Checking", m)
5 if m < 35:
6 failed = m
7 break
8print("First low mark:", failed)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Checking 78
Checking 91
Checking 64
Checking 28
First low mark: 28

What does continue do in a Python loop?

continue is gentler than break. It skips only the rest of the current round.

Python then jumps back to the top of the loop and starts the next round.

Think of a ticket checker at a cinema. A person without a ticket is skipped, but the line keeps moving.

Here the % operator gives the remainder. If n % 3 is 0, n is a multiple of 3, so we skip it.

🎬 How Python thinks — step 1 of 34Python is about to run line 1
▶for n in range(1, 11):
2 if n % 3 == 0:
3 continue
4 print(n, end=" ")
5print()
6print("Multiples of 3 were skipped.")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
1 2 4 5 7 8 10 
Multiples of 3 were skipped.

💡 end=" " puts a space instead of a new line. So the numbers stay on one row.

How do I skip bad data with continue?

Real data is often messy. Some entries are empty or wrong.

continue lets you ignore a bad entry and still handle the good ones.

Here a temperature of -999 means the sensor broke that day. We skip it so the average stays honest.

🐍 Python — you can edit this code
Output
Skipping a broken reading
Skipping a broken reading
Average of 5 good days: 33.0

How do I use while True with break?

while True: makes a loop that would run forever. That sounds scary, but break gives it an exit.

This pattern is perfect when the user decides when to stop. The loop asks again and again.

When the user types quit, break ends the loop. Every other answer is handled normally.

Notice the check for quit comes first. If it came after int(item), Python would crash on the word quit.

🐍 Python — you can edit this code
Output
Price of item (or quit): 40
Bill so far: 40
Price of item (or quit): 25
Bill so far: 65
Price of item (or quit): 60
Bill so far: 125
Price of item (or quit): quit
Final bill: 125 rupees

Can I use break and continue in the same loop?

Yes. Each one does its own job. continue skips a round, and break ends the whole loop.

Below, a cricket batter's runs ball by ball are checked. Dot balls (0) are skipped.

The word "out" ends the innings, so break stops the loop there.

🐍 Python — you can edit this code
Output
Runs so far: 4
Runs so far: 5
Runs so far: 11
Out! Innings over.
Final score: 11

💡 The last "4" is never counted, because break ended the loop before reaching it.

🤔 Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

for i in range(1, 8):
    if i == 4:
        break
    print(i)

Question 2. What does this print?

for i in range(1, 6):
    if i == 2 or i == 4:
        continue
    print(i)

Question 3. What does this print?

n = 0
total = 0
while True:
    n = n + 1
    if n > 4:
        break
    total = total + n
print(total)

🧩 Your challenge

Make a quiz checker. Keep asking "What is 7 x 8?" until the user types the right answer (56). Skip empty answers with continue without counting them. Use break when the answer is right, then print how many real tries it took.

Expected output (with the inputs given):
What is 7 x 8? 54
Not quite, try again.
What is 7 x 8? 
What is 7 x 8? 63
Not quite, try again.
What is 7 x 8? 56
Well done!
You needed 3 tries.
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

for n in range(1, 6):
    print(n)
    break

No error message – but it prints only 1, because break runs in the very first round.

✅ Right

for n in range(1, 6):
    print(n)
    if n == 3:
        break

break with no if around it always ends the loop at once. Put it inside an if.

❌ Wrong

n = 0
while n < 5:
    if n == 2:
        continue
    print(n)
    n = n + 1

No error message – the program never stops, because n gets stuck at 2.

✅ Right

n = 0
while n < 5:
    n = n + 1
    if n == 2:
        continue
    print(n)

continue jumped over n = n + 1, so n stayed 2 forever. In a while loop, change the variable before continue.

❌ Wrong

if 5 > 3:
    print("Done")
    break

SyntaxError: 'break' outside loop

✅ Right

for i in range(3):
    if i == 1:
        print("Done")
        break

break and continue only work inside a for or while loop. An if alone is not a loop.

📌 Remember

  • break leaves the loop at once and jumps to the line after it.
  • continue skips the rest of this round and starts the next round.
  • Put break and continue inside an if, so they run only when needed.
  • Use break to stop a search as soon as you find a match.
  • while True: with break is great for menus that end on quit.
  • In a while loop, update the counter before any continue.

Finished the quiz and the challenge?

Questions students ask

What is the difference between break and continue in Python?

break ends the whole loop. continue only skips the current round and the loop carries on with the next one.

Is while True a bad idea in Python?

No, as long as there is a break inside it. It is a common pattern for menus and games that run until the user quits.

Does break stop the whole program?

No. break only leaves the loop it is in. The lines after the loop still run normally.

Can I use break outside a loop?

No. Python gives a SyntaxError, because break and continue only make sense inside a for or while loop.