Concepts / Loop Control: break and continue Statements

Loop Control: break and continue Statements

A for loop is a definite loop that processes each item in a known collection exactly once.

  • Programming
Interactive lab

Try it: while Loops, break and continue

How a while loop re-evaluates its condition before every pass, how break leaves the loop at once, how continue jumps back to the condition, how a sentinel value ends a while True loop, and why a loop whose condition never becomes False runs forever.

How it works

  1. Evaluate the condition. If it is False, skip the body and continue after the loop.
  2. If it is True, run the body from the top, then go back and evaluate the condition again.
  3. continue skips the rest of the body and goes straight back to the condition check.
  4. break leaves the loop immediately; the condition is not checked again (while True loops rely on it, e.g. stopping at a sentinel like 'done').
  5. If nothing in the body can make the condition False, the loop never ends; the lab's guard stops it after N iterations and says so.

Default run (21 steps): Ready: Countdown. A while loop checks its condition before every pass. … Finished after 5 iterations and 6 condition checks.

Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. input() returns the lines you type in the list, one per call (the prompt is not printed), and raises EOFError when they run out, like a closed keyboard stream. The guard is the lab's, not Python's.

Educational simulation

Loading the simulation…

What the Source Establishes

The supplied material defines a for loop as a definite loop that processes each item in a known collection exactly once. It explains the iteration variable, the required for and in keywords, indentation, the colon after the for statement, and the difference between for loops and while loops. However, the supplied material names break and continue in the section title without defining what either statement does. This article therefore teaches the documented for-loop foundation and clearly marks the behavior that still requires a source definition.

Definite Loop Structure

A for loop is a definite loop because it works through a known collection. The source describes its processing rule as handling each item in that collection exactly once. This makes the collection the basis for the loop's progression: the loop moves from one item to the next rather than relying on an unknown number of repetitions.

Definition

The iteration variable is the variable that holds the current item during an iteration. Its value changes automatically as the loop advances through the collection.

Known collectionitems in sequenceItem 1current itemIteration variableholds the current itemItem 2current itemItem 3current itemfirst itemnext itemnext itemassigned during iterationassigned during iterationassigned during iteration
Which value does the iteration variable contain during each pass through a known collection?

Tracing Collection Progress

Worked example

Following Three Items

Suppose a for loop processes a known collection containing three items. Trace the iteration variable as the loop advances.

  1. 1
    First iteration

    The iteration variable holds the first item in the collection.

  2. 2
    Second iteration

    The iteration variable changes automatically and holds the second item.

  3. 3
    Third iteration

    The iteration variable changes automatically again and holds the third item.

  4. 4
    Completion

    Each item in the known collection has been processed exactly once.

Result

The collection determines the sequence of items, while the iteration variable represents the current item at each stage.

Known collectionthree itemsFirst itemcurrent itemCollection processedeach item onceSecond itemcurrent itemThird itemcurrent itemvisitadvanceadvancefinish
In what order does a definite loop visit the items in a known collection?

What do you think happens?

A for loop is processing a known collection with three items. What should you expect about the number of item-processing stages?

  • The loop has a stage for each item in the collection

  • The loop must continue until a condition changes

  • The loop processes no items

Reveal answer
Answer:

The loop has a stage for each item in the collection.

The supplied definition describes a for loop as a definite loop that processes each item in a known collection exactly once.

Required For Syntax

The source identifies four structural requirements for a for statement: the keyword for, the keyword in, proper indentation, and a colon after the for statement. These requirements describe the form of the loop, while the iteration variable and collection determine what is processed.

  • Use the keyword for.
  • Use the keyword in.
  • Include a colon after the for statement.
  • Indent the statements belonging to the loop.
  • Use an iteration variable to represent the current item.

For or While

Loop typeWhen it is suitableWhat the source emphasizes
for loopThe collection or number of items to process is knownProcesses each item in a known collection exactly once
while loopThe number of iterations is unknownBetter when the iteration count is not known in advance

Choosing a loop based on whether the iteration count is known

The key distinction is knowledge of the iteration count. Choose a for loop when you know the collection size in advance. A while loop is described as more suitable when the number of iterations is unknown. This distinction is separate from the undocumented behavior of break and continue; the supplied material does not explain how either statement changes loop execution.

Mistakes to Avoid

  • Common mistake

    Treating the iteration variable as fixed

    What it looks like

    Assuming that the iteration variable keeps the first item for every stage

    Why it fails

    The source states that the iteration variable changes automatically with each iteration.

    Do this instead

    Trace the current item separately at each stage of the loop.

  • Common mistake

    Using a for loop when the iteration count is unknown

    What it looks like

    Choosing a for loop even though the stopping point is not known in advance

    Why it fails

    The source identifies for loops as ideal when the collection size is known and while loops as better for unknown iteration counts.

    Do this instead

    Base the choice on whether the number of items or iterations is known.

  • Common mistake

    Ignoring the structural requirements of a for statement

    What it looks like

    Leaving out for, in, the colon, or proper indentation

    Why it fails

    The source identifies all of these as requirements of the syntax.

    Do this instead

    Check for the required keywords, colon, and indentation before tracing the loop.

  • Common mistake

    Claiming a precise meaning for break or continue from this source alone

    What it looks like

    Explaining one of those statements without a definition in the supplied material

    Why it fails

    The source pack names the statements but does not state their effects on control flow.

    Do this instead

    Consult a source section that explicitly defines each statement before teaching or applying its behavior.

Practice Check

MEDIUM

A collection contains five known items. Explain why a for loop is an appropriate loop choice, describe what the iteration variable represents, and state how many times the collection's items are processed according to the supplied definition. Then identify the additional information you would need before explaining how break or continue affects the loop.

Hints
  • Use the definition of a definite loop.
  • Remember that the iteration variable holds the current item.
  • Separate the documented for-loop behavior from the undocumented break and continue behavior.

Key Takeaways

  1. 1
    A for loop is a definite loop over a known collection.
  2. 2
    The loop processes each item in that collection exactly once according to the supplied definition.
  3. 3
    The iteration variable holds the current item and changes automatically as the loop progresses.
  4. 4
    A for loop is preferred when the collection size is known; a while loop is better when the iteration count is unknown.
  5. 5
    The supplied source pack does not define the control-flow effects of break or continue, so those effects require additional material.

At a glance

  • A for loop processes the items of a known collection exactly once.
  • The iteration variable identifies the current item and changes automatically on each iteration.
  • The choice between for and while depends on whether the iteration count is known.
  • The supplied source pack does not provide enough information to explain the behavior of break and continue statements.

Related concepts

More concepts starting with L