Java For Loop

A Java for loop repeats a block of code while a condition stays true. It is especially useful when you know, or can calculate, how many times the loop should run.

A for loop puts the starting value, the condition, and the update in one line. This makes counter-based loops compact and easy to read.

Basic Syntax

A standard for loop has three parts inside the parentheses: an initializer, a condition, and an update. The initializer usually creates a counter variable, the condition decides whether the loop should continue, and the update changes the counter after each run.

public class Main {
    public static void main(String[] args) {
        for (int count = 1; count <= 5; count++) {
            System.out.println("Count: " + count);
        }
    }
}

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

How It Works

In the example, int count = 1 runs once before the loop starts. Then Java checks count <= 5. If the condition is true, the loop body runs.

After the body runs, Java executes count++, which adds 1 to the counter. Then it checks the condition again. When count becomes 6, the condition is false and the loop stops.

The Three Parts

Part Example Purpose
Initializer int count = 1 Sets up the starting value.
Condition count <= 5 Keeps looping while true.
Update count++ Changes the counter after each run.

These three parts are separated by semicolons. The loop body goes in braces after the parentheses.

Changing The Step

The update does not have to add exactly 1. You can add a larger number when you want to skip through values by a fixed step.

public class Main {
    public static void main(String[] args) {
        for (int number = 2; number <= 10; number += 2) {
            System.out.println(number);
        }
    }
}

Output:

2
4
6
8
10

Here, number += 2 increases the counter by 2 each time. The loop prints the even numbers from 2 through 10.

Counting Down

A for loop can also count downward. Start with a larger value, use a condition that stays true while the counter is still high enough, and subtract in the update.

public class Main {
    public static void main(String[] args) {
        for (int seconds = 3; seconds >= 1; seconds--) {
            System.out.println(seconds);
        }

        System.out.println("Go!");
    }
}

Output:

3
2
1
Go!

The counter starts at 3. After each print, seconds-- subtracts 1. When seconds becomes 0, the condition seconds >= 1 is false, so Java exits the loop and prints Go!.

For Loop Or While Loop?

Use a for loop when the setup, stopping condition, and update are all naturally tied to one counter. Use a while loop when the loop should continue until some condition changes and the number of repeats is less predictable.

Common Mistakes

  • Using < when you meant <=, which can make the loop run one fewer time than expected.
  • Updating the counter in the wrong direction, such as adding when the condition needs the value to get smaller.
  • Changing the counter inside the loop body in a way that makes the loop harder to understand.
  • Putting a semicolon right after the for parentheses, which creates an empty loop body.

Takeaway: a Java for loop is the clearest choice when you need to repeat code with a counter that has a clear start, stop, and update.