Python Break and Continue

Let’s look at break and continue

  1. Notice

    continue skips the rest of this iteration. break exits the loop; neither automatically exits the entire program.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
limit = 6
for number in range(limit):
    if number == 1:
        continue
    if number == 4:
        break
    print(number)
print("done")
Live state

Top-level scope

limit→Object 1
number→Object 6
Object 1 · int6Object 6 · int4
This step

continue skips the rest of this iteration. break exits the loop; neither automatically exits the entire program.

Picture break and continue

continue skips the rest of this iteration. break exits the loop; neither automatically exits the entire program.

Image brief

Alt text
A diagram for Python Break and Continue. Instructions: limit = 6 for number in range(limit): if number == 1: continue if number == 4: break print(number) print("done"). Displayed result: 0 2 3 done. The final example bindings are limit --> object 1 (int); number --> object 6 (int). object 1: 6; object 6: 4
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Break and Continue". Teaching focus: continue skips the rest of this iteration. break exits the loop; neither automatically exits the entire program. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS limit = 6 for number in range(limit): if number == 1: continue if number == 4: break print(number) print("done") | v DISPLAYED RESULT 0 2 3 done FINAL BINDINGS limit --> object 1 (int) number --> object 6 (int) object 1: 6 object 6: 4 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Break and Continue. Instructions: limit = 6 for number in range(limit): if number == 1: continue if number == 4: break print(number) print("done"). Displayed result: 0 2 3 done. The final example bindings are limit --> object 1 (int); number --> object 6 (int). object 1: 6; object 6: 4

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
limit = 6
for number in range(limit):
    if number == 1:
        continue
    if number == 4:
        break
    print(number)
print("done")
Live state

Top-level scope

limit→Object 1
number→Object 6
Object 1 · int6Object 6 · int4
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
0
2
3
done

What do you expect this program to print?

What does this example display?

limit = 6
for number in range(limit):
    if number == 1:
        continue
    if number == 4:
        break
    print(number)
print("done")
What does this example display?
Choose one answer. You can try again.

Answer and explanation

0 2 3 done

Follow the printed values in order. The output is: 0 2 3 done

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

continue skips the rest of this iteration. break exits the loop; neither automatically exits the entire program.

continue skips the rest of this iteration. break exits the loop; neither automatically exits the entire program.

Break and Continue

In Python, break and continue are control statements that alter the natural flow of loops. They help you exit a loop early or skip an iteration based on a condition. These simple yet powerful tools are essential for clean and efficient loop control.

Using break in Python

The break statement is used to exit a loop prematurely when a specific condition is met. Once triggered, it immediately stops the loop and continues with the next line after the loop.

Example: Stop loop when number equals 3

for num in range(1, 6):
    if num == 3:
        print("Breaking the loop at", num)
        break
    print("Current number is", num)
Current number is 1
Current number is 2
Breaking the loop at 3

Explanation:

As soon as the number reaches 3, the break statement is executed. The loop exits, so we never see 4 or 5 printed.

Using continue in Python

The continue statement skips the current loop iteration and jumps directly to the next iteration. It’s useful when you want to ignore certain cases but continue looping.

Example: Skip number 3 but continue

for num in range(1, 6):
    if num == 3:
        print("Skipping", num)
        continue
    print("Current number is", num)
Current number is 1
Current number is 2
Skipping 3
Current number is 4
Current number is 5

Explanation:

Here, 3 is skipped, but the loop continues to process the remaining numbers. The continue prevents the print below it from executing for number 3.

Break vs Continue: What’s the Difference?

Aspect break continue
Effect on Loop Terminates the entire loop Skips the current iteration
Next Step Control goes to the first line after the loop Control goes back to the loop condition check
Usage To stop processing early To avoid processing certain conditions

Real-World Check: Avoiding Infinite Loops

If you use break or continue in a while loop, be extra careful:

  • With break, ensure that the condition to exit is reachable.
  • With continue, make sure the loop variables are still being updated, or you risk an infinite loop.

Example: While loop with break

count = 1
while True:
    print("Count is", count)
    if count == 5:
        break
    count += 1
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5

Best Practices

  • Use break for early exit when a target is found—such as stopping after finding a match in a search.
  • Use continue when you want to filter out certain conditions but still want the loop to proceed.
  • Avoid overly complex loop conditions—let break and continue manage clarity inside the loop.

What to remember

Connect what you noticed in the example with the idea below.

Remember

continue skips the rest of this iteration. break exits the loop; neither automatically exits the entire program.

Watch out for this idea

continue exits the entire loop.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Collections →