Last Updated : 12 Jul, 2025
Java for loop is a control flow statement that allows code to be executed repeatedly based on a given condition. The for loop in Java provides an efficient way to iterate over a range of values, execute code multiple times, or traverse arrays and collections.
Now let's go through a simple Java for loop example to get the clarity first.
Example:
Java
// Java Program to demonstrate for loop
class Geeks {
public static void main(String args[]) {
// for loop to print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
System.out.println("" + i);
}
System.out.println("Loop has ended.");
}
}
1 2 3 4 5 Loop has ended.Dry-Running Above Example
Workingfor (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
Java for loop is divided into various parts as mentioned below:
Initializes the loop variable. This is executed once at the start of the loop.
Example:
2. Test Expressionint i = 1;
Tests the loop condition. If true, the loop body is executed; otherwise, the loop terminates.
Example:
3. Update Expressioni <= 10
After executing the loop body, this expression increments/decrements the loop variable by some value.
Example:
Flow Chart for loop in Java Example 1: Printing Numbers from 1 to 10 Javai++;
// Java program to print numbers from 1 to 10
class Geeks {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
1 2 3 4 5 6 7 8 9 10Example 2: Printing "Hello World" 5 Times Java
// Java program to illustrate for loop
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");
}
}
Hello World Hello World Hello World Hello World Hello WorldExample 3: Calculating Sum from 1 to 20 Java
// Java program to calculate the
// sum of numbers from 1 to 20
class Geeks {
public static void main(String args[]) {
int s = 0;
// for loop begins
// and runs till x <= 20
for (int x = 1; x <= 20; x++) {
s = s + x;
}
System.out.println("Sum: " + s);
}
}
Nested For Loop
Java Nested For Loop is a concept of using a for loop inside another for loop. It is commonly used for multidimensional problems.
Example: Printing a Matrix-like Pattern
Java
// Java program to illustrate
// Nested For Loop
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();
}
}
}
(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.
Java Infinite for LoopAn infinite loop occurs when the loop's condition never becomes false. In the example below, the condition i >= 1
always evaluates to true, leading to an infinite loop.
Example 1:
Java
// Java Program to Illustrate
// infinite loop
class Geeks {
public static void main(String args[])
{
for (int i = 1; i >= 1; i++) {
System.out.println("Infinite Loop " + i);
}
}
}
Output:
Infinite Loop 1
Infinite Loop 2
...
Example 2: Another way to create an infinite loop is by using empty semicolons ;; in the for loop.
Java
public class Geeks {
public static void main(String[] args) {
for (;;) {
System.out.println("Infinite Loop");
}
}
}
Output:
infinite loop
infinite loop
....
Important 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 LoopRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4