Loading...

Python Break and Continue

The while page ended only when its question became False. break does not wait for that. It leaves the loop immediately, even while cups < 5 is still True. continue does not leave. It skips the lines under it for this one pass, then the loop asks the question again. print("done"), written level with while, is outside. It runs after break, and it runs when the loop later ends on its own.

Start cups at 0. The question is cups < 5. Each pass prints the number first. When that number is 2, break runs. cups = cups + 1 is under the break, so it does not run on that pass. 2 is still less than 5. The question has not failed. The loop is over anyway.

Leave after printing 2

python
cups = 0
while cups < 5:
    print(cups)
    if cups == 2:
        break
    cups = cups + 1
print("done")

The screen shows 0, then 1, then 2, then done. There is no 3 and no 4. cups holds 2. That 2 was printed, and the loop left before the number could move.

Skip one pass, and stay in the loop

continue needs the number to move before the skip. Otherwise the value you are skipping never changes, the question stays True, and the loop does not finish. In this program the first line inside the loop adds 1. When the number is 2, continue skips print for this pass only. The loop returns to cups < 4. Another pass starts, the number becomes 3, and 3 is printed.

Skip 2, then keep going

python
cups = 0
while cups < 4:
    cups = cups + 1
    if cups == 2:
        continue
    print(cups)
print("done")

You see 1, then 3, then 4, then done. The missing line is 2. done is there because continue never abandoned the loop. The question failed later, on its own, and the outside line ran once.

The same break, placed before print

Move break above print. The pass that reaches 2 leaves before anything is written. done still runs, because it is level with while.

Leave before 2 is written

python
cups = 0
while cups < 5:
    if cups == 2:
        break
    print(cups)
    cups = cups + 1
print("done")

The screen shows 0, then 1, then done. cups is 2, and this time that 2 was not printed. The programs differ only in where break sits.

Write continue above cups = cups + 1 and the skip can freeze the number. cups stays on the value you meant to skip, the question stays True, and the loop does not finish. Do not run that arrangement. Move the number first. Then decide whether this pass should skip the lines under continue.

break and continue are lowercase words. Break or Continue with a capital letter is a name Python does not know, and the program stops. The next page numbers each item it walks, and it pairs two lists. It does not use these two words.

Work it out first

  1. The first program prints, then breaks when cups is 2. What appears, and what does cups hold afterward?
  2. The continue program adds 1 before it skips. Which numbers are written, and does done appear?
  3. The third program breaks before print. Is 2 on the screen?
  4. You move continue above cups = cups + 1, and the skip is still true. Does the loop finish?

Where the result changes

  • Waiting for cups < 5 to fail after you have already written break
  • Expecting 3 and 4 once break has run at 2
  • Putting continue before the number changes
  • Reading a line placed under break as if it ran
  • Writing Break or Continue with a capital letter

One reading

break leaves at once. The while question can still be True. A line level with while still runs, and a line under break does not.
If break sits after print, you see 0, 1, 2, then done, and cups stays 2. If break sits before print, you see 0, 1, then done, and cups is still 2.
continue skips only the lines under it, for this pass. With the number moved first, you see 1, 3, 4, then done, and 2 is absent.
If the number does not move, continue can keep the question True and the loop does not finish.
The next page numbers each item and pairs two lists.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top