# AP® Computer Science A 2.8 for Loops

Source: https://lockincs.com/ap-csa/unit-2/2-8-for-loops/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 2.8.A, 2.8.A.1, 2.8.A.2, 2.8.A.3

A for loop is an iterative statement whose header has three parts: the initialization, the Boolean expression and the update. The initialization runs once and sets up the loop control variable; the Boolean expression is checked before every iteration; the update runs after the whole body and before the next check. Any for loop can be rewritten as an equivalent while loop, and the reverse.

## What you will learn

- 2.8.A Develop code to represent iterative processes using for loops and determine the result of these processes.

## 1\. The three parts of the header

A `for` loop is a type of iterative statement. Its header has three parts, separated by semicolons: the initialization, the Boolean expression and the update 2.8.A.1.

```
for (int lap = 1; lap <= 4; lap++) {
    System.out.println("Lap " + lap);
}
```

Output:

```
Lap 1
Lap 2
Lap 3
Lap 4
```

| Part | In the example | Job |
| --- | --- | --- |
| Initialization | `int lap = 1` | Create and set the loop control variable |
| Boolean expression | `lap <= 4` | Decide whether to run the body again |
| Update | `lap++` | Change the loop control variable after each iteration |

## 2\. The exact order of execution

The initialization statement runs only once, before the Boolean expression is evaluated for the first time. The variable it initializes is called the loop control variable. The Boolean expression is evaluated right after the initialization and then after each update, until it is false. In each iteration, the update runs after the entire loop body and before the Boolean expression is evaluated again 2.8.A.2.

```
for (int i = 0; i < 3; i++) {
    System.out.println("body sees i = " + i);
}
```

Output:

```
body sees i = 0
body sees i = 1
body sees i = 2
```

| Step | Action | i | Result |
| --- | --- | --- | --- |
| 1 | initialization | 0 |  |
| 2 | check `i < 3` | 0 | true |
| 3 | body | 0 | prints 0 |
| 4 | update `i++` | 1 |  |
| 5 | check `i < 3` | 1 | true |
| 6 | body | 1 | prints 1 |
| 7 | update `i++` | 2 |  |
| 8 | check `i < 3` | 2 | true |
| 9 | body | 2 | prints 2 |
| 10 | update `i++` | 3 |  |
| 11 | check `i < 3` | 3 | false, loop ends |

Count from the table: the initialization ran 1 time, the Boolean expression 4 times, the body 3 times and the update 3 times.

A loop control variable declared in the header exists only inside the loop:

```
for (int i = 0; i < 3; i++) {
    System.out.println(i);
}
System.out.println(i);
```

```
error: cannot find symbol
```

## 3\. Counting iterations and other updates

You can count iterations without tracing every step:

| Header | Values of the loop control variable | Iterations |
| --- | --- | --- |
| `for (int i = 0; i < 10; i++)` | 0 to 9 | 10 |
| `for (int i = 1; i <= 10; i++)` | 1 to 10 | 10 |
| `for (int i = 1; i < 10; i++)` | 1 to 9 | 9 |
| `for (int i = 0; i <= 10; i += 2)` | 0, 2, 4, 6, 8, 10 | 6 |
| `for (int i = 10; i > 0; i--)` | 10 down to 1 | 10 |
| `for (int i = 1; i < 100; i *= 3)` | 1, 3, 9, 27, 81 | 5 |

The update does not have to be `i++`. This loop counts down by 5:

```
for (int seconds = 20; seconds >= 0; seconds -= 5) {
    System.out.print(seconds + " ");
}
```

Output:

```
20 15 10 5 0 
```

A `for` loop is a good fit for an accumulator, such as the sum of the even numbers from 2 to 10:

```
int sum = 0;
for (int n = 2; n <= 10; n += 2) {
    sum += n;
}
System.out.println("Sum: " + sum);
```

Output:

```
Sum: 30
```

## 4\. for and while are interchangeable

A `for` loop can be rewritten as an equivalent `while` loop, and the other way around 2.8.A.3. Move the initialization above the loop, keep the Boolean expression, and make the update the last statement of the body.

```
for (int day = 1; day <= 3; day++) {
    System.out.println("Day " + day);
}
```

```
int day = 1;
while (day <= 3) {
    System.out.println("Day " + day);
    day++;
}
```

Both print the same three lines:

Output:

```
Day 1
Day 2
Day 3
```

One difference: in the `while` version, `day` is declared before the loop, so it still exists after the loop, with the value 4.

Going the other way, a `while` loop that halves a value becomes a `for` loop:

```
int steps = 0;
for (int size = 64; size > 1; size /= 2) {
    steps++;
}
System.out.println(steps);
```

Output:

```
6
```

## 5\. Common for loop errors

A semicolon after the header gives the loop an empty body. The empty body runs 5 times, then the block runs once:

```
int total = 0;
for (int i = 0; i < 5; i++); {
    total++;
}
System.out.println(total);
```

Output:

```
1
```

Changing the loop control variable in the body as well as in the header skips values:

```
for (int i = 0; i < 10; i++) {
    System.out.print(i + " ");
    i++;
}
```

Output:

```
0 2 4 6 8 
```

Each iteration adds 2 to `i`: once in the body and once in the update. Keep all changes to the loop control variable in the header.

## Common mix-ups

- Not true: The update runs before the body, at the top of each iteration. The update runs after the entire body, just before the Boolean expression is checked again. In the first iteration the body uses the initial value.
- Not true: The initialization runs every time the loop repeats. It runs exactly once, before the first Boolean expression evaluation.
- Not true: You can use the loop control variable after the loop ends. A variable declared in the for loop header only exists inside the loop. Using it after the loop is a compile error (cannot find symbol).

## In 30 seconds

- A for loop header has three parts, separated by semicolons: initialization; Boolean expression; update.
- The initialization runs once. The variable it sets up is the loop control variable.
- Order: initialize, check, body, update, check, body, update, ... until the check is false.
- for (int i = a; i < b; i++) runs b - a times; with <= it runs b - a + 1 times (when a <= b).
- Every for loop can be rewritten as an equivalent while loop, and every while loop as a for loop.

## Checkpoint: 6 practice questions

### Question 1

Consider the following code segment. What is printed as a result of executing the code segment?

```java
int total = 0;
for (int i = 2; i < 10; i += 3) {
    total += i;
}
System.out.println(total);
```

- A. 26
- B. 7
- C. 15
- D. 24

**Answer: C.** The loop control variable takes the values 2, 5 and 8; the next value, 11, fails `i < 10`. The total is 2 + 5 + 8 = 15. 26 also adds 11, but 11 fails the check, so the body never runs with it. 7 stops after 2 and 5.

### Question 2

Consider the following code segment. How many times is `"x"` printed?

```java
for (int k = 5; k <= 25; k += 5) {
    System.out.print("x");
}
```

- A. 5
- B. 4
- C. 6
- D. 25

**Answer: A.** k takes the values 5, 10, 15, 20 and 25, and 25 passes the check `k <= 25`, so the body runs 5 times. 4 forgets the boundary value 25, and 25 confuses the bound with the number of iterations.

### Question 3

Consider the following code segment. What is printed as a result of executing the code segment?

```java
for (int i = 0; i < 3; i++) {
    System.out.print(i);
    i++;
}
```

- A. 012
- B. 0
- C. 024
- D. 02

**Answer: D.** The body prints i and then adds 1, and the update adds another 1 after the body. So the printed values are 0, then 2; after that i becomes 4 and `4 < 3` is false. "012" ignores the `i++` in the body, and "024" forgets that the Boolean expression is checked before each iteration.

### Question 4

Which of the following code segments prints the same output as `for (int j = 10; j > 0; j -= 4) { System.out.print(j + " "); }` ?

- A. `int j = 10; while (j > 0) { j -= 4; System.out.print(j + " "); }`
- B. `int j = 10; while (j > 0) { System.out.print(j + " "); j -= 4; }`
- C. `int j = 0; while (j < 10) { System.out.print(j + " "); j += 4; }`
- D. `int j = 10; while (j > 0) { System.out.print(j + " "); } j -= 4;`

**Answer: B.** An equivalent `while` loop puts the initialization before the loop, keeps the Boolean expression, and makes the update the last statement in the body, which prints 10 6 2. Updating before printing prints 6 2 -2. Counting up from 0 prints 0 4 8, and putting the update after the loop leaves j at 10 forever, an infinite loop.

### Question 5

Consider the following code segment. Which of the following best describes the result of compiling and running it?

```java
int sum = 0;
for (int i = 0; i < 3; i++) {
    sum += i;
}
System.out.println(sum + i);
```

- A. 6 is printed, because `i` is 3 after the loop.
- B. 5 is printed.
- C. 3 is printed.
- D. The code does not compile.

**Answer: D.** The loop control variable `i` is declared in the `for` loop header, so it exists only inside the loop. Using `i` in the last statement causes a compile error (cannot find symbol). If `i` had been declared before the loop, it would be 3 after the loop and 3 + 3 = 6 would print, which is why 6 is tempting.

### Question 6

Consider the following code segment. What is printed as a result of executing the code segment?

```java
int count = 0;
for (int i = 0; i < 4; i++);
{
    count += 2;
}
System.out.println(count);
```

- A. 8
- B. 2
- C. 0
- D. Nothing is printed because the code does not compile.

**Answer: B.** The semicolon right after the header is the loop body, an empty statement, so the loop runs 4 times doing nothing. The block that follows is an ordinary block that runs once, so count is 2. 8 assumes the block is the loop body. The code is legal, so it compiles.

## Questions students ask

**What are the three parts of a for loop in Java?**

The initialization, which sets up the loop control variable; the Boolean expression, which is checked before each iteration; and the update, which runs after each iteration of the body.

**In what order does a for loop run?**

The initialization runs once. Then Java checks the Boolean expression, runs the body if it is true, runs the update, and checks again, until the Boolean expression is false.

**How many times does a for loop run?**

For for (int i = a; i < b; i++), b - a times. For i <= b, b - a + 1 times. Trace the first and last values when the update is not i++.

**Can every for loop be written as a while loop?**

Yes. Put the initialization before the while loop, use the Boolean expression as the while condition, and put the update as the last statement of the body.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
