Java For Loop
When you know how many times something should repeat — or you want to step through a range of numbers — the for loop is the tool of choice. It puts the setup, the condition and the update all on one line.
Syntax
for (initialisation; condition; update) {
// body
}- initialisation runs once, before the loop starts (usually
int i = 0). - condition is checked before every pass; the loop stops when it's false.
- update runs after every pass (usually
i++).
class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println("Pass " + i);
}
}
}Pass 0
Pass 1
Pass 2
Pass 3
Pass 4
Counting in different ways
The update can be anything — count by twos, count down, or multiply:
class Main {
public static void main(String[] args) {
for (int i = 0; i <= 10; i += 2) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 5; i >= 1; i--) {
System.out.print(i + " ");
}
System.out.println();
for (int p = 1; p <= 1000; p *= 10) {
System.out.print(p + " ");
}
System.out.println();
}
}0 2 4 6 8 10
5 4 3 2 1
1 10 100 1000
Accumulating a result
A loop often builds up a value — a sum, a product, a string:
class Main {
public static void main(String[] args) {
int sum = 0;
long factorial = 1;
for (int i = 1; i <= 10; i++) {
sum += i;
factorial *= i;
}
System.out.println("1 + ... + 10 = " + sum);
System.out.println("10! = " + factorial);
}
}1 + ... + 10 = 55
10! = 3628800
Nested loops
A loop inside another loop runs completely for each pass of the outer loop. This is perfect for grids and tables:
class Main {
public static void main(String[] args) {
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= 4; col++) {
System.out.printf("%4d", row * col);
}
System.out.println();
}
}
} 1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
The inner loop runs 4 times per outer pass, so the body runs 4 × 4 = 16 times in total.
The for-each loop
To visit every element of an array or collection, the enhanced for loop (for-each) is simpler and avoids index mistakes:
class Main {
public static void main(String[] args) {
String[] planets = {"Mercury", "Venus", "Earth", "Mars"};
for (String planet : planets) {
System.out.println(planet + " has " + planet.length() + " letters");
}
}
}Mercury has 7 letters
Venus has 5 letters
Earth has 5 letters
Mars has 4 letters
Read for (String planet : planets) as "for each planet in planets". Use a classic for loop instead when you need the index or want to modify the array.
| Loop | Best for |
|---|---|
for (int i = ...; ...; ...) |
counting, needing the index, unusual steps |
for (T x : collection) |
reading every element in order |
while |
repeating until a condition changes |
Note: A variable declared in the loop header (
int i) only exists inside the loop.
Test yourself
How many times does for (int i = 0; i < 8; i += 3) run its body?
- 2
- 3
- 8
- 9
In a nested loop where the outer runs 3 times and the inner runs 5 times per pass, how often does the inner body run?
- 5
- 8
- 15
- 3
Which loop header iterates over every element of int[] data?
- for (data : int x)
- for (int x : data)
- for (int x in data)
- foreach (int x of data)