Python 2.4Code walkthrough8 parts

break, continue and else on Loops

Control a loop from the inside.

Loading your progress…Next: 3.1 Lists

In this lesson8 parts

  1. 01Three attempts
  2. 02break: leaving a loop early
  3. 03continue: skipping one iteration
  4. 04The loop else: when a loop ends without break
  5. 05What the loop else does not mean
  6. 06while True, and break
  7. 07Where break and else mislead
  8. 08Recap, your challenge, and the module

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

break
Ends the loop at once; the program continues with the first line after it. It ends only the loop it is directly in.
continue
Ends this iteration; the loop goes on with the next item, or checks its condition again.
loop else
A block written at the same level as a for or while, that runs when the loop ends without break.

Quiz 5 questions

Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

The self-checking notebook for this lesson is Notebook 2 · Loops.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.