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

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.

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.

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

Tracing Collection Progress

Following Three Items

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

First iteration: The iteration variable holds the first item in the collection.

Second iteration: The iteration variable changes automatically and holds the second item.

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

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

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

visitadvanceadvancefinishKnown collectionthree itemsFirst itemcurrent itemCollection processedeach item onceSecond itemcurrent itemThird itemcurrent item
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

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

  • Treating the iteration variable as fixed

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

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

  • Using a for loop when the iteration count is unknown

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

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

  • Ignoring the structural requirements of a for statement

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

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

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

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

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

Key Takeaways

  • 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.