Concepts / Nested Loops

Nested Loops

You will see that we have two for loops. The outer loop is reading the lines of the file and the inner loop is iterating through each of the words on that particular line. This is an example of a pattern called nested loops because one of the loops is the outer loop and the other loop is the inner loop.

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

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

The Two-Level Pattern

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.

for this lineafter completionif another line existsotherwiseRead a lineouter loop stepProcess wordsinner loop to completionNext lineouter loop continuesFinishedno more lines
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.

begin inner loopnext wordinner loop completebegin inner loopnext wordLine 1outer loop selects itWord 1inner loopWord 2inner loopLine 2outer loop selects itWord 1inner loopWord 2inner loop
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

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.

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.

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.

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.

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

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

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

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

  • Calling both loops the outer loop.

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

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

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

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

    Fix: 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.

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

Key Takeaways

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