Java For Loop

The Java For loop is used to repeat a block of statements with the given number of times until the given condition is False. The for loop is one of the most used one in Java or any other programming language. Let us see the syntax:

Java For loop Syntax

The syntax of the For Loop in Java Programming language is as follows:

for (initialization; test condition; increment/decrement operator)  {
       Statement 1
       Statement 2
       ………
       Statement n
}

If you observe the above syntax, in Java for loop, there are three expressions separated by the semi-colons (;) and the execution of these expressions are as follows:

  • Initialization: It starts with the initialization statement. So, initialization of counter variables is done first (Example counter = 1 or i = 1). The initialization section is executed only once at the beginning.
  • Test Condition: The value of the counter variable will test against the test condition. If the result evaluates to True, the compiler will execute the statements inside the body of the loop. If the condition is false, it will terminate.
  • Increment and decrement operator: This expression will run after the end of each iteration. This operator helps to increase or decrease the counter variable of Java for loop as per our requirement.

Java For Loop initialization

The Java For loop has the flexibility to omit one or more sections from the declaration. Although we can skip one or more sections from the declaration, we have to put the semicolon (;) in place; otherwise, it will throw compilation error.

Initialize the counter variable can skip, as shown below:

int i = 1;

for( ; i <= 20; i++)

If you observe the above code snippet, the counter variable declared in the previous line. Like initialization condition, we can also skip the increment part of the Java For loop.

int i = 1;

for( ; i <= 10; )  {
    //statements     
    i++;
}

If you observe the above code snippet, the increment part declared in the body. It also allows us to initialize more than one counter variable at a time with comma separate:

for(i = 1,j = 10; i < j; i++)

Java For loop also allows using multiple conditions. Instead of using a comma, we have to use the logical operator to separate the two conditions.

for(i=1,j=20; i <= 10 && j >= 20; i++) {
   //statements
   j++;
}

Like the test condition, Java for loop allows us to use more than one increment operator as follows.

for(i = 1, j = 1; i <= 10 && j <= 10; i++, j++)

Infinite For loop

for( ; ; )

Initialization, the increment and decrement operator section uses a comma to separate multiple declarations. To separate the test conditions, you must use logical operators to join conditions

Flow Chart

The screenshot below will show the flow chart of For Loop in Java Programming language.

Java For Loop FLOW CHART

The execution process is:

  1. Initialization: We initialize the counter variable(s) here.
  2. Test condition: It will check the condition against the counter variable. If the condition is True, the compiler will execute the statements inside it. If the condition is False, then it will exit.
  3. After completing the iteration, it will execute the Increment and Decrement Operator inside the Java for loop to increment or decrement the value.
  4. Again it will check the expression after the value incremented. As long as the condition is True, the statements inside this will execute.

Java For Loop example

This program allows the user to enter any integer values. Then it will calculate the sum of natural numbers up to the user entered number.

package Loops;

import java.util.Scanner;

public class ForLoop {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int i, number, sum = 1;
		sc = new Scanner(System.in);	
		
		System.out.println("\n Please Enter the any integer Value: ");
		number = sc.nextInt();
		
		for (i = 1; i <= number; i++)  {
			sum = sum * i;
		}
		System.out.format(" Sum of the Numbers From the is: %d ", sum);
	}
}
Java For Loop 1

In this program, the first statements will ask the user to enter any integer value below 10. Next, we are assigning the user entered value to the integer variable (number)

Next line, we used the Java For loop, and the Condition inside it will make sure that the value of i is less than or equal to user-specified Number.

for(i = 1; i <= number; i++)  {
	sum = sum * i;
}

In this example, User Entered value: Number = 5 and we initialized the sum = 0

First Iteration

Within this, we initialized the i value as 1. Remember, Initialization happens only once. Next, the compiler will check the expression (i <= number), which is TRUE. So, the statement or block of code inside it will execute.

sum = sum * i

sum = 1 * 1 ==> 1

Next, i value will incremented by 1 (i++). Please refer to Increment and Decrement Operators article to understand this ++ notation.

Java for loop Second Iteration

Within the first Iteration, the values of i changed as i = 2. Next, it will evaluates the condition (i <= number), which is TRUE. So, the statement inside it will execute.

sum= 1 * 2 = 2

Next, the number will increment by 1 (number ++).

Third Iteration

Within the second Iteration, the values of both i and sum changed as i = 3 and sum = 2. Next, the compiler will check the condition (i <= number), which is TRUE.

sum = 2 * 3 = 6

Fourth Iteration

Within the third Iteration, the values of i = 4 and sum = 6. Next, the compiler will check the condition (i <= number), which is TRUE. So, the statement inside it will execute.

sum = 6 * 4 = 24

Fifth Iteration

Within the fourth Iteration of this for loop java, the values of i = 5 and sum = 24.

sum = 24 * 5 = 120

I = 6. So, the condition present in the code ( 6 <= 5) evaluates to false.

Last System statement will print the number of digits present in the given number as output.