While loops are an important part of AP Computer Science A Unit 2: Selection and Iteration. Topic 2.7 focuses on using while loops to repeat instructions while a Boolean condition remains true. You should be able to trace loop control variables, evaluate conditions accurately, determine how many times a loop executes, and identify potential infinite loops.
In this interactive practice set, you will work through 10 multiple-choice questions covering Topic 2.7: while Loops. The questions emphasize code tracing, loop conditions, control-variable updates, integer arithmetic, and common off-by-one situations.
Key Concepts to Remember Before You Start:
-
Pre-Test Evaluation:
The Boolean condition in a
whileloop is evaluated before each iteration. If it evaluates tofalseinitially, the loop body never runs. - Loop Control Variable Updates: The loop control variable should be updated appropriately so that the condition can eventually become false.
- Infinite Loop Traps: Failing to update a loop control variable can cause a loop to continue indefinitely.
-
Off-by-One Errors:
Pay close attention to strict inequalities such as
<and>versus inclusive inequalities such as<=and>=.
Work through the interactive quiz below. After each question, review the explanation to strengthen your code-tracing and loop-analysis skills.
AP CSA 2.7: While Loops Practice
int sum = 0;
while (count <= 4) {
sum += count;
count++;
}
System.out.println(sum);
count starts at 1. The loop adds 1, 2, 3, and 4 to
sum, giving 10. After the fourth iteration,
count becomes 5, so 5 <= 4 is false and
the loop terminates.
while (x > 0) {
System.out.print(x + ” “);
x -= 2;
}
The loop prints 10, 8, 6, 4, and 2. After printing 2,
x becomes 0. The next condition check is
0 > 0, which is false, so 0 is not printed.
while (num < 10) {
System.out.print(num + ” “);
num += 3;
}
num is 3, then 6, then 9. After printing 9,
num becomes 12. The next condition,
12 < 10, is false, so the loop stops.
int i = 1;
while (i <= 5) {
product *= i;
}
i remains 1 because it is never updated inside the loop.
Therefore, 1 <= 5 remains true and the loop does not
terminate.
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
System.out.println(sum);
The algorithm adds the digits of 345.
First, 345 % 10 = 5.
Then 34 % 10 = 4.
Finally 3 % 10 = 3.
Therefore, sum = 5 + 4 + 3 = 12.
while (val < 30) {
val *= 2;
}
System.out.println(val);
The values of val are 2, 4, 8, 16, and 32.
When val becomes 32, the condition
32 < 30 is false, so the loop terminates and
32 is printed.
while (k > 5) {
System.out.print(k + ” “);
k–;
}
System.out.println(“Done”);
The condition is checked before the loop body executes.
Since k = 5, the condition 5 > 5
is false immediately. Therefore, the loop body is skipped and
only Done is printed.
int b = 20;
while (a < b) {
a += 3;
b -= 2;
}
System.out.println(a + ” ” + b);
Trace the values after each iteration:
- Start:
a = 1,b = 20 - Iteration 1:
a = 4,b = 18 - Iteration 2:
a = 7,b = 16 - Iteration 3:
a = 10,b = 14 - Iteration 4:
a = 13,b = 12
The next condition check is 13 < 12, which is false.
Therefore, the loop terminates and prints 13 12.
int count = 0;
while (n > 1) {
n /= 2;
count++;
}
System.out.println(count);
Java integer division truncates the fractional part:
- Start:
n = 12→n = 6, count = 1 n = 6→n = 3, count = 2n = 3→n = 1, count = 3
The next condition is 1 > 1, which is false.
Therefore, the loop terminates and prints 3.
while loops in Java is FALSE?
Variables declared inside a block, such as a
while loop body, have block scope.
They cannot be accessed outside that block.
Unit 2.7 While Loops Practice Complete!
Why Mental Trace Tables Matter for While Loops
In AP CSA multiple-choice questions, you may need to determine what a
program does without executing it. Questions involving
while loops often require you to maintain the values of
loop control variables and accumulators across multiple iterations.
A quick trace table can help you track those values and avoid
off-by-one errors.
Continue Your AP CSA Practice
Once you are comfortable tracing while loops, continue with
Topic 2.8: For Loops Practice
,
where you will practice initialization, condition checks, update
expressions, and loop tracing.