Java For Loop
Keep counted repetition in one place.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind For Loop.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
A conventional for loop keeps initialization, continuation condition, and update together. Trace the condition before each iteration, including the final failed check. That makes off-by-one mistakes easier to spot.
Explore the details
Java For Loop
Use a for loop when you know exactly how many times the loop should run. It packs the init, condition, and update into one compact line.
The Three Parts of a for Loop
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
for | Keyword that starts the for loop |
int i = 0 | Statement 1: runs once before the loop starts — initialise your counter |
i < 5 | Statement 2: checked before every iteration — loop runs while this is true |
i++ | Statement 3: runs after every iteration — update the counter |
} | Closing brace — end of loop body |
- Init (
int i = 0) — runs once before the loop starts. Sets the starting value. - Condition (
i < 5) — checked before every iteration. If false, loop stops. - Update (
i++) — runs after every iteration. Moves the counter forward.
More Examples
for vs while: Use for when you know the number of iterations in advance. Use while when the loop count depends on a condition that can change unpredictably.
// Count from 1 to 5
for (int i = 1; i <= 5; i++) {
System.out.print(i + " "); // 1 2 3 4 5
}
// Enhanced for-each — arrays
int[] nums = {10, 20, 30, 40, 50};
for (int n : nums) {
System.out.print(n + " "); // 10 20 30 40 50
}
// Nested loops — times table
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.printf("%3d", i * j);
}
System.out.println();
}A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int total = 0;
for (int n = 1; n <= 4; n++) {
total += n;
}
System.out.println(total);
}
}Predict first. Then reveal the expected output / வெளியீடு
10
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Change the upper bound to 5 and predict the new total.
Reveal the answer and reasoning / விடை
The result is 15, because 1 + 2 + 3 + 4 + 5 = 15.
One mistake to avoid: n < 4 and n <= 4 describe different sets of values. Decide whether the upper bound is inclusive.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.