Concepts / Nested Loops

Nested Loops

A correct trace should show one complete inner-loop pass for the first line, one complete pass for the second line, and one complete pass for the third line. Together, those passes cover every word on every line.

  • Programming

A Line-by-Line Hook

Imagine an input file made of several lines. Each line contains words that must be examined. One loop can move from line to line, but processing every word requires another loop inside it. The outer loop reads the lines, while the inner loop iterates through the words on the particular line currently being processed.

Input file linescollection of linesLine 1one particular lineWords on Line 1words processed by theinner loopLine 2one particular lineWords on Line 2words processed by theinner loopcontainscontainscontainscontains
How does the program move from the collection of lines to the words contained within each particular line?

The Two-Level Pattern

Definition

Nested loops are one or more loops inside another loop. The loop surrounding the other loop is the outer loop. The loop inside it is the inner loop.

The defining behavior is the completion order: every time the outer loop runs once, the inner loop runs to completion. In the file-processing pattern, the outer loop selects a line, and the inner loop processes the words on that line. Only after the inner loop has finished does the outer loop move to the next line.

Read a lineouter loop stepProcess wordsinner loop to completionNext lineouter loop continuesFinishedno more linesfor this lineafter completionif another line existsotherwise
How does the inner loop fit inside the outer loop, and how many times does the inner loop run for each line?

Execution Order

A useful way to trace nested loops is to pause after the outer loop chooses one line. At that moment, the inner loop owns the work. It visits the words on that line one by one and does not give control back to the outer loop until it has completed its pass. The outer loop then selects the next line, and the same inner-loop process begins again.

Line 1outer loop selects itWord 1inner loopWord 2inner loopLine 2outer loop selects itWord 1inner loopWord 2inner loopbegin inner loopnext wordinner loop completebegin inner loopnext word
What happens next when the outer loop reads one line and the inner loop processes each word on that line?

What do you think happens?

Suppose the outer loop selects a line containing three words. Does the outer loop immediately select the next line after the first word, or does the inner loop continue first?

  • The outer loop immediately selects the next line

  • The inner loop continues through the remaining words

  • Both loops stop

Reveal answer
Answer:

The inner loop continues through the remaining words.

The inner loop runs to completion each time the outer loop runs once. The next line is selected only after all words on the current line have been processed.

Counting Every Word

Worked example

A File with Two Lines

Consider an input file with two lines. The first line contains two words, and the second line contains three words. Trace how nested loops visit the words.

  1. 1
    Outer iteration one

    The outer loop reads the first line. The inner loop then visits the first word and the second word on that line.

  2. 2
    Outer iteration two

    After the inner loop finishes the first line, the outer loop reads the second line. The inner loop visits the first, second, and third words on that line.

  3. 3
    Complete pass

    The two loops have covered every word on every line: two words from the first line and three words from the second line.

Result

The inner loop completes separately for each line, allowing the combined nested loops to count all five words.

Outer-loop selectionInner-loop workWhat has been covered
First lineFirst word, then second wordEvery word on the first line
Second lineFirst word, then second word, then third wordEvery word on the second line

The inner loop finishes the words for one selected line before the outer loop selects another line.

Mistakes in Tracing

  • Common mistake

    Assuming the outer loop moves to the next line after processing only one word.

    What it looks like

    Treating the sequence as Line 1, Word 1, Line 2, Word 1.

    Why it fails

    The inner loop runs to completion each time the outer loop runs once.

    Do this instead

    Trace every word on the current line before moving the outer loop to the next line.

  • Common mistake

    Calling both loops the outer loop.

    What it looks like

    Describing the loop over words as if it selected lines.

    Why it fails

    The two loops have different responsibilities: the outer loop reads lines, and the inner loop iterates through words on the selected line.

    Do this instead

    Label the line-reading loop outer and the word-processing loop inner.

  • Common mistake

    Expecting one loop to process every word on every line by itself.

    What it looks like

    Counting words on the current line but never repeating that work for later lines.

    Why it fails

    The complete coverage comes from combining the outer loop over lines with the inner loop over words.

    Do this instead

    Check that the inner loop is inside the outer loop so it runs for each selected line.

Practice Trace

EASY

A file has three lines. The first line has one word, the second has two words, and the third has one word. Describe the order in which the outer and inner loops work. Then state why the complete nested-loop process reaches every word.

Hints
  • Write down the first line selected by the outer loop.
  • List every word processed by the inner loop before moving to the next line.
  • Repeat the same reasoning for all three lines.
Key point

A correct trace should show one complete inner-loop pass for the first line, one complete pass for the second line, and one complete pass for the third line. Together, those passes cover every word on every line.

Nested Loop Summary

  1. 1
    A nested loop is a loop inside another loop.
  2. 2
    The outer loop reads the lines of the input file.
  3. 3
    The inner loop iterates through the words on the currently selected line.
  4. 4
    The inner loop runs to completion each time the outer loop runs once.
  5. 5
    Together, the loops can count every word on every line.

At a glance

  • Nested loops place one loop inside another.
  • The outer loop selects each line, while the inner loop processes the words on that line.
  • The inner loop completes before the outer loop selects the next line.
  • This relationship ensures that every word on every line can be counted.

Related concepts

More concepts starting with N