Introduction
Welcome to the FiveHive article for topic 2.8 of AP CSA!
In this article, we will cover the learning objective of 2.8 as shown in the AP® Computer Science A Course and Exam Description 2025.
2.8.A - Develop code to represent iterative processes using for loops and determine the result of these processes
In this (pretty short) article, we’re going to continue the iteration arc by introducing the second kind of loop in Java: for loops!
This is the last piece of content in the entire unit; the rest of the unit is essentially applying all of what we learned to do cool things like nested iteration. So get excited!*
Anyways, let’s get to it!
Anyone that picked up on the reference is a man (or woman) of culture
for Loops
These loops are also quite simple, but contain a little more to them than your average while loop.
Here is the syntax:
for (int i = 0; i < 5; i++)
{
[DO SOMETHING]
}
Of course, it’s an iterative statement. But unlike while loops, their headers are made up of three components, each of which are separated by semicolons as you can see above:
- Initialization (int i = 0)
- Boolean Expression (i < 5)
- Incrementor/The Update (i++)
Let’s break it down!
Initialization
for (int i = 0; i < 5; i++)
{
[DO SOMETHING]
}
Initialization refers to the variable being initialized in the for loop header. In our example above, the variable i is being initialized as an integer with a value of 0.
This variable has a fancy name: Loop Control Variable (LCV). It executes only ONCE prior to the first boolean expression evaluation.
You know how in our while loops we often initialize a counter variable before the while loop that would get compared to some value before each iteration, and in each iteration we incremented/decremented that counter in some way to control how many times our while loop looped?
Think of the LCV as our counter variable!
Boolean Expression
for (int i = 0; i < 5; i++)
{
[DO SOMETHING]
}
Right after initializing our LCV, the second part of our for loop header is evaluated, which is a boolean expression.
Typically, this boolean expression, like a while loop, compares the LCV (aka the counter variable) to some value.
Like a while loop, before even iterating, we check the boolean expression.
If it’s true, we start looping.
And afterwards it executes after each iteration, and the loop stops looping once it finally evaluates to false.
Otherwise (false), the loop never runs.
Incrementor/The Update
for (int i = 0; i < 5; i++)
{
[DO SOMETHING]
}
This statement is executed after a full iteration is completed and before the boolean expression is checked again. Typically, it updates the LCV we set at the beginning, which is then compared to some value as we said earlier in the boolean expression.
And there you have it! That’s the structure of a for loop. Simple, easy, awesome.
Quick Practice: What is the output of this program? Try it out yourself and then we’ll walk through it!
for (int i = 5; i < 9; i++)
{
i++;
System.out.println(“Everything is awesome!”);
}
Yes, I made it a little strange, but it’s not too bad you’ll see!
-
First, we initialize our bad boy LCV.
- Here, we initialize our LCV to be int variable i with a value of 5.
-
Then, we check our boolean expression.
- Is 5 less than 9? Yup!
- REMEMBER! We don’t increment our LCV just yet! We wait until A FULL ITERATION has been completed!
-
Then, we enter the loop body:
-
First, we increment i → THIS IS NOT OUR LOOP HEADER DOING THIS, THIS IS THE LOOP'S BODY DOING IT!
- 5 + 1 = 6
- Then we print "Everything is awesome!"
- That's the first iteration.
-
First, we increment i → THIS IS NOT OUR LOOP HEADER DOING THIS, THIS IS THE LOOP'S BODY DOING IT!
-
Finally, we increment i:
- 6 + 1 = 7 → Now this is our loop header’s work.
-
And we repeat:
- 7 < 9 → Yup
- 7 + 1 = 8 → Again, loop body, not header.
- "Everything is awesome!"
- 8 + 1 = 9 → Loop header.
- Now, we check our boolean expression, 9 is not less than 9, so the loop terminates.
- All in all, the program simply prints “Everything is awesome!” twice.
- NOTE: Without that i++ in our loop body, “Everything is awesome!” would instead be printed 4 times.
Practice
Recall 2.8’s practice problem:
Write a method that takes a large int number and prints out each of its digits, starting from the last digit…
You did that in a while loop. Fun fact, you can write for loops as while loops and vice versa!
So now instead of a while loop, do it as a for loop! Simple!
Practice Canonical Solution
public void printNumbers(int num)
{
for(int i = num; i > 0; i /= 10)
{
System.out.println(i % 10);
}
}
