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.
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.
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.
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.
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?
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 selection | Inner-loop work | What has been covered |
|---|---|---|
| First line | First word, then second word | Every word on the first line |
| Second line | First word, then second word, then third word | Every 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
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
- A nested loop is a loop inside another loop.
- The outer loop reads the lines of the input file.
- The inner loop iterates through the words on the currently selected line.
- The inner loop runs to completion each time the outer loop runs once.
- 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.