Introduction
The for loop is one of the most essential tools in Java programming. It helps you repeat tasks with precision and control, especially when the number of repetitions is known. Beginners often understand its syntax but miss the logic behind it โ which leads to mistakes. This article focuses only on the for loop and explains it in a way that clears confusion and builds confidence.
๐ What Is a for Loop?
A for loop lets you repeat a block of code multiple times with a clear start, condition for continuation, and update after each repetition.
General form:
for (initialization; condition; update) {
// body
}
๐ก What Each Part Means
- Initialization โ Sets the start point (runs once)
- Condition โ Checked before each iteration
- Update โ Changes loop variable (runs after each iteration)
- Body โ Code that runs each time the condition is true
๐ Basic Example
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
โ Output:
1
2
3
4
5
๐ What happens under the hood:
1๏ธโฃ i = 1 (initialization)
2๏ธโฃ Check i <= 5 (true)
3๏ธโฃ Run body
4๏ธโฃ i++ (update)
Repeat until condition fails
โ Common Mistakes & Confusions
โ Mistake 1 โ Wrong Condition Symbols
for (int i = 1; i = 5; i++) // โ WRONG
โก๏ธ This causes a compile error because = is an assignment, not a condition.
โ Correct:
for (int i = 1; i == 5; i++)
but this still rarely makes sense; usually you want < or <=.
โ Mistake 2 โ Off-by-One Errors
for (int i = 1; i < 5; i++)
โ Output:
1
2
3
4
โ But if you expected 5 also, you need:
for (int i = 1; i <= 5; i++)
๐ This one small symbol is a huge source of bugs.
โ Mistake 3 โ Loop Variable Scoped Outside?
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}
System.out.println(i); // โ ERROR
๐ก i is only visible inside the loop.
โ Mistake 4 โ Unintended Infinite Loop
for (int i = 1; i <= 3; ) {
System.out.println("Oops");
}
โ No update โ condition always true โ infinite loop.
โ Mistake 5 โ Accidental Semicolon
for (int i = 1; i <= 5; i++); // โ <-- notice ;
{
System.out.println("Hello");
}
๐ต This runs the loop without body and then prints "Hello" once. Sneaky!
๐ง Tricky for Loop Examples
๐ Multiple Variables
for (int i = 1, j = 5; i <= j; i++, j--) {
System.out.println(i + " " + j);
}
โ Output:
1 5
2 4
3 3
โ You can initialize and update multiple variables as long as they are of the same type.
๐ No Initialization
int i = 1;
for (; i <= 3; i++) {
System.out.println(i);
}
โ Initialization can be outside โ loop still works.
โพ Infinite Loop (Explicit)
for (;;) {
System.out.println("Forever!");
}
โ All parts of a for loop are optional.
โ Reversing with for
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
โ Great for counting down.
๐ฅ Using break
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
โ Stops the loop before completion.
โ Output:
1
2
3
4
๐ Using continue
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
โ Skips even numbers.
โ Output:
1
3
5
๐ญ Quick Recap
โ The for loop is simple but powerful when you understand:
- Order of execution
- How conditions control flow
- How tiny mistakes change behavior
๐ง Always trace loops step by step to see how values change.
๐ Summary โ Key Tips for Students
โ Know the difference between <, <=, ==, and =
โ Watch for missing updates
โ Avoid unintended semicolons
โ Use break and continue intentionally
โ Practice dry-running loops with pencil and paper
