Java for Loop
Iterate a fixed number of times in Java with the for loop — initialization, condition, and update expressions.
The for loop is the workhorse of Java iteration. It bundles three pieces — what to do before the loop, what to check before each iteration, and what to do after each iteration — into a single header. The result is more compact and harder to bungle than the same thing written with while.
Syntax
for (initialization; condition; update) {
// body
}Three semicolon-separated parts:
- Initialization — runs once, before the loop starts. Typically declares the counter.
- Condition — checked before each iteration. If false, the loop ends.
- Update — runs after each iteration. Typically increments the counter.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}Prints 0 1 2 3 4. The header is read as: start with i = 0, run as long as i < 5, increment i each time.
Equivalent while form
A for loop is mechanically equivalent to this while:
{
int i = 0; // initialization
while (i < 5) { // condition
System.out.println(i);
i++; // update
}
}The advantage of for is that everything controlling the loop lives in one line, making intent obvious at a glance.
Counting variations
The for header is flexible. The three pieces can be anything:
// counting down
for (int i = 10; i > 0; i--) { ... }
// stepping by 2
for (int i = 0; i < 100; i += 2) { ... }
// looping over an array by index
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
// nested with a separate step
for (int i = 0; i < n; i++) {
// ...
}If you don't need a particular part, leave it out — but keep the semicolons:
for (;;) { // identical to while (true)
if (done()) break;
}Scope of the loop variable
A variable declared in the initialization clause is local to the loop. After the loop ends, it's gone:
for (int i = 0; i < 5; i++) {
// i is visible here
}
// i is not visible hereThat's by design — it stops you from accidentally relying on the counter's final value. If you need the value outside the loop, declare it beforehand:
int i;
for (i = 0; i < 5; i++) {
if (someCondition(i)) break;
}
System.out.println("stopped at i=" + i);Multiple variables in one header
You can declare and update more than one variable, separated by commas:
for (int i = 0, j = 10; i < j; i++, j--) {
System.out.println(i + " " + j);
}Useful occasionally — but if a loop needs more than two interacting variables, that's usually a sign to refactor into smaller pieces.
When to choose for over while
Use a for loop when you know — or can express in one line — how many iterations you need or which range you're scanning. Use a while loop when the stopping condition is detached from a counter (e.g. "until input runs out", "until success").
For walking an array or collection element-by-element, the enhanced for-each loop is almost always cleaner.
A worked example
What's next
When you just want to visit every element of an array or collection without managing an index, the for-each loop is the right tool.
Practice
What is printed by for (int i = 0; i < 5; i += 2) System.out.print(i); ?