Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
limit = 6
for number in range(limit):
if number == 1:
continue
if number == 4:
break
print(number)
print("done")Top-level scope
6Object 6 · int4continue skips the rest of this iteration. break exits the loop; neither automatically exits the entire program.
(no output)
What do you expect this program to print?
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: 4Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
limit = 6
for number in range(limit):
if number == 1:
continue
if number == 4:
break
print(number)
print("done")Top-level scope
6Object 6 · int4The example has finished. Compare the final output with the values and references above.
0 2 3 done
What do you expect this program to print?
limit = 6
for number in range(limit):
if number == 1:
continue
if number == 4:
break
print(number)
print("done")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.
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.
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
As soon as the number reaches 3, the break statement is executed. The loop exits, so we never see 4 or 5 printed.
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.
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
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.
| 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 |
If you use break or continue in a while loop, be extra careful:
break, ensure that the condition to exit is reachable.continue, make sure the loop variables are still being updated, or you risk an infinite loop.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
break for early exit when a target is found—such as stopping after finding a match in a search.continue when you want to filter out certain conditions but still want the loop to proceed.break and continue manage clarity inside the loop.Connect what you noticed in the example with the idea below.
continue skips the rest of this iteration. break exits the loop; neither automatically exits the entire program.
continue exits the entire loop.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.