Syntax

java
for (initialization; condition; update) {
    // body
}

How a for loop executes

java
for (int i = 0; i < 5; i++) {
    System.out.println(i);
}
Step 1: Initialize  → i = 0
Step 2: Check condition → 0 < 5? true → execute body → print 0
Step 3: Update → i = 1
Step 4: Check condition → 1 < 5? true → execute body → print 1
Step 5: Update → i = 2
... (continues) ...
Step X: Update → i = 5
Step Y: Check condition → 5 < 5? false → EXIT loop

for loop vs. while loop

java
// for loop
for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

// Equivalent while loop
int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}

Common for loop patterns

Count from 0 to n-1

java
for (int i = 0; i < n; i++) {
    // runs n times: i = 0, 1, 2, ..., n-1
}

Count from 1 to n

java
for (int i = 1; i <= n; i++) {
    // runs n times: i = 1, 2, 3, ..., n
}

Count backwards

java
for (int i = 10; i >= 1; i--) {
    // runs 10 times: i = 10, 9, 8, ..., 1
}

Count by twos

java
for (int i = 0; i < 20; i += 2) {
    // i = 0, 2, 4, 6, ..., 18
}

Count with specific range

java
for (int i = 5; i <= 15; i++) {
    // i = 5, 6, 7, ..., 15 (11 iterations)
}

How many times does a loop run?

java
// General formula for: for (int i = start; i < end; i++)
// Iterations = end - start (when end > start)

for (int i = 0; i < 10; i++)    // 10 - 0 = 10 times
for (int i = 3; i < 8; i++)     // 8 - 3 = 5 times
for (int i = 0; i <= 10; i++)   // 10 - 0 + 1 = 11 times (inclusive!)
for (int i = 5; i > 0; i--)    // 5 times (i = 5, 4, 3, 2, 1)

Variable scope

java
for (int i = 0; i < 5; i++) {
    System.out.println(i);   // i is accessible here
}
// System.out.println(i);    // COMPILE ERROR — i doesn't exist here!

// If you need i after the loop, declare it before:
int i;
for (i = 0; i < 5; i++) {
    System.out.println(i);
}
System.out.println("Final: " + i);  // works! i = 5

String traversal with for

java
String word = "Hello";
for (int i = 0; i < word.length(); i++) {
    String ch = word.substring(i, i + 1);
    System.out.println(i + ": " + ch);
}
// 0: H
// 1: e
// 2: l
// 3: l
// 4: o

Count vowels

java
String text = "Hello World";
int vowels = 0;
for (int i = 0; i < text.length(); i++) {
    String ch = text.substring(i, i + 1).toLowerCase();
    if (ch.equals("a") || ch.equals("e") || ch.equals("i") || 
        ch.equals("o") || ch.equals("u")) {
        vowels++;
    }
}
System.out.println("Vowels: " + vowels);   // 3

Remove all spaces

java
String original = "Hello World";
String noSpaces = "";
for (int i = 0; i < original.length(); i++) {
    String ch = original.substring(i, i + 1);
    if (!ch.equals(" ")) {
        noSpaces += ch;
    }
}
// noSpaces = "HelloWorld"

Accumulation patterns

java
// Sum of 1 to n
int n = 10;
int sum = 0;
for (int i = 1; i <= n; i++) {
    sum += i;
}
// sum = 55

// Factorial (n!)
int n = 5;
int factorial = 1;
for (int i = 1; i <= n; i++) {
    factorial *= i;
}
// factorial = 120 (5! = 1×2×3×4×5)

// Powers of 2
int power = 1;
for (int i = 0; i < 10; i++) {
    System.out.println("2^" + i + " = " + power);
    power *= 2;
}

Trace practice

java
int result = 0;
for (int i = 1; i <= 4; i++) {
    result += i * i;
}

AP Exam Tips

Common Mistakes

Key Vocabulary