The for loop in Java is a control flow statement used to execute a block of code repeatedly based on a condition. It is especially useful when the number of iterations is known in advance, such as iterating over a range of values, arrays, or collections.
Example:
class Geeks {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
System.out.println("Loop has ended.");
}
}
Output
1 2 3 4 5 Loop has ended.
Example:
- The loop variable i starts from 1.
- The loop runs as long as i <= 5.
- After each iteration, i is incremented by 1.
- When the condition becomes false, the loop terminates.
Syntax
for (initialization; condition; update) {
// loop body
}

Working:
- Initialization is executed once when the loop starts.
- The condition is evaluated before every iteration.
- If the condition is true, the loop body executes.
- After execution, the update expression runs.
- Steps 2–4 repeat until the condition becomes false.
- Control exits the loop once the condition fails.
Flow Chart for loop in Java

Examples and Usage of for loop
The following examples demonstrate how for loops and nested for loops are used in Java for iteration, pattern printing, and calculations.
Example 1: Printing Numbers from 1 to 10
class Geeks {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
Output
1 2 3 4 5 6 7 8 9 10
Example 2: Printing "Hello World" Multiple Times
class Geeks {
public static void main(String args[]) {
// Writing a for loop
// to print Hello World 5 times
for (int i = 1; i <= 5; i++)
System.out.println("Hello World");
}
}
Output
Hello World Hello World Hello World Hello World Hello World
Example 3: Calculating Sum from 1 to 20
class Geeks {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 20; i++) {
sum += i;
}
System.out.println("Sum: " + sum);
}
}
Output
Sum: 210
Nested For Loop
A nested for loop is a loop inside another loop. It is commonly used for matrix operations and pattern printing.
Example: Printing a Matrix-like Pattern
class Geeks {
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();
}
}
}
Output
(1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3)
Note: To know more about Nested loops refer Nested loops in Java.
Infinite for Loop in Java
An infinite for loop occurs when the loop condition never becomes false.
class Geeks {
public static void main(String[] args) {
for (;;) {
System.out.println("Infinite Loop");
}
}
}
Output:
Infinite Loop 1
Infinite Loop 2
Note: A "Time Limit Exceeded" error occurs when a program runs longer than the time allocated for execution, due to infinite loops.
Advantages of for Loop
- This is ideal for iterating over a known range or collection.
- Efficient syntax for initialization, condition checking, and incrementing.
- Easy to control the number of iterations.
- Efficient for looping through arrays and collections.