TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 14 of 22 · By Ethan Cole · Updated November 21, 2026

Python while Loops With break, continue and else

A while loop repeats its block as long as a condition stays true. Use it when you do not know in advance how many repetitions you need: reading input until the user types quit, retrying a network call, or running a game loop. When you are walking through a known collection, a for loop is simpler.

The loop must make progress

Python checks the condition before each pass. If it is true, the body runs and Python checks again. Something inside the body must eventually make the condition false; in the countdown below, count -= 1 does that. Forget it and you have an infinite loop. In a terminal, press Ctrl+C to stop one. count -= 1 is shorthand for count = count - 1; +=, *= and friends work the same way.

Python 3 · editable

Expected output

Countdown: 3
Countdown: 2
Countdown: 1
Liftoff!
1 3 5 7
no word starts with z

break and continue

  • break leaves the innermost loop immediately, skipping any remaining items or checks.
  • continue skips the rest of the current pass and jumps back to the condition (or, in a for loop, to the next item).

A common pattern is while True: with a break inside, used when the exit test belongs in the middle of the loop rather than at the top. In the example, even numbers are skipped with continue, and the loop stops with break once n passes 7.

The else clause on loops

Both for and while loops accept an else block, which runs only if the loop finished without hitting break. It reads best as "no break". The last part of the example searches for a word starting with z; because no break happened, the else reports that nothing was found. Without it you would need an extra flag variable. Use it sparingly and comment it, since many readers do not expect it.

Common mistakes

  • Forgetting to update the variable in the condition, creating an infinite loop.
  • Placing continue before the update line, so the update is skipped and the loop never ends.
  • Thinking loop else runs when the loop condition is false at the start only. It runs whenever the loop ends normally, including after many passes.

Summary

  • while cond: repeats while cond is true and is checked before each pass.
  • Make sure every pass moves toward ending the loop.
  • break exits; continue skips to the next pass.
  • A loop's else runs only when no break occurred.

Quiz: while, break, continue

1. What does continue do inside a loop?

continue jumps straight to the next iteration.

2. When does the else block of a loop run?

Loop else is skipped if break ends the loop.

3. Which loop is infinite?

Nothing changes n, so n > 0 stays true forever.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.