Introduction
Welcome to the FiveHive article for topic 2.11 of AP CSA!
In this article, we will briefly cover the learning objective of 2.11 as shown in the AP® Computer Science A Course and Exam Description 2025.
2.11.A - Develop code to represent nested iterative processes and determine the result of these processes.
This article… kind of breaks the cycle somewhat? You’re TECHNICALLY learning something new, but it is more or less stringing together what you already know.
Specifically, we’re learning about nested iteration! In this article, I’m going to introduce this concept quickly, then you’re gonna do some practice on what the output of nested iteration looks like (with my awesome guidance of course). Then, you’re gonna write a little bit of a hard nested iteration program on your own (there is also a cute bonus)!
Without further ado, let’s jump straight into it! Get excited!
Nested What Now?
Up to this point, we’ve only explored algorithms that utilize one for loop that iterates and that’s simply it. We’ve taken a look at what happens if we put if statements within one another… but what happens when we do the same for loops?
So something like this:
LOOP 1 → Iterates so long as some condition is true.
LOOP 2 → Nested in the first loop.
It’s very simple! Remember how with loops GENERALLY they iterate until some condition is false? And on each iteration they do a series of tasks?
Here, when we embed another loop (let’s call it other) in this harmony, other will start iterating! And it will keep iterating, until its specific condition is false, whether it be a for/while loop! Only then when other stops iterating, the outer loop will finally iterate.
That’s all it is! Now, I think you’re better off spending your time doing practice, so let’s do just that!
Guided Practice
Output Question (OQ) I: What is the output of the following program?
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 5; j++)
{
System.out.print(“#”);
}
System.out.println();
}
Answer on your own before you see my solution! Trust me, it’ll benefit you a whole lot more!
Ok, let’s do this.
So looking at the outer for loop, we see that i is set to 0, and each iteration it will increment by one.
It’ll then keep looping until i equals 7. In other words, the outer for loop will iterate 7 times.
Given that, it will repeat its contents 7 times. What are its contents? We have ANOTHER loop (that’s the nested loop) and a print statement.
That inner loop sets j to 0 and increments by one each iteration, and will continue doing that until j equals 5. In other words, the inner loop iterates 5 times.
Then after the inner loop does its 5 iterations, an empty print statement executes. Well, what’s in the inner loop?
A statement to print “#” each iteration.
Putting all that together: since we know the inner loop must complete its iterations before the outer loop does its thing, this program will print “#” 5 times and print a new line, and then print another 5 “#”, a new line, and then it will repeat this process 5 more times.
The output will then be a 5x7 rectangle! Like so:
#####
#####
#####
#####
#####
#####
#####
Let’s try another one.
OQ II: What is the output of the following program?
for (int i = 0; i < 9; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(“#”);
}
System.out.println();
}
Solve it on your own!!!
Ok, so this program is very similar to the last… all that we changed is now the outer loop iterates 9 times, and the inner loop still prints “#”, but the number of times it iterates will be equal to the value of i.
So on the first iteration when i is 0, since j is set to 0 and it is less than or equal to i (which is 0), “#” prints ONCE. Then a new line is printed.
Then on the second iteration, i is 1, and thus the inner loop will execute twice, printing two hashtags on the second line.
Fast forward to when i is 8, and the inner loop will execute 9 times, printing 9 hashtags on the 9th line. Thus, the output will look like this:
#
##
###
####
#####
######
#######
########
#########
Bonus Question! (you don’t have to do this, but it’ll help with the practice question)
How do you make the staircase printed above left-aligned like this?

Solve this on your own!!!!
It’s simple!
Conceptually, to right align the staircase, you have to on each line space the hashtag(s) a number of times that is equal to ten minus the row number.
Let’s use dots to visualize this for now.
So this first hashtag:
#
Becomes:

Then for the second:

And fast forward and it will look like this:

To implement that in code, you can simply use a for loop, setting the LCV to 9, decrementing 1 each iteration, and stopping when it is equal to the LCV of the outer loop, and in the loop simply printing “.”. This way for the first row, 9 dots print, then for the second row 8 dots print, and so on.
So something like this:
for (int j = 9; j > i; j--)
{
System.out.print(".");
}
Want to remove the dots? Easy!
for (int j = 9; j > i; j--)
{
System.out.print(" ");
}

Practice
We made a right-aligned staircase and a left-aligned one. How about we combine the two?! Write a program to print this:

Practice Canonical Solution
for (int i = 0; i < 9; i++)
{
for (int j = 9; j > i; j--)
{
System.out.print(" ");
}
for (int j = 0; j <= i; j++)
{
System.out.print("#");
}
System.out.print(" ");
for (int j = 0; j <= i; j++)
{
System.out.print("#");
}
System.out.println();
}
Practice (Bonus)

Write a program that makes the pyramid above!
Hint: Modify the right stair program VERY slightly…
Practice (Bonus) Canonical Solution
for (int i = 0; i < 9; i++)
{
for (int j = 9; j > i; j--)
{
System.out.print(" ");
}
for (int j = 0; j <= i; j++)
{
System.out.print("# ");
}
System.out.println();
}
See? Very minor change!
