1. LxD Cycle Process

Empathize: Students often confuse an array’s length with its last valid index. In a leaderboard, that mistake can skip a player or cause an ArrayIndexOutOfBoundsException.

Define:

  • POV: CSA students need to connect array length, valid indices, and loop conditions because every complete traversal visits indices from 0 through length - 1.
  • Learning Goal: Students will traverse a one-dimensional array with standard for, while, and enhanced for loops.

Ideate:

  • HMW Question: How might we help students visualize a leaderboard’s length and last valid rank?
  • HMW Question: How might we show when an index-based loop is necessary and when an enhanced for loop is clearer?
  • Activity: Print leaderboard scores, demonstrate the <= error, and use an enhanced for loop to display values.

Prototype & Test: A peer ran the leaderboard and homework runners. The first draft hid the array declaration and used solved homework, so I made each runner a complete Java class and left the homework condition visibly incorrect for students to repair.


2. Lesson Plan

Learning Objective: Use standard for, while, and enhanced for loops to access every element in a one-dimensional array.

Success Criteria: You can traverse an entire array, explain why valid indices range from 0 to length - 1, repair a <= bounds error, choose an appropriate loop, and explain why an enhanced for loop cannot modify array elements by changing its loop variable.

Tech Talk (5 minutes)

An array is a numbered row of values that all have the same type. Each value is stored at an index, and indexes start at 0, so the first score is scores[0]. A traversal means visiting every element in order, usually with a loop. If an array has length 4, it has four elements at indexes 0, 1, 2, and 3; therefore, the last valid index is length - 1. Asking for index 4 goes past the end of the array and causes an ArrayIndexOutOfBoundsException.

Our running scenario is a game leaderboard:

Code Runner Challenge

Run it, then change a score and predict the updated leaderboard

View IPYNB Source
int[] scores = {982, 915, 877, 844};
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

A complete index-based traversal uses <:

Code Runner Challenge

Compare an index-based while loop with an enhanced for loop

View IPYNB Source
for (int rank = 0; rank < scores.length; rank++) {
    System.out.println(scores[rank]);
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

A while loop can traverse the same leaderboard when you control the index yourself. An enhanced for loop is useful when you only need each value and not its index. Index 0 is displayed as Rank 1 because people count from 1, while arrays count from 0.

The loop condition must be rank < scores.length, not rank <= scores.length, because scores.length is one position past the final valid index.

From the College Board

AP CSA Unit 4, Topic 4.4 focuses on traversing arrays with iteration. A traversal visits array elements in sequence using a loop, and the loop must remain within the array’s valid index range.


3. Reference Guide

Key Vocabulary

Term Definition Example
Array Fixed-size collection of values with the same type. int[] scores = {980, 900};
Index Position of an element in an array. scores[0]
Length Number of elements in an array. scores.length
Traversal Visiting every element in an array. A loop from 0 to length - 1
Bounds error Attempting to access an invalid index. scores[scores.length]
Enhanced for loop Loop that visits each element directly. for (int score : scores)

Choosing a Loop

  • Need the rank or index? Use a standard for loop.
  • Need to update elements? Use a standard for loop.
  • Need manual condition control? Use a while loop.
  • Only need each score? Use an enhanced for loop.

4. Code Examples

A. Standard for Loop Traversal

This runner uses a standard for loop to print every leaderboard score exactly once.

Code Runner Challenge

Compare the safe < condition with the failing <= condition

View IPYNB Source
// CODE_RUNNER: Run it, then change a score and predict the updated leaderboard
public class LeaderboardTraversal {
    public static void main(String[] args) {
        int[] scores = {982, 915, 877, 844};

        for (int rank = 0; rank < scores.length; rank++) {
            System.out.println("Rank " + (rank + 1) + ": " + scores[rank]);
        }
    }
}

LeaderboardTraversal.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

B. while Traversal and Enhanced for

This runner uses a while loop for ranked scores, then uses an enhanced for loop to print player names without indexes.

Code Runner Challenge

Observe the unchanged array, then the updated array

View IPYNB Source
// CODE_RUNNER: Compare an index-based while loop with an enhanced for loop
public class LeaderboardLoopChoices {
    public static void main(String[] args) {
        int[] scores = {982, 915, 877, 844};
        String[] playerNames = {"Avery", "Blair", "Casey", "Drew"};

        System.out.println("While loop with ranks:");
        int rank = 0;
        while (rank < scores.length) {
            System.out.println("Rank " + (rank + 1) + ": " + scores[rank]);
            rank++;
        }

        System.out.println("Enhanced for loop with player names:");
        for (String playerName : playerNames) {
            System.out.println(playerName);
        }
    }
}

LeaderboardLoopChoices.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

C. Compare < and <=

This runner completes one traversal with <, then catches the <= version and prints Java’s real bounds error.

Code Runner Challenge

Replace the sample scores and run your leaderboard

View IPYNB Source
// CODE_RUNNER: Compare the safe < condition with the failing <= condition
public class LeaderboardBoundsComparison {
    public static void main(String[] args) {
        int[] scores = {982, 915, 877, 844};

        System.out.println("Using <:");
        for (int rank = 0; rank < scores.length; rank++) {
            System.out.println("Rank " + (rank + 1) + ": " + scores[rank]);
        }

        System.out.println("Using <=:");
        try {
            for (int rank = 0; rank <= scores.length; rank++) {
                System.out.println("Rank " + (rank + 1) + ": " + scores[rank]);
            }
        } catch (ArrayIndexOutOfBoundsException error) {
            System.out.println("Java error: " + error.getMessage());
        }
    }
}

LeaderboardBoundsComparison.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

D. Enhanced for Loop Limitation

This runner shows that changing the enhanced loop variable does not change the array, so an index-based for loop is needed to update elements.

Code Runner Challenge

Repair <= to <, then add a seventh score without changing the loop condition

View IPYNB Source
// CODE_RUNNER: Observe the unchanged array, then the updated array
public class LeaderboardUpdateChoices {
    public static void main(String[] args) {
        int[] scores = {700, 800, 900};

        System.out.println("After changing the enhanced loop variable:");
        for (int score : scores) {
            score += 50;
        }
        for (int score : scores) {
            System.out.println(score);
        }

        System.out.println("After changing elements with an index-based loop:");
        for (int index = 0; index < scores.length; index++) {
            scores[index] += 50;
        }
        for (int score : scores) {
            System.out.println(score);
        }
    }
}

LeaderboardUpdateChoices.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 a raw frontmatter cell.
  3. Add a markdown cell explaining the valid index range, your loop choice, and why an enhanced for loop does not modify array elements when its loop variable changes.
  4. Add runnable Java cells for the Popcorn Hack and Homework Hack.
  5. Run each cell and leave the output visible.
  6. End every Java cell with ClassName.main(null);.

Popcorn Hack (In-Class)

Create a six-player leaderboard. Print every player’s rank and score with a standard for loop. Then use an enhanced for loop to print the score values without ranks. Replace the sample scores with your own.

// CODE_RUNNER: Replace the sample scores and run your leaderboard
public class LeaderboardPopcorn {
    public static void main(String[] args) {
        int[] scores = {760, 845, 912, 688, 934, 801};

        System.out.println("Ranked players:");
        for (int rank = 0; rank < scores.length; rank++) {
            System.out.println("Rank " + (rank + 1) + ": " + scores[rank]);
        }

        System.out.println("Score values:");
        for (int score : scores) {
            System.out.println(score);
        }
    }
}

LeaderboardPopcorn.main(null);

Homework Hack

The starter runner has one intentional bug: its loop uses <=. Run it to see the caught bounds message, then change only the condition to <. In your markdown response, explain why an array of length 5 has valid indices 0 through 4, choose the appropriate loop for the traversal, and explain why < works.

Second task: Add a seventh score to the array without changing the loop condition, and confirm every rank still prints.

// CODE_RUNNER: Repair <= to <, then add a seventh score without changing the loop condition
public class LeaderboardHomework {
    public static void main(String[] args) {
        int[] scores = {710, 865, 903, 947, 981};

        try {
            for (int rank = 0; rank <= scores.length; rank++) {
                System.out.println("Rank " + (rank + 1) + ": " + scores[rank]);
            }
        } catch (ArrayIndexOutOfBoundsException error) {
            System.out.println("Repair needed: " + error.getMessage());
        }
    }
}

LeaderboardHomework.main(null);

6. Grading Plan (1 Point Total)

Part Points What earns the points
Popcorn 0.2 Complete leaderboard traversal and enhanced for loop run correctly.
Homework traversal 0.25 Student changes <= to < and visits every valid index.
Seventh score 0.2 Student adds a seventh score without changing the loop condition and prints every rank.
Homework explanation 0.25 Markdown explains 0 through length - 1, why < works, chooses an appropriate loop, and explains the enhanced for limitation.
Code quality 0.1 Cells are runnable, print visible output, and end with ClassName.main(null);.
Total 1.0  

Quick Validation Checklist

  • Every runner is a complete Java class with main.
  • Every runner declares its own variables and prints visible output.
  • The bounds comparison shows both < and <= behavior.
  • Every catch block prints error.getMessage().
  • The enhanced for limitation is demonstrated.
  • The homework starter uses <=; the student changes it to <.
  • A seventh score is added without changing the loop condition.
  • A markdown explanation includes the valid range, loop choice, and enhanced for limitation.

7. Lesson Revisions & Feedback Evidence

Feedback Received: A peer noted that the first draft had solved homework and examples without complete runner classes, making the prompts and executions difficult to follow.

Revision Made: The lesson now uses a leaderboard scenario, gives every runner its own Java class and main method, includes an observable bounds-error runner, and leaves the homework repair for the student.


References

College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025].

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.