Learn how to use for loop in java with this tutorial. Java for Loop is used in programming to execute a set of codes repeatedly until the condition is true. There are many problems and real examples can be created using the loop.

Java For Loop

For Loop contains the three arguments in the for function. The first argument contains the initialization of the variable as per your need. The second argument contains the condition to make true or false until you want to execute the statement inside the loop. The final argument contains the variable with increment and decrement operator.

Below is the syntax to create your own for loop and use in your programming.


Let’s see each type of for loop programming with some simple examples given below.

Java For Loop Examples

you can create simple for loop, infinite for loop, for loop iteration and for-each loop on array elements. Each section contains the useful codes with the result in the output.

Let’s learn each for loop examples and analyze the output to understand the working of the loop.

Simple Java For Loop Example

A simple example contains the simple for loop to print the numbers from 0 to 9. The for loop given below iterate repeatedly for 10 times and print the value using the ‘println’ statement.

Each iteration output prints in the next line and there are 10 lines to print one output in each.




The above example executes the code repeatedly until the value of i is less than 10. An increment operator is using here to increase the value of variable i for each iteration.

Java For Loop to Iterate Through an Array Example

The array is a homogeneous collection of data which you can iterate and print each element using the loop. To iterate each element and print, you need to use condition variable less than the array length as given below example.




Each element of an array is print in a single line. The output in the above example contains the five array items prints in five lines one by one.

Java For-each Loop Example

This is the simple way of iterating through each element of an array. You can call this a for each loop method of an array. In this method, you have to use the array variable name inside the for function with other variables which you have to declare an integer.

Below is the example contains the array with five items. It gives the output same as the output you have in the above-given example.




Check the above-given example and understand the loop in this. The declared variable i starts the value from 0 and ends with the length of the array.It gives the output as arr[0],arr[1]….arr[4].

Infinite Java For Loop Example

An infinite loop is a loop that contains the condition that never can be false and the iteration performs repeatedly for infinite times. However, you can stop the infinite loop by using the break statement inside the loop and put an if condition if the match will break the loop.


In addition to the above infinite loop, you can also create an infinite loop by using nothing inside the for function. Leave each parameter blank in the for function creates a for loop that executes the code for infinite times.


You can also stop the execution of the statement inside the infinite loop using the break statement inside the loop. Put the condition in the if statement, which only follows when the condition is true and break the loop.

You must also read.