Join our newsletter for the latest updates.
Java for Loop

Java for Loop

In this tutorial, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming.

In computer programming, loops are used to repeat a specific block of code until a certain condition is met (test expression is false). For example,

Imagine we need to print a sentence 50 times on your screen. Well, we can do it by using the print statement 50 times (without using loops). How about you need to print a sentence one million times? You need to use loops. With loops, we can simply write the print statement one time and run it for any number of times.

It's just a simple example showing the importance of loop in computer programming.


Java for Loop

The syntax of for Loop in Java is:

for (initialization; testExpression; update)
{
    // codes inside for loop's body
}

Working of for loop

  1. The initialization expression is executed only once.
  2. Then, the test expression is evaluated. Here, test expression is a boolean expression.
  3. If the test expression is evaluated to true,
    Codes inside the body of for loop is executed.
    Then the update expression is executed.
    Again, the test expression is evaluated.
    If the test expression is true, codes inside the body of for loop is executed and update expression is executed.
    This process goes on until the test expression is evaluated to false.
  4. If the test expression is evaluated to false, for loop terminates.

for Loop Flowchart

Java for Loop flowchart
Working of for loop

Example 1: for Loop

// Program to print a sentence 10 times

class Loop {
    public static void main(String[] args) {
      
        for (int i = 1; i <= 10; ++i) {
            System.out.println("Line " + i);
        }
    }
}

Output:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

In the above example, we have

  • initialization expression: int i = 1.e
  • test expression: i <=10
  • update expression: ++i

Here, initially, the value of i is 1. So the test expression evaluates to true for the first time. Hence, the print statement is executed. Now the update expression is evaluated.

Each time the update expression is evaluated, the value of i is increased by 1. Again, the test expression is evaluated. And, the same process is repeated.

This process goes on until i is 11. When i is 11, the test expression (i <= 10) is false and the for loop terminates.

To learn more about test expression and how it is evaluated, visit relational and logical operators.


Example 2: for Loop

// Program to find the sum of natural numbers from 1 to 1000.

class Number {
    public static void main(String[] args) {
      
        int sum = 0;
	   
        for (int i = 1; i <= 1000; ++i) {
            sum += i;     // sum = sum + i
        }
	   
        System.out.println("Sum = " + sum);
    }
}

Output:

Sum = 500500

Here, we have a variable named sum. Its initial value is 0. Inside for loop, we have initialized a variable named i with value 1.

In each iteration of for loop,

  • the sum variable is assigned value: sum + i
  • the value of i is increased by 1

The loop continues until the value of i is greater then 1000. For better visualization,

1st iteration: i = 1 and sum = 0+1 = 1
2nd iteration: i = 2 and sum = 1+2 = 3
3rd iteration: i = 3 and sum = 3+3 = 6
4th iteration: i = 4 and sum = 6+4 = 10
... .. ...

999th iteration: i = 999 and sum = 498501 + 999 = 499500
1000th iteration: i = 1000 and sum = 499500 + 1000 = 500500

infinite for Loop

We should be always careful while working with loops. It is because if we mistakenly set test expression in such a way that it is never false, the for loop will run forever.

This is called infinite for loop. For example,

// Infinite for Loop

class Infinite {
    public static void main(String[] args) {
      
        int sum = 0;

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

Here, the test expression (i <= 10) is never false and hello is printed infinite number to times (at least in theory).

Note: The initialization, update and test expression used in for statement is optional. Here's another example of the infinite for loop.

for ( ; ; ) {

}

Java for-each Loop

In Java, there is an alternative syntax of for loop to work with Java arrays and Java collections (known as for-each loop). For example,

for (int a : array) {
    System.out.println(a);
}

To learn more, visit: Java for-each Loop