⏱️ 7 min read • Beginner Level • Lesson 28
Completed on . You can revise this lesson or continue to the next topic.
The for loop in Java is used to execute a block of code repeatedly for a specific number of times. It is commonly used when the number of iterations is already known.
Before learning the for loop, you should understand
Java while loop,
Java do-while loop, and
Java operators.
A for loop is a repetition control structure that allows you to write
a loop compactly. It is mostly used when you know how many times a statement or block
of statements should execute.
A for loop is best for counter-based repetition.
Initialization, condition, and update are written in one line.
It is commonly used for printing patterns, tables, arrays, and repeated tasks.
A for loop has three main parts: initialization, condition, and update.
for (initialization; condition; update) {
// loop body
}
initialization runs once, the condition is checked before every iteration,
and the update runs after each iteration.
The following diagram shows how a for loop works. Java initializes the loop variable, checks the condition, executes the loop body, updates the variable, and then repeats.
true, the loop body executes.false, the loop stops.
The following program prints Hello five times using a for loop.
public class ForDemo {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(i + ". Hello");
}
}
}
Output:
1. Hello 2. Hello 3. Hello 4. Hello 5. Hello
i is initialized with value 1.i <= 5 is checked before each iteration.i with Hello.i++ increases the value of i by 1.i becomes 6, the condition becomes false and the loop stops.
The following program calculates the sum of numbers from 1 to 5.
public class SumForLoop {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum = sum + i;
}
System.out.println("Sum = " + sum);
}
}
Output:
Sum = 15
A for loop can also count backward by using a decrement operation.
public class ReverseForLoop {
public static void main(String[] args) {
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
}
}
Output:
5 4 3 2 1
public class FactorialExample {
public static void main(String[] args) {
int number = 5;
int factorial = 1;
for(int i = 1; i <= number; i++) {
factorial = factorial * i;
}
System.out.println("Factorial of " + number + " = " + factorial);
}
}
Output:
Factorial of 5 = 120
number stores the number whose factorial
needs to be calculated.
factorial is initialized to 1
because factorial calculation starts with multiplication by 1.
for loop starts from i = 1 and runs
until i <= number.
factorial is multiplied by i.
number = 5, the calculations are:
1 × 1 = 1
1 × 2 = 2
2 × 3 = 6
6 × 4 = 24
24 × 5 = 120
factorial becomes 120.
Factorial of 5 = 120.
A factorial is the product of all positive integers from
1 to the given number.
For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.
The following program prints the multiplication table of 5.
public class MultiplicationTable {
public static void main(String[] args) {
int number = 5;
for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i + " = " + (number * i));
}
}
}
Output:
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
A for loop can be placed inside another for loop. This is called a nested for loop and is commonly used for patterns, tables, and matrix operations.
for(int row = 1; row <= 3; row++) {
for(int col = 1; col <= 3; col++) {
System.out.print("* ");
}
System.out.println();
}
Output:
* * * * * * * * *
An infinite for loop happens when the condition never becomes false. Java also allows all three parts of a for loop to be omitted.
public class InfiniteForLoop {
public static void main(String[] args) {
for (;;) {
System.out.println("This loop runs forever");
}
}
}
false.
| for Loop | while Loop |
|---|---|
| Best when the number of iterations is known. | Best when the number of iterations is not known. |
| Initialization, condition, and update are written in one place. | Initialization, condition, and update are usually written separately. |
| Commonly used for counters, arrays, and tables. | Commonly used for condition-based repetition. |
When should you use a for loop instead of a while loop?
Use a for loop when the number of iterations is known in advance. Use a while loop when the number of iterations depends on a condition.
for loop is used to repeat code a fixed number of times.for (;;).🧠 Test your understanding with a quick quiz
Topic: For-loop | Language: Java
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}for (int i = 1; i <= 5; i++) {
System.out.println("Hello");
}for (int i = 1; i <= 3; i++) {
System.out.println(i);
}🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.