Introduction
Welcome to the FiveHive article for topic 2.7 of AP CSA!
In this article, we will cover the learning objectives of 2.7 as shown in the AP® Computer Science A Course and Exam Description 2025
-
2.7.A - Identify when an iterative process is required to achieve a desired result.
- Important for FRQS!
-
2.7.B - Develop code to represent iterative processes using while loops and determine the result of these processes.
- Important for MCQs and FRQs
In this (pretty short) article, we’re going to conclude the selection arc and transition to the iteration arc, the second key component of algorithms! We’re gonna introduce the basics of iteration, infinite loops, off-by-one errors, and look specifically at while loops.
In the next article, we’re gonna continue the iteration arc by introducing for loops.
Anyways, let’s get to it!
Iteration
Iteration is more or less just repetition, but if you want the College Board jargon: it is a form of repetition (????????????????). Eh, it’s basically just repetition.
I guess College Board says it like that because they define iteration as the following:
Iteration statements (for and while loops) essentially change the flow of the program. Specifically, iteration statements take some amount of code and repeat it some number of times (0-n).
Quick FAQ
How do they know when to stop?
Well, iteration statements are coupled with boolean expressions, and so long as that bad boy is true, that code will just keep repeating and repeating. When it becomes false, it simply stops.
What if the boolean expression never evaluates to false?
Well, my friend, that’s what is referred to as an infinite loop, in which the iteration statement keeps going and executing its content without ever stopping, leading for the program to crash and die.
What if the boolean expression of the iteration statement is false before the loop begins?
The loop just never executes.
What is it called when our loop is a bad boy and executes one too many/too few times? That’s called an Off-by-one error*.
*Yes, this is actually in the CED. I have no idea why, in all honesty you probably won’t see it ever used on the MCQs let alone the FRQs.
while Loops
This is quite a basic loop in my opinion. The syntax looks like the following:
When the program encounters a while loop, before actually starting to loop it’ll do an initial check of its boolean expression (condition). If it evaluates to:
True? We start looping. After each iteration, the boolean expression is checked again.
If it's true, the loop continues. Otherwise if it's false, it ceases.
False? Simple. No looping for you buddy.
Let’s do a simple example, then have you write your own algorithm using a while loop!
Quick Example
What is the output of the following program? Try it on your own then we’ll walk through it.
int counter = 0;
while (counter < 10)
{
System.out.println(“I love AP CS A”);
}
counter++;Let’s walk through it!
- We start by defining an integer variable called counter and setting it to 0.
-
Then we encounter our new friend: the while loop.
- Its condition in the parentheses: counter < 10
-
In the brackets of the loop is the programming statement
-
System.out.println("I love AP CS A")
-
-
To put the loop into words:
- While the value of the counter variable is less than 10, print "I love AP CS A" to the console.
-
We check the condition: is counter less than 10?
- Yes, because counter 0, and 0 is less than 3 (sometimes).
- We then enter the loop and print "I love AP CS A" once to the console
-
2nd iteration, check the condition: is counter less than 10?
- Yes, counter is... still 0
- We then enter the loop and print "I love AP CS A" once again to the console
- You get to 4th, 5th, 6th, and 20th time, counter is still 0, and we print “I love AP CS A” again and again…
- Aha! This is an infinite loop… notice how counter++, the thing that is supposed to update counter, is outside our loop and this never runs! Thus, our loop never terminates, and we get an infinite console filled with lies*.
*That was a joke.
How do we fix this???
Simple. Just move the counter into the loop like so:
int counter = 0;
while (counter < 10)
{
System.out.println(“I love AP CS A”);
counter++;
}That way, the counter actually reaches 10 and the loop stops iterating, and the program properly prints “I love AP CS A” 10 times.
Practice
Write a method that takes a large int number and prints out each of its digits, starting from the last digit, using a while loop.
For example, if I input the number “71563” to either method, both must print the following:
Practice Canonical Solution
public void printNumbers(int num)
{
while(num > 0)
{
System.out.println(num % 10);
num /= 10;
}
}
