AP CSA Nested Loops
Nested Loops in AP CSA: Complete Guide (2025-2026)
Nested loops in AP CSA are two or more loops where one loop is placed inside the body of another, and they are a staple of the AP Computer Science A exam in Unit 2 (25–35%). The outer loop controls the rows; the inner loop controls the columns. For every single iteration of the outer loop, the inner loop runs its COMPLETE cycle from start to finish. Counting total iterations across nested loops — and tracing the values of both loop variables simultaneously — is a required skill for AP exam success.
📄 Table of Contents
💻 Code Examples — Predict First
Before running each example, write down your prediction. This is the single most effective AP exam study technique.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
System.out.print(row * col + " ");
}
System.out.println();
}
}
}
Row 1: 1 2 3 / Row 2: 2 4 6 / Row 3: 3 6 9 — For each outer row, the inner loop runs 3 complete cycles. Total: 9 iterations of the inner body.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
int count = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
count++;
}
}
System.out.println(count);
}
}
12 — Outer runs 4 times (i=0,1,2,3). For each, inner runs 3 times (j=0,1,2). Total = 4 × 3 = 12.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
* / * * / * * * / * * * * — When the inner loop limit depends on the outer variable (j <= i), the inner loop runs a DIFFERENT number of times each row. Total iterations = 1+2+3+4 = 10.
❌ Common Pitfalls
These are the mistakes students most often make on the AP CSA exam with nested loops AP CSA. Study them carefully.
Every time the outer loop advances, the inner loop's variable is re-initialized from scratch. The inner variable does NOT carry over between outer iterations. Students often trace as if j continues from where it left off.
// Each time outer runs, j RESTARTS at 0:
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) { ... }
// j is back to 0 next outer iteration
}
When the inner loop bound uses the outer variable (like j <= i), total iterations are NOT simply outer_count × fixed_inner. You must sum the inner counts: 1+2+3+...+n = n(n+1)/2.
// NOT 4x4=16 iterations. Total is 1+2+3+4=10
for (int i=1; i<=4; i++)
for (int j=1; j<=i; j++) { }
In a nested loop, the outer variable (e.g., row) controls the row and the inner variable (col) controls the column. Accidentally printing the outer variable in the inner loop produces incorrect patterns.
An off-by-one in either loop propagates across all iterations. A wrong outer limit affects all rows; a wrong inner limit affects all columns. The AP exam tests both, sometimes with limits like n-1 vs n.
On the AP CSA exam, to count total nested loop iterations: if the inner loop runs a fixed number of times, multiply outer_count × inner_count. If the inner limit depends on the outer variable, draw a small table and sum the inner counts for each outer iteration.
The AP FRQ section frequently uses nested loops to process 2D arrays. The outer loop index = row, inner loop index = column. When you see grid[i][j], i is always the outer loop variable and j is the inner.
✍ Check for Understanding (8 Questions)
System.out.println() execute?for(int i=0;i<5;i++) for(int j=0;j<4;j++) System.out.println();
for(int i=1;i<=3;i++)
for(int j=1;j<=2;j++)
System.out.print(i+","+j+" ");
count after this code?int count=0;
for(int i=1;i<=4;i++)
for(int j=0;j count++;
for(int r=4;r>=1;r--)
for(int c=1;c<=r;c++)
System.out.print("*");
System.out.println();
for(int i=0;i<3;i++)
for(int j=i;j<3;j++)
/* body */
for(int i=2;i<=4;i++)
for(int j=1;j<=3;j++)
if(i==j) System.out.print(i+" ");
int total=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
total++;What is total in terms of n?
❓ Frequently Asked Questions
In a nested loop, the inner loop runs completely for each single iteration of the outer loop. If the outer runs m times and the inner runs n times, the inner body executes m x n times total (assuming constant inner bounds).
If both loops have fixed bounds, multiply outer_count x inner_count. If the inner loop's bound depends on the outer variable (like j <= i), you must add up the inner counts manually: 1+2+3+...+n = n(n+1)/2.
Nested loops are the standard way to process every element in a 2D array. The outer loop iterates over rows (arr.length), the inner loop iterates over columns (arr[0].length). This pattern appears on AP FRQs almost every year.
No. Each loop variable is scoped to its own loop. The inner variable j is re-initialized at the start of each outer iteration. The outer variable i is accessible inside the inner loop body.
The inner loop typically prints values on the same line. The outer loop typically calls System.out.println() to move to the next line. This row-column print pattern creates grids, triangles, and rectangular patterns.
Tanner Crow — AP CS Teacher & Tutor
11+ years teaching AP Computer Science at Blue Valley North High School (Overland Park, KS). Verified Wyzant tutor with 1,845+ hours, 451+ five-star reviews, and a 5.0 rating. His AP CSA students score 5s at more than double the national rate.
- 54.5% of students score 5 on AP CSA (national avg: 25.5%)
- 1,845+ verified tutoring hours • 5.0 rating
- Free 1-on-1 tutoring inquiry: Wyzant Profile
🔗 Related Topics
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]