Latest :

Java For Loop – Tutorial With Examples | Loops

Java for loop tutorial with examples and complete guide for beginners. The below article on Java for loop will cover most of the information, covering all the different methods, syntax, examples that we used in for loops.

What Are Java Loops – Definition & Explanation

Executing a set of statements repeatedly is known as looping. We have 3 types of looping constructs in Java. These looping statements are also known as iterative statements.


2.for while

  • The above three different java loops are used primarily with same purpose and the difference is in their syntax.
  • Because of the syntactical differences, their behavior may differ a little bit. We will see the differences soon.

For Loop In Java & Different Types

Java For Loop, is probably the most used one out of the three loops. Whatever we can do for a while we can do it with a Java for loop too (and of course with a do-while too).

There are two kinds of for loops

1. Normal for loop

2. For each style of for loop

For Loop In Java Different Types And Explanation:

1.Normal for loop



  • When control comes to a Java for loop, it executes the initialization part first. This part is executed only once.
  • The one-time activities associated with the loop (that too at the beginning) are done here.
  • Then control moves to condition part. If the condition results in true, the control enters the body.

If it results in false the control comes out of the java for loop and executes the statement immediately following the loop.

When the condition is true, the loop body is executed. This is where the actual logic is executed. Then control comes to incr/decr part. Here, generally, the control variables are incremented/decremented.

After executing the incr/decr part, the control comes to condition part. If it results true, the body is executed, then comes to incr/decr and then to condition part. This process is repeated as long as condition results in true.



In the above example, in the initialization part, the variable a is initialized with 1.

This activity takes place only once. Then it moves to condition a<=3 which results in true.

So it comes to loop body to print “drink more water” and then it moves to increment a value by 1 so that a becomes 2. Then it comes to condition part, results in true, print the message and comes back to increment to make a value 3. Once again the condition is true, so prints the message in the body and then comes to make a value 4.

Then the condition results in false (as 4<=3 is false) and comes out to execute the statement after the loop.

  • In a Java for loop, initialization is executed only once irrespective of a number of times the loop is executed.
  • The condition is checked N+1 times where N is the number of times the body is executed.
  • Incr/decr part is executed N times (same as the number of times the body is executed).

Example Program:


When we create a variable at initialization part of the for loop that variable is automatically dead when the loop ends (in C, we cannot create a variable at initialization part of the for loop).

Such variable cannot be used outside the loop. If we want we can create a variable a fresh with the same name outside.


int x=len;             is not valid as len is dead.

int len=p;             is valid, as len is treated as a fresh variable




Example Program:




Example: 2



  1. We can have any number of statements in a for loop body.
  2. All of them should be enclosed within curly braces.
  3. If we have only one statement, then the curly braces are optional.




The initialization part can have any number of statements, but all of them should be separated by a comma.

When initialization ends, then that part should end with a semi-colon. In the following example, three statements (a=10), (b=20) and (c=30) are placed in the initialization part separated by commas.


Example Program:


  • Similar to initialization part, the incr/decr part can also have multiple statements separated by commas.



Example Program:


But the condition part should not be separated by commas.

All supposed conditions should be combined using AND or OR operators to make it a single condition (in C, we can have multiple conditions separated by commas and truth value of the last condition is considered as truth value of condition part).


for(a=2; a<=b && a<=c; a++)       is valid

for(a=3; a<=b, a<=c; a++)             is not valid

for(a=4; a>=b || a>=0; a–)            is valid


The condition should always result in a boolean value. Other types of values are not allowed.


for(int a=5,b=6; a<=b ; a++)        is valid

for(int a=7,b=8; a+b; a++)            is not valid

Generally, the header part of the for loop decides (with initialization, condition, and incrementation) how many times the loop will be executed and the actual logic is placed in the body.

In case the header part itself has the actual logic implicitly then the body may not be required. In that case, we can have a dummy body. We have two examples below, the first one has a set of curly braces without any code inside. In the second example, just a semi-colon is the body of the loop.



Example Program:




2. Nesting for loop

  • We can have a for loop inside another for loop. This is known as nesting.


In this example, first control comes to initiation part of outer for loop.

The explanation for the Above Example:

Their value of a becomes 11.

Then it comes to its condition part.

If the condition is true, it enters the loop body and executes statementA.

Then control comes to initialization part of inner loop and then to condition part.

If the condition part is true, it enters the body of loop body and executes statementB.

And then it goes to incrementation part of inner for loop and then to condition part of the same (inner) loop.

This (inner loop) is repeated as long as the inner condition is true.

Example Program:



We can place any construct inside another a for loop. There is no limit on the number of constructs we can place in.


To come out of a for loop from the middle of the body, we can use a break. The break will take the control out of the loop without regard to a condition in the header.


If we want to go the next iteration of a loop from the middle of the body (by skipping the remaining statements of current iteration) then we can use continue.


Even though it is named to be initialization, condition and incrementation parts, we can write any statement in that part. The condition part should result in a boolean value. At initialization, we can create variables but not at incrementation part. The following code is valid.

Sometimes the initialization takes place by the time control comes to the for loop, in that case, we can write a dummy statement (just a semi-colon) at initialization part. Similarly, we can skip the incrementation part. If we use a dummy condition (just a; ) then it is treated as true. So all the following usages are valid.






If we write a for loop like for(;;) and there is no break or return kind of statements then that loop becomes an infinite loop. The following are infinites.

Eg1:        for( ; ; )

Eg2:        for(init; true; incr)


2.Foreach style of for loop



These kinds of loops are used to travel through a set of elements strictly in an order (from beginning to ending). The Set of elements can be an array, an ArrayList, a LinkedList or any other collection.

Suppose we have an array and wanted to print each element of the array we can write a normal loop like the following.


In this example, the loop is repeated for 4 times (number of elements in the array) and each time ith element (from 0th to 3rd) is printed.

To get the same result, we can use the following notation (for each style).


int a[]={10,20,30,40};

for(int x : a)


In this case, the variable x gets each value of the array (one value per iteration) and it is printed. Relatively this approach (foreach) is comfortable to visit all elements of the list. If we want parts of the list then we can use the normal for loop.

The following example gives another example of using for each notation to travel through an ArrayList.


Similar to normal loop, we can come out of this loop also using a statement like break;

To travel through a two dimensional array we can write a code like the following.

In the above example, the outer loop travels through an array of references (where each reference will refer an array) and the inner loop travels through each element (of the current array).



Check Also

Convert String To Date In Java – JavaTutoring

Convert String to Date In Java – Here we cover the different ways to convert ...