1. LxD Cycle Process

Empathize: Students can write a for header by copying a pattern but cannot say how many passes it makes, so an off by one error is invisible to them until output is wrong.

Define:

  • POV: CSA students need to count iterations from the header alone, because the AP exam asks for exact counts far more often than it asks for new loops.
  • Learning Goal: Students will trace the header, count iterations exactly, predict output, and convert a for loop to the equivalent while loop.

Ideate:

  • HMW Question: How might we get students to count passes from the header, rather than running the loop to find out?
  • Activity: A header tracer showing when each part runs, an iteration counter, an output prediction, and a for-to-while conversion.

Prototype & Test: Activity 4 asks for the equivalent while loop, which forces the three header parts to be separated and placed correctly.


2. 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 (5 minutes)

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.

3. 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.

4. 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 ...

5. Hacks & Practice Tasks

Prepare your submission IPYNB

  1. Create a new notebook in your portfolio homework area: _notebooks/homework.
  2. Add one markdown cell at the top with the frontmatter:
---
layout: post
title: CSA Unit 2.08 for Loops HW
categories: [Java, For-Loops]
lesson_language: Java
lesson_topic: For-Loops HW
lesson_source: APCSA
lesson_type: lesson
permalink: /csa/unit_02/2_8_hw
author: yourGithubID
---
  1. Add code cells for the Popcorn Hack and the Homework Hack. Ensure all code runs and output is visible.
  2. Include a markdown cell before each code section explaining the concept.

Submission Safety Rules (Read First)

[IMPORTANT] To avoid grading errors, follow these rules exactly:

  • Write your prediction before you run each code section
  • Execute every code cell and leave the output visible
  • Show your work for any hand-traced prediction
  • Do not just copy code; explain what each line does
  • Label each question clearly

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 (In-Class)

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);
}

6. Grading Plan (1 Point Total)

Classroom Rubric

  • 0.2 points: Popcorn completion Student printed the multiples of 5 from 5 to 50 on one line using a single for loop, then printed the count.

  • 0.8 points: Homework completion

    • 0.5 Factorial: A for loop computes n! correctly, with 5! printing 120.
    • 0.2 Quick check: The iteration count question is answered from the header without running the loop.
    • 0.1 Notebook quality: Every code cell executed with output visible, and a markdown cell before each code section explaining the concept.

Quick Validation Checklist

  • Present: one for loop and no while in the Popcorn answer
  • Present: factorial verified for at least two values of n
  • Absent: an off by one caused by <= where < was intended
  • Test: 5! prints 120 and 0! prints 1

7. Lesson Revisions & Feedback Evidence

Feedback Received: Team review of the Unit 2 set found that these lessons did not follow the section format used by the Unit 1 lessons, and that they ended without the assignment submission form every other CSA lesson provides.

Revision Made: The lesson was reorganised into the shared CSA lesson format, so it now opens with the LxD cycle and lesson plan, states its reference material before the code, and groups the activities, Popcorn Hack and Homework Hack under one practice section. The duplicate lesson heading was removed, and the page now renders the standard Submit Assignment form at the end.

Design Decision Kept: Activity 4 asks for the equivalent while loop, which forces the three header parts to be separated and placed correctly.

Open Item: Record peer feedback from the team teach delivery here once the lesson has been taught.


8. Summary

  • for (init; condition; update): init once, condition before every pass, update after every pass.
  • Header variables are only visible inside the loop.
  • for when the count is known, while when it is not; the two are interchangeable.
  • Count iterations by listing the values the loop variable takes.
  • Next lesson: using if inside loops to build the standard algorithms.

9. References

College Board Course and Exam Description

Topic 2.8, for Loops, is required content on the AP Computer Science A Exam. The objective quoted in section 2 of this lesson 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

Submit Assignment

Click to upload or drag and drop
PDF, ZIP, images, documents, or Jupyter notebooks (.ipynb) (Max 10MB per file)

Need to update a submission later? Open the submissions dashboard.