Home » Java Tutorial » For loop Java with Examples

For loop Java with Examples

In coding, “for” loop is used to reduce the redundancy in the code. When a same line of code needs to be written for so many times, then it will lead to very large code and which won’t be a good practice and also it will make the code more complex. For loop Java is used to make the code less complex.

It will save time as well because when a single line of code is to be written for a large number of times like more than a hundred times, it would be very time-consuming. So, for solving this problem we have for loop java which will reduce the code and will make code more readable and simpler.

When to use for loop java?

Java for loop is advised to use when the number of iterations is fixed. As there are other loops that can be used in place of java for loop. But when the number of iterations is known then for loop would be a better choice.

How for loop java works?

For loop is an entry control loop, which means that the condition is checked at the entry point of the loop and if that condition is satisfied then only the statements in the for loop is executed. Once all the statements are successfully executed inside the for loop, the condition specified at the entry point is again checked and if the condition is again satisfied, then again, the statements of the loops are executed. This cycle continues until the condition specified at the entry point is not satisfied.

Different ways of using java for loop

1. Simple for loop

2. Enhanced for loop (also known as for each loop)

Simple for loop java

Simple for loop is made up of generally four components which are Initialization, Condition, Increment/Decrement, and Statement.

Let us first see the syntax of the for loop and where these four components lie.


for(Initialization; Condition; Increment/Decrement)

Now let us discuss each component in detail:

1. Initialization

It is an optional condition, where you can place the initial condition which will be executed first before entering the loop. So, here we can initialize the variable or can use the already initialized variable.

2. Condition

This is also an optional condition where the specified condition will be checked every time before entering the loop. So, the for loop would be executed until the condition is not False as it will return a boolean value.

3. Increment/Decrement

Here we can specify the variable increment or decrement so the values of the variable will be updated every time the loop runs.

4. Statement

The statement is in the body part of the loop which will contain the statements which will be executed until the for loop runs and the condition specified in the for loop is not False.

Infinite for loop

If we do not specify initialization, Condition, and Increment/Decrement then that would result into infinite for loop.

        	// Statements

Example of simple for loop

class Ex1
        	public static void main(String args[])
    	             	System.out.println("Program to display numbers from 1 to 5");
                    	// Initializing variable num with 1 and setting the condition
    	             	for(int num=1; num<6; num++)
        	                     	// Displaying the value of num variable
System.out.println("The value of num is: "+num);


The value of num is: 1
The value of num is: 2
The value of num is: 3
The value of num is: 4
The value of num is: 5

So, in the above example, we used the variable value and incremented it using for loop and displayed it every time. The flow of the following code will be:

The variable num will be initialized with value 1, then the condition is checked which will be true for the first time where 1<6. After that, the statements inside the loop will be executed and then the value will be incremented by 1. And again, the same process will be followed until the condition is false.

Nested For loop

When there is a for loop present inside of a for loop, then it is known as nested for loop. It will be clearer with the help of this fun code of a pattern.

class Ex2
        	public static void main(String args[])
   	              	System.out.println("Program to display numbers from 1 to 5");
    	             	for(int i=1; i<6; i++)
        	                     	for(int j=1; j<=i; j++)


* *
* * *
* * * *
* * * * *

Here we used two for loops where the other for loop is inside the first for loop. So, first for loop will be used as the number of rows and for each row how many stars are needed is specified with the help of inner for loop.

Enhanced for loop java / For each loop java

This enhanced for loop java which is also known as for each loop java is used to traverse through the array. So, it will be easier to traverse an array or collection in java then the simple for loop as we do not need to increment the counter. The traversing will return elements one by one and no index values will be used in for each loop java.


for(Type var:array_name)
//code to be executed 

Here we will specify the datatype and also a variable that will be used to store the elements of the array. That element will be used with the help of the variable for that particular pass.


// Enhanced For loop java example  

// program to display the elements of the array 

public class Ex3
public static void main(String[] args)
                    	 // Declaring array 
                    	int arr[]={11,22,33,44,55}; 
                    	// traversing the array and printing the elements
                    	for(int i:arr)



In the above example, we created an array of type integer with five integer value, and then we used enhanced for loop java for traversing it and displayed the elements as they are traversed.

Do you know?
1. Java Scanner with Examples
2. Java Stack with Examples
3. ParseInt Java
4. Try Catch JAVA
5. For loop Java with Examples
6. Java vs Javascript
7. Java Iterator with Examples
8. Substring Java
9. Java Switch Statement with Examples
10. Java Queue

Pin It on Pinterest