Python 3D Academyবাংলা

break and continue

🌍 In real life. You're looking through a stack of exam papers for Rimi's. A blank page? Skip it and go on to the next paper. Found Rimi's paper? Stop looking at once; there's no need to check the rest of the stack.

Those two moves, "skip this one" and "stop now", are continue and break.

The idea in one sentence: continue skips the rest of this lap, and break ends the whole loop at once.

Its words:

  • continue: jump straight to the next lap
  • break: leave the loop; Python carries on below it
  • while True:: a loop that would run forever, ended only by a break

Step by step, for for n in range(1, 6): with if n == 3: continue:

  1. n is 1, then 2: the if is False, so each is printed.
  2. n is 3: continue jumps to the next lap, so 3 is never printed.
  3. n is 4, then 5: printed as usual.

You almost always put continue and break inside an if.

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

A for loop can also walk through values written in square brackets, like [4, 7, 0, 5]. That's a list, and Chapter 8 is all about lists. Here break stops the loop as soon as it reaches 0:

for n in [4, 7, 0, 5]:
    if n == 0:
        break
    print(n)
print("stopped")
5 never prints: break ended the loop at 0.

while True: makes a loop that would run forever, and break is the way out. It's handy when you keep asking until the user types a special word:

while True:
    word = input("Say something (or quit): ")
    if word == "quit":
        break
    print("You said", word)
print("Bye!")

How Python does it. In a for loop, continue takes the next value; in a while loop, it goes back to check the condition. break leaves only the innermost loop it's in.

What it costs. break can save a lot of work: a search that stops at the first match does fewer laps than one that always walks to the end. If the match is near the front, it finishes almost at once.

Mistakes and edge cases.

  • continue before the counter. In a while loop, a continue above n += 1 skips the update, so the loop can run forever.
  • break outside a loop. break on its own gives SyntaxError: 'break' outside loop.
  • break in a nested loop. It ends only the inner loop; the outer one keeps going.
  • Lines after break. Anything after break in the same block never runs.

Quiz

  1. What does this print?

    for i in range(10):
        if i == 3:
            break
        print(i)
  2. What does this print?

    for i in range(5):
        if i % 2 == 0:
            continue
        print(i)
  3. What does this print?

    n = 0
    while True:
        n += 1
        if n == 4:
            break
    print(n)
  4. What does continue do?

    • Skips the rest of this lap and starts the next one
    • Stops the whole loop
    • Starts the loop again from the very beginning
  5. Which line has a mistake?

    x = 5
    if x > 3:
        break
    print(x)

Coding tasks

  1. Print the numbers from 1 to 10, one per line, but skip every multiple of 3 (3, 6 and 9). Use continue.

  2. Keep asking for whole numbers until the user types 0. Then print the total of all the numbers they typed.

  3. Ask for a whole number n (2 or more). It is prime if no number from 2 to n - 1 divides it exactly. Print prime or not prime.