Java for Loop
In Java, loops are used to execute a block of code repeatedly as long as a specified condition is true. Among all looping statements, the for loop is one of the most commonly used because it provides a clear and compact way to control the flow of iteration.
The for loop is especially useful when the number of iterations is known in advance — for example, when iterating over an array, printing a sequence of numbers, or performing a task multiple times.
Syntax of the for Loop
for (initialization; condition; update) {
// Code to be executed repeatedly
}
Explanation of Components
- Initialization:
This part initializes the loop control variable. It executes only once, at the beginning of the loop.
Example: int i = 0; - Condition:
The loop continues as long as this condition is true. When it becomes false, the loop stops.
Example: i < 5; - Update:
After each iteration, this expression updates the loop control variable (usually increment or decrement).
Example: i++
Example 1: Basic for Loop
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(“Iteration: ” + i);
}
}
}
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Explanation:
The loop starts with i = 1 and executes as long as i <= 5. After each iteration, i is incremented by 1.
Example 2: Printing Even Numbers Using for Loop
public class EvenNumbersExample {
public static void main(String[] args) {
for (int i = 2; i <= 10; i += 2) {
System.out.println(i);
}
}
}
Output:
2
4
6
8
10
Explanation:
Here, i starts from 2 and increases by 2 after each iteration. This way, only even numbers are printed.
Example 3: for Loop with Decrement
public class CountdownExample {
public static void main(String[] args) {
for (int i = 5; i > 0; i–) {
System.out.println(“Countdown: ” + i);
}
}
}
Output:
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Explanation:
In this example, the loop runs backward — it starts from 5 and decreases i by 1 each time until the condition i > 0 becomes false.
Example 4: Nested for Loop
A nested for loop means one loop inside another. It is often used for patterns, grids, or matrix operations.
public class NestedForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print(“(” + i + “,” + j + “) “);
}
System.out.println(); // Move to next line
}
}
}
Output:
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
Explanation:
The outer loop runs for i = 1 to 3, and for each iteration of i, the inner loop runs for j = 1 to 3.
Example 5: Infinite for Loop
If you omit all three components, the for loop becomes infinite. You can manually stop it using a break statement.
public class InfiniteLoopExample {
public static void main(String[] args) {
for (;;) {
System.out.println(“Running infinitely…”);
break; // Stops the loop
}
}
}
Output:
Running infinitely…
Explanation:
Since there’s no condition, the loop would run forever if the break statement weren’t used.
Example 6: Enhanced for Loop (for-each Loop)
The enhanced for loop, introduced in Java 5, simplifies iteration over arrays and collections.
Syntax:
for (type variable : arrayOrCollection) {
// Code to be executed
}
Example:
public class ForEachExample {
public static void main(String[] args) {
String[] languages = {“Java”, “Python”, “C++”, “JavaScript”};
for (String lang : languages) {
System.out.println(lang);
}
}
}
Output:
Java
Python
C++
JavaScript
Explanation:
The loop automatically iterates through each element in the languages array — no need for indexing.
Key Points to Remember
- The for loop is ideal when the number of iterations is known in advance.
- The initialization, condition, and update parts are optional.
- You can use multiple variables in the initialization and update sections, separated by commas.
- The enhanced for loop (for-each) is used for arrays and collections — it’s simpler and avoids index errors.
- You can use break to exit the loop early and continue to skip to the next iteration.
Example 7: Using break and continue in for Loop
public class BreakContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the rest of the loop for i=3
}
if (i == 5) {
break; // Exit the loop when i=5
}
System.out.println(i);
}
}
}
Output:
1
2
4
Explanation:
When i equals 3, the continue statement skips that iteration. When i equals 5, the break statement terminates the loop.
Conclusion
The for loop in Java is a fundamental control structure that allows developers to execute code repeatedly in a controlled manner. Whether it’s a traditional counter-based loop, a nested loop, or the enhanced for-each loop, understanding how to use for loops effectively is essential for writing efficient Java programs.
With its flexibility and concise syntax, the for loop remains a favorite tool among developers for performing repetitive tasks.