Concepts / Nested Loops: Combining Multiple Iterations

Nested Loops: Combining Multiple Iterations

To trace a nested loop, write down both current items at every pass. If the outer variable has not changed, the inner variable is still moving through its collection. The outer variable changes only after the inner loop has completed its current run.

  • Programming

Two Levels of Repetition

A nested loop is a loop placed inside the body of another loop. The outer loop selects one item, and the inner loop performs its complete iteration sequence while the outer loop remains on that item. After the inner loop finishes, the outer loop moves to its next item and the inner loop begins again.

Outer and Inner Progression

Each for loop processes every item in its known collection exactly once. In a nested arrangement, the outer loop supplies the current outer item to the loop body. Because the inner loop is inside that body, the inner loop processes its collection before the outer loop can request its next item.

Outer item AInner item 1first inner passInner item 1first inner passInner item 2second inner passOuter item BInner item 2second inner passstarts inner loopnext inner iteminner loop finishesstarts inner loop againnext inner item
For each item processed by the outer loop, how many times does the inner loop run, and in what order are all iterations executed?
Worked example

Pairing Two Collections

An outer collection contains A and B. An inner collection contains 1 and 2. Trace the order in which the nested loops process items.

  1. 1
    Step 1

    The outer iteration variable receives A. The inner loop then receives 1.

  2. 2
    Step 2

    The outer variable still contains A while the inner variable advances to 2.

  3. 3
    Step 3

    The inner loop has completed its collection, so the outer variable advances to B. The inner loop starts again with 1.

  4. 4
    Step 4

    The outer variable still contains B while the inner variable advances to 2.

Result

The processing order is A with 1, A with 2, B with 1, and B with 2.

Iteration Variables in Motion

An iteration variable holds the current item for its loop and changes automatically with each iteration. In nested loops, the outer and inner loops have separate iteration variables. The outer variable identifies the current outer item. The inner variable identifies the current inner item while the inner loop is running.

outer = Ainner = 1outer = Ainner = 2outer = Binner = 1outer = Binner = 2inner variable changesinner loop finishes; outer changesinner variable changes
Which collection item does each iteration variable contain at each step, and how does it control the current loop-body execution?
Key point

To trace a nested loop, write down both current items at every pass. If the outer variable has not changed, the inner variable is still moving through its collection. The outer variable changes only after the inner loop has completed its current run.

Tracing Every Pass

A reliable trace follows the control flow rather than jumping directly to the final result. Start with the first outer item. Then list every inner item in order. Only after the inner collection is exhausted should you move to the next outer item.

First outer itemFirst inner itemprocess current pairNext inner itemNext outer itemrestart inner loopAll outer itemsenter inner loopafter processinganother inner iteminner collection finishedanother outer itemouter collection finished
What happens next as the outer and inner loop variables change, and what processing step occurs at each iteration?

What do you think happens?

An outer collection contains A and B, and an inner collection contains 1 and 2. Which pair is processed immediately after A with 2?

  • A with 1

  • B with 1

  • B with 2

Reveal answer
Answer:

B with 1

After the inner loop finishes 1 and 2 for A, the outer variable advances to B and the inner loop starts again with its first item.

Choosing for or while

A for loop is a definite loop: it processes each item in a known collection exactly once. It is therefore suitable when the collection and its size are known in advance. A while loop is better when the number of iterations is unknown and repetition should continue based on a condition.

Questionfor loopwhile loop
What determines repetition?Items in a known collectionA condition
When is it a good fit?When the collection size is known in advanceWhen the iteration count is unknown
How does progression occur?The iteration variable changes automatically to the next itemRepetition continues while the condition supports it

The central distinction is whether the collection gives you a known set of iterations.

Frequent Tracing Errors

  • Common mistake

    Assuming the outer variable changes after every inner pass

    What it looks like

    Treating A with 1, B with 2 as the first two passes

    Why it fails

    The inner loop processes its collection before the outer loop advances.

    Do this instead

    Keep the outer item fixed while listing all inner items: A with 1, then A with 2.

  • Common mistake

    Tracking only one iteration variable

    What it looks like

    Writing down the inner items without recording which outer item is current

    Why it fails

    A nested loop has an outer current item and an inner current item.

    Do this instead

    Record both variables for every pass.

  • Common mistake

    Choosing a for loop when the iteration count is unknown

    What it looks like

    Using a known-collection loop for repetition that depends on an unknown number of condition checks

    Why it fails

    The source distinguishes for loops as suitable for known collection sizes and while loops as suitable for unknown iteration counts.

    Do this instead

    Use a while loop when the number of iterations is unknown and continuation depends on a condition.

  • Common mistake

    Forgetting the required for-statement structure

    What it looks like

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

    Why it fails

    These are required parts of the for-loop syntax.

    Do this instead

    Check for the keywords for and in, a colon after the for statement, and proper indentation.

Practice the Trace

EASY

An outer collection contains red, blue, and green. An inner collection contains small and large. List every outer-item and inner-item pair in the exact order processed by the nested loops.

Hints
  • Keep red as the outer item while processing both inner items.
  • Move to blue only after the inner collection is complete.
  • Repeat the same process for green.
MEDIUM

Decide which loop is the better fit: processing every item in a known collection, or repeating until an unknown condition becomes false. Explain your choice using the distinction between for and while loops.

Hints
  • A known collection points toward a definite loop.
  • An unknown iteration count points toward a condition-controlled loop.

Nested Loop Checklist

  • A for loop processes each item in a known collection exactly once.
  • The iteration variable holds the current item and changes automatically with each iteration.
  • In nested loops, the inner loop completes its collection for the current outer item before the outer loop advances.
  • Trace execution by recording the outer and inner variables together at every pass.
  • Use for loops for known collection sizes and while loops when the iteration count is unknown.

Key Takeaways

  • Nested loops combine an outer iteration with a complete inner iteration.
  • The inner loop runs through its collection before the outer loop moves to its next item.
  • Both iteration variables should be tracked when tracing execution.
  • For loops fit known collections; while loops fit unknown iteration counts controlled by a condition.

Related concepts

More concepts starting with N