Lesson 23 of 50 – Java For Loop
46%

Java For Loop

A for loop in Java is used to execute a block of code repeatedly. It is especially useful when we know how many times we want to repeat a task.

1. What Is a For Loop?

Suppose you want to print "Hello Java" five times. Instead of writing five print statements, you can use a loop.

for (int i = 1; i <= 5; i++) {
    System.out.println("Hello Java");
}

Output:

Hello Java
Hello Java
Hello Java
Hello Java
Hello Java
A loop reduces repeated code and makes programs easier to maintain.
2. Syntax of the For Loop
for (initialization; condition; update) {
    // Code to execute repeatedly
}
  • Initialization: Creates or initializes the loop variable.
  • Condition: Checks whether the loop should continue.
  • Update: Changes the loop variable after each iteration.
  • Body: Contains the statements executed during each iteration.

Example:

for (int i = 1; i <= 3; i++) {
    System.out.println(i);
}
3. Understanding Each Part
for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}
Part Meaning
int i = 1 Starts the counter at 1.
i <= 5 Continues while i is less than or equal to 5.
i++ Increases i by 1 after each iteration.
System.out.println(i) Prints the current value of i.
4. Print Numbers from 1 to 10
public class Main {
    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
5. Print Numbers in Reverse Order

You can use a for loop to print numbers from 10 to 1.

for (int i = 10; i >= 1; i--) {
    System.out.println(i);
}

Output:

10
9
8
7
6
5
4
3
2
1

i-- decreases the value of i by 1.

6. Print Even Numbers

We can print even numbers from 2 to 20 by increasing the counter by 2.

for (int i = 2; i <= 20; i += 2) {
    System.out.println(i);
}

Output:

2
4
6
8
10
12
14
16
18
20

i += 2 adds 2 to the current value of i.

7. Calculate the Sum of Numbers

This program calculates the sum of numbers from 1 to 10.

public class Main {
    public static void main(String[] args) {

        int sum = 0;

        for (int i = 1; i <= 10; i++) {
            sum = sum + i;
        }

        System.out.println("Sum = " + sum);
    }
}

Output:

Sum = 55

The variable sum stores the running total.

8. Multiplication Table

Print the multiplication table of 5.

public class Main {
    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
9. For Loop with an Array

A for loop can access array elements using their indexes.

public class Main {
    public static void main(String[] args) {

        String[] names = {"Amit", "Rahul", "Priya", "Neha"};

        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }
    }
}

Output:

Amit
Rahul
Priya
Neha
Array indexes start at 0. The expression names.length gives the number of elements.
10. Multiple Variables in a For Loop

You can initialize and update two variables in one for loop.

for (int i = 1, j = 10; i <= 5; i++, j--) {
    System.out.println("i = " + i + ", j = " + j);
}

Output:

i = 1, j = 10
i = 2, j = 9
i = 3, j = 8
i = 4, j = 7
i = 5, j = 6
11. Infinite For Loop

An infinite loop continues indefinitely because its condition never becomes false.

for (;;) {
    System.out.println("Hello Java");
}

This loop has no initialization, condition, or update expression.

Be careful when running infinite loops. They can keep printing output until the program is stopped.
12. Common For Loop Mistakes
Mistake Correction
Using i < 5 when you need 1 to 5 Use i <= 5 when starting at 1.
Forgetting to update the counter Include an update such as i++.
Using i <= array.length Use i < array.length.
Adding an unnecessary semicolon after the loop header Write the opening brace directly after the header.

Incorrect example:

for (int i = 1; i <= 5; i++); {
    System.out.println(i);
}

The semicolon ends the loop body. Also, in this example, i is not in scope inside the following block. Remove the semicolon and keep the print statement inside the loop.

13. Practice Assignments

Write Java programs to solve these beginner-level assignments.

  1. Print numbers from 1 to 50.
  2. Print numbers from 50 to 1.
  3. Print all odd numbers from 1 to 30.
  4. Print the multiplication table of 7.
  5. Calculate the sum of numbers from 1 to 100.
  6. Calculate the factorial of a positive integer.
  7. Count down from 20 to 1.
  8. Print each element of an integer array.
  9. Count how many even numbers are in an array.
  10. Print the squares of numbers from 1 to 10.
Tip: Before writing a loop, decide the starting value, stopping condition, and update expression.

🧠 Quick Quiz

1. Which keyword is used to create a for loop in Java?




2. What does i++ do?




3. How many times does this loop run?

for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}