Java For Loop

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

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: Java For loop starts with the initialization statement. So, initialization of counter variables is done first (For example counter = 1 or i = 1). The initialization section is executed only once at the beginning of the for loop.
  • Test Condition: The value of the counter variable will test against the test condition. If the result is True, the Javac compiler will execute the statements inside the For loop. If the condition fails, then For loop 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. Please refer to Increment and decrement operators in Java article to understand the operator.

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 for loop, we have to put the semicolon (;) in place; otherwise, it will throw compilation error.

Initialize the counter variable can skip in the Java for loop, as shown below:

int i = 1;

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

If you observe the above code snippet, the counter variable declared before the for loop. Like initialization, 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 Java code snippet, the increment part declared in the for loop body. For loop 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 in for loop. 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( ; ; )

For initialization, the increment and decrement operator section uses a comma to separate multiple declarations. To separate the test conditions in java for loop, you must use logical operators to join conditions

Flow Chart of a Java For loop

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

Java For Loop FLOW CHART

The execution process of the Java for loop is:

  1. Initialization: We initialize the counter variable(s) here. For example i = 1.
  2. Test condition: It will check for the condition against the counter variable. If the condition is True, the compiler will execute the statements inside the for loop. If the condition is False, then it will exit from the loop
  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 for the condition after the value incremented. As long as the condition is True, the statements inside the for loop will execute.

Java For Loop example

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

// Java For Loop example
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 For Loop is: %d ", sum);
	}
}
Java For Loop 1

In this Java for loop program, the following 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)

System.out.println("\n Please Enter the any integer Value: ");
number = sc.nextInt();

Next line, we used the For loop, and the Condition inside the For loop 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 Java for loop example, User Entered value: Number = 5 and we initialized the sum = 0

First Iteration

Within the For Loop, we initialized the i value as 1. Remember, Initialization happens only once. Next, the compiler will check for the condition (i <= number), which is TRUE. So, the statement inside the for loop will execute.

sum = sum * i

sum = 1 * 1 ==> 1

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

Second Iteration

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

sum = sum * i

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 Javac compiler will check for the condition (i <= number), which is TRUE.

sum = sum * i

sum = 2 * 3 ==> 6

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

Fourth Iteration

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

sum = sum * i

sum = 6 * 4 ==> 24

Next, number is increment by 1 (number ++).

Fifth Iteration

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

Next, the compiler will check for the condition (i <= number), which is TRUE. So, statement inside the for loop executed.

sum = sum * i

sum = 24 * 5 ==> 120

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

Here i = 6. So, the condition present in the for loop ( 6 <= 5) will fail

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

System.out.format(" Sum of the Numbers From the For Loop is: %d ", sum);