Loops are one of the most powerful tools in programming, and in Java, they help you repeat actions without writing the same line of code over and over again. One of the most common and useful loop types is the
for loop.
In this guide, we’ll focus on the
Java for
statement—what it is, how it works, and how you can use it in real-world situations. Whether you're printing a list of numbers or processing items in an array, the
for
loop is your go-to tool.
Let’s get started!
What Is a For Loop?
A
for loop in Java allows you to run a block of code a specific number of times. It’s great when you know in advance how many times you want the loop to repeat.
The basic structure looks like this:
for (initialization; condition; update) {
// code to run each time the loop repeats
}
Breakdown:
- Initialization: Set a starting point (e.g.,
int i = 0
)
- Condition: The loop runs as long as this is
true
(e.g., i < 5
)
- Update: Runs after each loop cycle (e.g.,
i++
)
A Simple For Loop Example
Let’s print numbers from 1 to 5:
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
}
What happens here:
- The loop starts with
i = 1
- It checks if
i <= 5
- If
true
, it runs the code block and then increases i
by 1
- This repeats until
i
is greater than 5
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
When to Use a For Loop
Use a
for
loop when:
- You know how many times you want to run something
- You're working with arrays or collections where you're looping from start to finish
- You want compact, readable control over loop variables
Looping in Reverse
Want to count down instead of up? Just change the loop values:
for (int i = 5; i >= 1; i--) {
System.out.println("Countdown: " + i);
}
This loop decreases
i
each time instead of increasing it.
Using For Loops with Arrays
For loops are perfect for going through arrays.
Example:
String[] fruits = {"Apple", "Banana", "Cherry"};
for (int i = 0; i < fruits.length; i++) {
System.out.println(fruits[i]);
}
Output:
Apple
Banana
Cherry
Here,
fruits.length
tells the loop how many times to run.
Enhanced For Loop (For-Each)
Java also offers a simpler version of the
for
loop for arrays and collections:
for (String fruit : fruits) {
System.out.println(fruit);
}
This is called the
enhanced for loop or
for-each loop. It’s cleaner and safer when you don’t need to know the index.
Use this when:
- You just want to go through every item in an array or list
- You don’t need to modify the array or access items by index
Common Mistakes to Avoid
1. Infinite Loops
If you forget to update the loop variable, the loop might never stop:
for (int i = 0; i < 10;) {
System.out.println("Oops!");
// Missing i++ here
}
This will print "Oops!" forever (or until your program crashes).
2. Off-by-One Errors
Make sure your loop boundaries are correct. For example:
for (int i = 0; i <= 5; i++) { ... } // Runs 6 times
for (int i = 0; i < 5; i++) { ... } // Runs 5 times
Nested For Loops
You can put one
for
loop inside another. This is useful for working with grids, tables, or combinations.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.println("i = " + i + ", j = " + j);
}
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
Tips for Writing Better For Loops
- Use meaningful variable names when looping over data. Instead of
i
, try index
or count
when appropriate.
- Keep your loop condition simple and easy to read.
- Avoid modifying the loop variable inside the loop body unless you know what you’re doing.
- If you're using loops with collections (like
ArrayList
), consider using the enhanced for loop for better readability.
image quote pre code