2.11 Nested Iteration
Put a loop inside a loop to build grids and patterns, and count exactly how many times the inner body runs.
- 1. LxD Cycle Process
- 2. Lesson Plan
- 3. Reference Guide
- 4. Code Examples
- A. Simple: Nested Loops in Code
- Code Runner Challenge
- 5. Hacks & Practice Tasks
- 6. Grading Plan (1 Point Total)
- 7. Lesson Revisions & Feedback Evidence
- 8. Summary
- 9. References
1. LxD Cycle Process
Empathize: Students can write two loops but cannot say how many times the inner body runs, so they cannot predict the shape their code will print or debug it when it is wrong.
Define:
- POV: CSA students need to compute inner body counts exactly, because nested iteration is where informal run-time analysis in the next topic begins.
- Learning Goal: Students will build patterns with nested loops, count inner body executions for fixed and variable bounds, and predict printed output.
Ideate:
- HMW Question: How might we connect the shape a nested loop prints to the arithmetic of how many times its inner body ran?
- Activity: A pattern builder, a how-many-stars counting activity, and an output prediction.
Prototype & Test: Activity 3 uses an inner bound that depends on the outer variable, so students meet the summed case rather than only the product case.
2. Lesson Plan
Learning Objective: Represent nested iterative processes.
Success Criteria: You can put one loop inside another, state exactly how many times the inner body runs, and build a rectangular or triangular pattern from the two loop variables.
Tech Talk & Introduction (5 minutes)
A nested loop is a loop whose body contains another loop. The inner loop finishes completely on every single pass of the outer loop.
- For an outer loop of
mpasses and an inner loop ofnpasses, the inner body runsm * ntimes. - When the inner bound depends on the outer variable, the total is the sum of the inner counts, not a simple product. That is what produces triangles instead of rectangles.
System.out.printkeeps output on one line;System.out.println()after the inner loop ends the row.
3. Reference Guide
Key Ideas
- Nested iteration is a loop inside the body of another loop.
- The inner loop runs completely (all of its passes) for each single pass of the outer loop.
- When the loops are independent, the inner body runs
outer count * inner counttimes. 3 rows of 4 columns is 12 cells. - The inner loop can depend on the outer variable, for example
j <= i. That makes triangles instead of rectangles. System.out.printstays on the same line;System.out.println()after the inner loop ends the row.- Trace nested loops by tracking the outer and inner variables separately.
Rectangle Versus Triangle
| Rectangle (independent) | Triangle (dependent) |
|---|---|
for (int i = 1; i <= 3; i++) {for (int j = 1; j <= 4; j++) {System.out.print("*");}System.out.println();} |
for (int i = 1; i <= 3; i++) {for (int j = 1; j <= i; j++) {System.out.print("*");}System.out.println();} |
************ (12 stars) |
****** (6 stars) |
4. Code Examples
A. Simple: Nested Loops in Code
A multiplication table. Run it, then change size to 5 and 10.
Code Runner Challenge
Multiplication table: the inner loop prints one row, the outer loop repeats it.
View IPYNB Source
// CODE_RUNNER: Multiplication table: the inner loop prints one row, the outer loop repeats it.
public class TimesTable {
public static void main(String[] args) {
int size = 6; // try 5 and 10
for (int row = 1; row <= size; row++) {
for (int col = 1; col <= size; col++) {
int product = row * col;
if (product < 10) {
System.out.print(" "); // pad single digits so columns line up
}
System.out.print(product + " ");
}
System.out.println();
}
}
}
TimesTable.main(null);
5. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one markdown cell at the top with the frontmatter:
---
layout: post
title: CSA Unit 2.11 Nested Iteration HW
categories: [Java, Nested-Iteration]
lesson_language: Java
lesson_topic: Nested-Iteration HW
lesson_source: APCSA
lesson_type: lesson
permalink: /csa/unit_02/2_11_hw
author: yourGithubID
---
- Add code cells for the Popcorn Hack and the Homework Hack. Ensure all code runs and output is visible.
- Include a markdown cell before each code section explaining the concept.
Submission Safety Rules (Read First)
[IMPORTANT] To avoid grading errors, follow these rules exactly:
- Write your prediction before you run each code section
- Execute every code cell and leave the output visible
- Show your work for any hand-traced prediction
- Do not just copy code; explain what each line does
- Label each question clearly
Activity 1: Trace the Grid
Step through it and watch j restart from 1 every time i changes. The grid on the right lights up one cell per inner pass.
Activity 2: Pattern Builder
Change the rows and the pattern. Read the code to see why the inner loop bound is different for each shape.
Activity 3: How Many Stars?
Activity 4: Predict the Output
What does this print?
How many times does the inner println run?
Which change makes this print a 5 by 5 square of stars instead of a triangle?
Popcorn Hack (In-Class)
Print a right triangle of numbers where each row counts up to the row number:
1
1 2
1 2 3
1 2 3 4
Code Runner Challenge
Popcorn hack: number triangle with nested loops.
View IPYNB Source
// CODE_RUNNER: Popcorn hack: number triangle with nested loops.
public class NumberTriangle {
public static void main(String[] args) {
int rows = 4;
// TODO: outer loop for the rows, inner loop that prints 1 through i,
// and a println after each row
}
}
NumberTriangle.main(null);
Homework Hack
Two short tasks.
- Finish the runner: count the pairs
(a, b)with1 <= a < b <= nwhose sum is even. Forn = 4the pairs are (1,3) and (2,4), so the answer is 2. - Answer the quick check.
Code Runner Challenge
Homework: count pairs with an even sum using nested loops. Expected for 4: 2.
View IPYNB Source
// CODE_RUNNER: Homework: count pairs with an even sum using nested loops. Expected for 4: 2.
public class EvenPairs {
public static void main(String[] args) {
int n = 4; // test 4 (2), 5 (4), 6 (6)
int pairs = 0;
// TODO: outer loop a from 1 to n, inner loop b from a + 1 to n,
// count the pair when (a + b) % 2 == 0
System.out.println("Even-sum pairs: " + pairs);
}
}
EvenPairs.main(null);
How many times does total++ run?
6. Grading Plan (1 Point Total)
Classroom Rubric
-
0.2 points: Popcorn completion Student printed a right triangle of numbers where each row counts up to the row number.
-
0.8 points: Homework completion
- 0.5 Even sum pairs: The nested loop counts pairs with
1 <= a < b <= nwhose sum is even, printing 2 forn = 4. - 0.2 Quick check: The inner body count question is answered with the arithmetic shown.
- 0.1 Notebook quality: Every code cell executed with output visible, and a markdown cell before each code section explaining the concept.
- 0.5 Even sum pairs: The nested loop counts pairs with
Quick Validation Checklist
- Present: inner loop bound related to the outer variable where the task requires it
- Present:
printlnplaced after the inner loop so rows break correctly - Absent: a pair counted twice or counted with
a == b - Test:
n = 4gives 2 pairs andn = 5gives 4
7. Lesson Revisions & Feedback Evidence
Feedback Received: Team review of the Unit 2 set found that these lessons did not follow the section format used by the Unit 1 lessons, and that they ended without the assignment submission form every other CSA lesson provides.
Revision Made: The lesson was reorganised into the shared CSA lesson format, so it now opens with the LxD cycle and lesson plan, states its reference material before the code, and groups the activities, Popcorn Hack and Homework Hack under one practice section. The duplicate lesson heading was removed, and the page now renders the standard Submit Assignment form at the end.
Design Decision Kept: Activity 3 uses an inner bound that depends on the outer variable, so students meet the summed case rather than only the product case.
Open Item: Record peer feedback from the team teach delivery here once the lesson has been taught.
8. Summary
- The inner loop runs all the way through for every pass of the outer loop.
- Independent loops multiply their counts; dependent loops (
j <= i) make triangles. printbuilds a row,println()after the inner loop ends it.- Next lesson: counting statement executions to compare how fast two loops are.
9. References
College Board Course and Exam Description
Topic 2.11, Nested Iteration, is required content on the AP Computer Science A Exam. The objective quoted in section 2 of this lesson is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
Outside Academic Reference
A nested loop is simply an iteration statement appearing as the body of another; no separate construct is defined, which is why the inner loop’s own termination rules apply in full on every outer pass.
Reference List
College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/courses/ap-computer-science-a
Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java language specification: Java SE 21 edition. Oracle America. https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.14
Submit Assignment
Need to update a submission later? Open the submissions dashboard.