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 ones 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 is as follows:
- Initialization: It starts with the initialization statement. So, counter variables are initialized 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 Java for 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 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 must put the semicolon (;) in place; otherwise, it will throw a compilation error.
Initialize the counter variable can skip from Java for loop, as shown below:
int i = 1; for( ; i <= 20; i++)
If you observe the above code snippet, the counter variable is declared in the previous line. Like the 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 Java for loop code snippet, the increment part is 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++)
The 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( ; ; )
The increment and decrement operator section initialization uses a comma to separate multiple declarations. To separate the test conditions, you must use logical operators to join conditions.
Java For loop Flow Chart
The screenshot below will show the flow chart of For Loop in Java Programming language.

The execution process of Java for loop is:
- Initialization: We initialize the counter variable(s) here.
- Test condition: It will check the condition against the counter variable. If the condition is True, the compiler will execute the statements inside. If the condition is False, then it will exit.
- After completing the iteration, it will execute the Increment and Decrement Operator inside the Java for loop to increment or decrement the value.
- Again it will check the expression after the value is incremented. The statements inside this will execute as long as the condition is true.
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’s entered number using the Java for loop.
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); } }

The first statements in this program will ask the user to enter any integer value below 10. Next, we assign the user entered value to the integer variable (number)
Next line, we use the Java For loop, and the Condition inside it will make sure that the value of i is less than or equal to the 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 evaluate 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, 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.
The last System statement will print the number of digits present in the given number as output.