Skip to content

Course navigation

For teachersTopic 2.11 · Lesson kit

Nested Iteration

  • 3 class periods
  • 45 minutes each
  • Objective 2.11.A
  • 6 downloads with keys

Downloads

At a glance

Pacing
3 class periods of 45 minutes
Learning objectives
2.11.A
Essential knowledge
2.11.A.1
Materials
Projector and the Loop-de-Loop slide deckWhiteboard and markersMini whiteboards or scrap paperIDE on every computer (Days 1 to 3)Printed Loop-de-Loop puzzle worksheet (answer key for you) and exit ticket

45 minutes a day

Agenda

Day 145 minutes

  • Warm-up
  • Direct instruction
  • Guided practice
  • Closure
  • Activity
  1. 0:00–0:055 minWarm-upwrite out the whole plan (slide 2)
  2. 0:05–0:2015 minDirect instructionthe inner loop finishes first; don't reuse the name (slides 3–7)
  3. 0:20–0:255 minActivityDay 1 pause and try — count down, then count up (slide 8)
  4. 0:25–0:4015 minGuided practicepredict, then run OrderTrace and a nested loop of your own
  5. 0:40–0:455 minClosurewho moves first?

Day 245 minutes

  • Warm-up
  • Direct instruction
  • Guided practice
  • Closure
  • Activity
  1. 0:00–0:055 minWarm-uphow many lines print?
  2. 0:05–0:2217 minDirect instructionrows and columns, which variable to print, where println goes (slides 9–12)
  3. 0:22–0:275 minActivityDay 2 pause and try — where do the X's land? (slide 13)
  4. 0:27–0:4013 minGuided practicedraw a rectangle and a numbered grid in the IDE
  5. 0:40–0:455 minClosureprintln inside or after?

Day 345 minutes

  • Warm-up
  • Direct instruction
  • Closure
  • Exit ticket
  • Activity
  1. 0:00–0:055 minWarm-uphow many times does the body run?
  2. 0:05–0:2015 minDirect instructionan inner loop that depends on the outer variable (slides 14–17)
  3. 0:20–0:255 minActivityDay 3 pause and try — where does the inner loop start? (slide 18)
  4. 0:25–0:3510 minActivityDay 3 putting it together — every letter against every later letter (slide 19)
  5. 0:35–0:405 minClosureslides 20 and 21
  6. 0:40–0:455 minExit ticket

Overview

A nested loop is a loop in the body of another loop (2.11.A.1). Day 1: the inner loop runs completely, from its start to its end, each time the outer body runs, so the outer variable holds still while the inner variable moves. Day 2: rows and columns, where the inner loop prints one row and the outer loop repeats it for every row. Day 3: an inner loop whose bounds use the outer variable, so the number of inner passes changes on each outer pass, and counting the total passes.

Students know for and while loops (2.7, 2.8), print versus println, and counters. Stay inside the boundary: no arrays, no ArrayList, no char, and no run-time analysis vocabulary beyond counting passes (formal comparison of run times is Topic 2.12).

Teacher note

The slides are the Loop-de-Loop deck (21 slides); slide numbers refer to it. Day 1 uses slides 2–8, Day 2 slides 9–13, Day 3 slides 14–21. The worksheet (10 questions, about 35 minutes) is homework: questions 1, 2, and 6 after Day 1; questions 3, 4, and 5 after Day 2; questions 7, 8, 9, and 10 after Day 3. Question 9 settles the Think First guess about an inner while loop’s counter.

Demonstrations are in the topic’s examples/ folder (.snippet.java files hold only the statements for main): OrderTrace, SameNameFixed, CountDown (Day 1); Rectangle, RowNumbers, PrintlnInside, PrintlnFixed (Day 2); Triangle, ShrinkingNumbers, TriangleOffByOne, CountNoReset, CountReset, TriangleWhileBug, TriangleWhileFixed (Day 3).

Day 1 · Warm-up: Write out the whole plan

Show slide 2. Students write, in their own words, the full plan the slide describes, and notice that one repeated task is itself made of a repeated smaller task. Take two answers and keep them for the closure.

Day 1 · Direct instruction: The inner loop finishes first

Show slides 3 and 5, then run OrderTrace: outer 1 pairs with inner 1 and 2, then outer 2, then outer 3, six lines in all. Slide 6 shows the check-and-print sequence; have students call out each line before it appears. Slide 7: the inner loop cannot reuse the outer loop’s variable name.

Misconception: Reusing the outer loop's variable name

Students paste a loop header with int i inside another loop that already declares i. The outer variable still exists inside the outer body, so Java reports that i is already defined; the code does not compile. Compare SameName and SameNameFixed: rename the inner variable.

Day 1 pause and try

Slide 8: count down, then count up. Students write the exact output first, then say how many times the inner body ran per outer pass.

Day 1 · Guided practice: Guided practice

Students predict the output of OrderTrace on mini whiteboards, then run it in the IDE. Then each student writes a nested loop of their own (for example, three outer values and four inner values, printing both numbers) and trades with a partner, who predicts the output before running it.

Day 1 · Independent practice: Independent practice

Puzzle worksheet questions 1, 2, and 6 are homework: the JVM loop with two prints per pass, the pencil check table for c <= r, and the outer-versus-inner face-off.

Day 1 · Closure: Closure

Revisit the warm-up answers. Cold-call: “The outer loop runs 5 times and the inner runs 2 times each time. How many times does the inner body run in all?” (10.)

Day 2 · Warm-up: Warm-up

Show for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) { System.out.print("*"); } } with no println. Students say how many stars print (12) and on how many lines (one).

Day 2 · Direct instruction: Rows and columns

Slide 10: the inner loop prints one row; the outer loop repeats it for every row; System.out.println() after the inner loop ends each row. Slide 11 (worked example): print the inner variable for column numbers, the outer variable for row numbers; run RowNumbers. Slide 12: println inside the inner braces ends the line after every star.

Misconception: println in the wrong place

A student wants 2 rows of 4 stars but puts println() inside the inner braces, so every star gets its own line. Run PrintlnInside (8 lines of *), then PrintlnFixed. Teach: ending a row belongs after the inner loop, in the outer body.

Day 2 pause and try

Slide 13: students mark where the X’s land in the grid and write the output. Check by tracing the first two rows aloud.

Day 2 · Guided practice: Guided practice

In the IDE, students draw a rectangle (Rectangle) with their own row and column counts, then a grid of numbers (RowNumbers), then change which variable is printed. Move around the room: braces decide which loop a line belongs to, not indentation (worksheet WATCH OUT after question 6).

Day 2 · Independent practice: Independent practice

Puzzle worksheet questions 3, 4, and 5 (code tiles for the star rows, the snippet pool for the shrinking number rows, and the match-up that counts passes) are homework.

Day 2 · Closure: Closure

Show PrintlnInside’s code. Ask: “Where should the println() go to print two rows of four stars?” (Right after the inner closing brace.)

Day 3 · Warm-up: Warm-up

Show the count++ match-up idea from worksheet question 5, version 2, on the board: for i from 1 to 4, for j from 1 to i. Students predict the final count (10) and keep their guess for slide 15.

Day 3 · Direct instruction: Inner depends on outer

Slide 15: when the inner condition uses the outer variable, the inner loop runs a different number of times on every pass (1, 2, 3, 4, 5 for the triangle). Run Triangle. Slide 16: two inner loops in one row. Slide 17: a mistake in the inner condition repeats on every row; run TriangleOffByOne.

Misconception: An inner counter that never starts over

With while loops, you write the inner starting value yourself. Put int game = 1; before the outer loop and it runs once, so after the first outer pass the inner condition is false forever. Run CountNoReset (prints Games played: 3, not 12), then CountReset. The same slip is TriangleWhileBug, which prints only one star per row. The inner counter must be set inside the outer body. Worksheet question 9 settles it.

Misconception: An off-by-one inner condition

< instead of <= in the inner loop drops one item from every row, so the output is wrong in every row, not once. Have students count the missing stars in TriangleOffByOne.

Day 3 pause and try

Slide 18: students find where the inner loop starts and write the output. Ask what changes if it starts at row instead of 1.

Day 3 putting it together

Slide 19: every letter paired with every later letter, so the inner loop starts after the outer index. Students trace the first two outer passes before the output is revealed.

Day 3 · Independent practice: Independent practice

Puzzle worksheet questions 7, 8, 9, and 10 are homework (you’re the compiler, two truths and a lie, the one-line move, the wrap-up).

Day 3 · Closure: Closure and exit ticket

Show slide 20 (vocabulary), then slide 21: partners explain the order in which a nested loop runs. Give the exit ticket (about five minutes). Question 1 checks tracing a nested loop with a dependent inner bound, question 2 the inner counter that never restarts, and question 3 the same bug in a while nest (2.11.A.1). Re-teach the weakest one at the start of Topic 2.12.

Exit ticket

Question 1 · Tracing

Write the exact output.

for (int row = 1; row <= 4; row++)
{
    for (int star = 1; star <= row; star++)
    {
        System.out.print("*");
    }
    System.out.println();
}
Check your reasoning
*
**
***
****

The inner loop runs from 1 to row: once when row is 1, twice when 2, three times when 3, four times when 4. The println() after the inner loop ends each row. A complete answer has four lines of 1, 2, 3, and 4 stars.

Question 2 · Multiple choice

Four teams each play three games, and count should hold the total. What does this code print?

int count = 0;
int team = 1;
int game = 1;
while (team <= 4)
{
    while (game <= 3)
    {
        count++;
        game++;
    }
    team++;
}
System.out.println("Games played: " + count);
  1. Games played: 12
  2. Games played: 7
  3. Games played: 3
  4. Games played: 4
Check your reasoning

C. game is set to 1 once, before both loops. During team 1 it counts up to 4, so the inner body runs 3 times. For teams 2 to 4 the condition game <= 3 is false at once, so the inner body never runs. A is what the code was meant to print, but game is never reset. B adds the teams and games (4 + 3) instead of tracing. D is the number of teams.

Question 3 · Short answer

This code should print a triangle of 1, 2, then 3 stars. Does it compile? What does it print, and how do you fix it?

int row = 1;
int star = 1;
while (row <= 3)
{
    while (star <= row)
    {
        System.out.print("*");
        star++;
    }
    System.out.println();
    row++;
}
Check your reasoning

Model answer: It compiles and prints three lines with one * each. star is set to 1 once, before the outer loop, so after the first row it is 2, and the inner condition star <= row is true only once on each later row. Fix: move int star = 1; inside the outer loop, before the inner loop, so it restarts on every row.

A complete answer says it compiles, gives the output (three lines of one star), and explains that the inner counter must be reset inside the outer loop.