Reference Guide

Key Ideas

  • A for loop 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 for when the number of repetitions is known. Every for loop can be rewritten as a while loop.
  • Common headers: count up i = 0; i < n; i++, count down i = n; i > 0; i--, step by 2 i += 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 for version 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 exactly n times. Changing < to <= runs it n + 1 times.
  • Use for when the number of passes is known in advance, and while when 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.

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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.

for Loop Trace
Watch how often each part of the header runs.

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?

Step by Three
Predict, then count.

Activity 3: Predict the Output

for Loop Questions
Three questions about headers, scope, and equivalence.
Question 1

What does this print?

for (int i = 3; i > 0; i--) {
System.out.print(i + " ");
}
Question 2

Why does this code fail to compile?

for (int i = 0; i < 4; i++) {
System.out.println(i);
}
System.out.println("Last i: " + i);
Question 3

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.

Rewrite as a while Loop
Same three parts, different places.
int i = 2;
while (i <= 10) {
System.out.println(i);
i += 2;
}
System.out.println("Done");

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.

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack

Two short tasks.

  1. Finish the runner: compute n! (n factorial) with a for loop. 5! is 5 * 4 * 3 * 2 * 1 = 120.
  2. Answer the quick check.

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
Homework Quick Check
Type a number.

How many times does the body run?

for (int i = 10; i <= 50; i += 10) {
System.out.println(i);
}

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

All Java lessons