2.08 for Loops
Use the three part for loop header, convert between for and while, and count iterations exactly.
- Reference Guide
- College Board Requirements
- Lesson Plan
- Code Examples
- A. Simple: for Loops in Code
- Code Runner Challenge
- Hacks & Practice Tasks
- Activity 1: Trace the Header
- Activity 2: Count the Iterations
- Activity 3: Predict the Output
- Activity 4: Convert for to while
- Popcorn Hack
- Code Runner Challenge
- Homework Hack
- Code Runner Challenge
- References
Reference Guide
Key Ideas
- A
forloop packs the three loop control steps into one header:for (initialize; condition; update). - Order of execution: initialize once, then check the condition, run the body, run the update, check again.
- A variable declared in the header (like
int i) exists only inside the loop. Using it after the loop is a compile error. - Use
forwhen the number of repetitions is known. Everyforloop can be rewritten as awhileloop. - Common headers: count up
i = 0; i < n; i++, count downi = n; i > 0; i--, step by 2i += 2.
for and while Say the Same Thing
| for loop | equivalent while loop |
|---|---|
for (int i = 0; i < 5; i++) {System.out.println(i);} |
int i = 0;while (i < 5) {System.out.println(i);i++;} |
- Both print 0 1 2 3 4. The
forversion keeps initialize, test, and update together so they are hard to forget.
College Board Requirements
AP CSA Unit 2, Topic 2.8: for Loops.
This lesson is built directly against the College Board’s stated learning objective for this topic:
Represent iterative processes using a for loop.
Topic 2.8, for Loops, is required content on the AP Computer Science A Exam. The learning objective quoted above is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
Lesson Plan
Learning Objective: Represent iterative processes using a for loop.
Success Criteria: You can read the three parts of a for header, count iterations exactly from the header alone, and convert between a for loop and an equivalent while loop.
Tech Talk & Introduction
A for loop packs initialise, test and update into one header line, which keeps the three jobs of a loop control variable together where you can see them.
- The header is
for (initialise; test; update). - The initialiser runs once. The test runs before every pass. The update runs after every pass.
for (int i = 0; i < n; i++)runs exactlyntimes. Changing<to<=runs itn + 1times.- Use
forwhen the number of passes is known in advance, andwhilewhen it is not.
Code Examples
A. Simple: for Loops in Code
Run it. Then change the header to i = 1; i <= 10; i++ and predict the two totals before running again.
for loop demo: accumulate sums and count down by 25.
public class SumLoops {
public static void main(String[] args) {
int sumEven = 0;
int sumAll = 0;
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
sumEven += i;
}
sumAll += i;
}
System.out.println("Sum of evens: " + sumEven);
System.out.println("Sum of all: " + sumAll);
// Counting down with a step of 25
for (int p = 100; p >= 0; p -= 25) {
System.out.print(p + "% ");
}
System.out.println();
}
}
Code Runner Challenge
for loop demo: accumulate sums and count down by 25.
View IPYNB Source
// CODE_RUNNER: for loop demo: accumulate sums and count down by 25.
public class SumLoops {
public static void main(String[] args) {
int sumEven = 0;
int sumAll = 0;
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
sumEven += i;
}
sumAll += i;
}
System.out.println("Sum of evens: " + sumEven);
System.out.println("Sum of all: " + sumAll);
// Counting down with a step of 25
for (int p = 100; p >= 0; p -= 25) {
System.out.print(p + "% ");
}
System.out.println();
}
}
SumLoops.main(null);
Hacks & Practice Tasks
Activity 1: Trace the Header
The header line is highlighted three different ways: once for initialize, once per condition check, and once per update. Read the note each step.
Activity 2: Count the Iterations
Do not run it in your head one step at a time. Use the pattern: how many values does k take before the condition fails?
Activity 3: Predict the Output
What does this print?
Why does this code fail to compile?
Which while loop is equivalent to for (int i = 10; i >= 0; i -= 5) { System.out.println(i); }?
Activity 4: Convert for to while
Arrange the lines into the while loop that behaves exactly like for (int i = 2; i <= 10; i += 2) and prints the even numbers 2 through 10.
Popcorn Hack
Print the multiples of 5 from 5 to 50 on one line, then print how many numbers were printed. Use one for loop and no while.
Popcorn hack: multiples of 5 with one for loop. Expected count: 10.
public class Multiples {
public static void main(String[] args) {
int printed = 0;
// TODO: for loop that prints 5 10 15 ... 50 on one line
// and increments printed each time
System.out.println();
System.out.println("Printed " + printed + " numbers"); // expected: 10
}
}
Code Runner Challenge
Popcorn hack: multiples of 5 with one for loop. Expected count: 10.
View IPYNB Source
// CODE_RUNNER: Popcorn hack: multiples of 5 with one for loop. Expected count: 10.
public class Multiples {
public static void main(String[] args) {
int printed = 0;
// TODO: for loop that prints 5 10 15 ... 50 on one line
// and increments printed each time
System.out.println();
System.out.println("Printed " + printed + " numbers"); // expected: 10
}
}
Multiples.main(null);
Homework Hack
Two short tasks.
- Finish the runner: compute
n!(n factorial) with aforloop.5!is5 * 4 * 3 * 2 * 1 = 120. - Answer the quick check.
Homework: factorial with a for loop. Expected: 5! = 120.
public class Factorial {
public static void main(String[] args) {
int n = 5; // test with 5 (120), 1 (1), and 7 (5040)
int result = 1;
// TODO: for loop from 1 to n that multiplies result by each value
System.out.println(n + "! = " + result);
}
}
Code Runner Challenge
Homework: factorial with a for loop. Expected: 5! = 120.
View IPYNB Source
// CODE_RUNNER: Homework: factorial with a for loop. Expected: 5! = 120.
public class Factorial {
public static void main(String[] args) {
int n = 5; // test with 5 (120), 1 (1), and 7 (5040)
int result = 1;
// TODO: for loop from 1 to n that multiplies result by each value
System.out.println(n + "! = " + result);
}
}
Factorial.main(null);
How many times does the body run?
References
College Board Course and Exam Description
Topic 2.8, for Loops, is required content on the AP Computer Science A Exam. The learning objective quoted in section 3 (College Board Requirements) is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
Outside Academic Reference
The specification defines the order of the three header parts precisely: the initialiser runs once, the condition is evaluated before each iteration, and the update runs after the body, which is why the iteration count follows directly from the header.
Reference List
College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/courses/ap-computer-science-a
Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java language specification: Java SE 21 edition. Oracle America. https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.14