Learn how to execute a block of code a specific number of times using the for loop.
Java · beginner · 15 min · 20 steps · free
What you'll learn
Understand the three components of a for loop: initialization, condition, and update.
Learn the execution flow of a for loop.
Recognize how the loop variable controls the number of iterations.
Understand the scope of the loop control variable.
ForLoopDemo.java
public class ForLoopDemo {
public static void main(String[] args) {
// This for loop will print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
System.out.println("Loop finished.");
}
}
Step by step
The program enters the for loop. The first part, the initialization (int i = 1), is executed once.
Next, the condition (i <= 5) is checked. Since 1 is less than or equal to 5, the condition is true, and we enter the loop body.
The code inside the loop body is executed. The string "Iteration: " is created and will be added to the string pool.
The value of i (1) is concatenated with the string, and the result is printed to the console.
After the loop body, the update statement (i++) is executed. The value of i is incremented to 2.
The condition (i <= 5) is checked again. Since 2 is less than or equal to 5, the condition is true.
The loop body executes again, printing the current value of i.
The update statement (i++) executes, incrementing i to 3.
The condition (3 <= 5) is true. The loop continues.
The loop body prints "Iteration: 3".
i is incremented to 4.
The condition (4 <= 5) is true.
The loop body prints "Iteration: 4".
i is incremented to 5.
The condition (5 <= 5) is true.
The loop body prints "Iteration: 5".
i is incremented to 6.
The condition (i <= 5) is checked. Since 6 is not less than or equal to 5, the condition is false. The loop terminates.
Execution continues to the next statement after the loop. The variable i is now out of scope and removed from the stack.
The final message is printed to the console.
Common mistakes
Off-by-one errors: using < instead of <=, or starting/ending at the wrong number.
Creating an infinite loop by having a condition that never becomes false.
Modifying the loop counter variable inside the loop body, which can lead to unexpected behavior.
Best practices
Declare the loop variable inside the for statement (e.g., for (int i = 0; ...)) to limit its scope to the loop.
Use meaningful names for loop variables if they represent more than a simple counter.
Prefer the for loop over a while loop when the number of iterations is known beforehand.
Key takeaways
A for loop has three parts: the initialization (runs once), the condition (checked before every pass) and the update (runs after every pass).
The body runs only while the condition is true. If it is false from the start, the body never runs.
A variable declared in the for statement exists only inside the loop.
Check yourself
How many times does int i = 1 run in this loop?
Answer: Once. The initialization runs once, before the first condition check.
With i <= 5, what is i when the loop stops?
Answer: 6. The loop stops when the condition first fails, which happens when i becomes 6.
What happens if <= is changed to <?
Answer: It prints 1 to 4. With i < 5, the loop stops as soon as i reaches 5, so 5 is never printed.