for loop in java

Let’s learn for loop in java.

for loop in java

for loop is used to iterate a block of statements multiple times.

Use for loop if you know the number of iteration is fixed.

Here are three types of loops in java with example,

  1. while loop
  2. do-while loop
  3. for loop

Loops in java executes a group of statements or instructions whilst some condition is true.

Syntax

for(initialization; condition; increment/decrement)
{
   statement(s).....
}

for loop in java

For loop has four parts variable declaration, condition, statements and increment/decrement variable. Here’s the flow of for loop.

  • Loop begins with an initialization which initializes the loop, and is executed only once.
  • Then condition in for loop is evaluated. If condition is true then statements inside for loop is executed. 
  • When condition is false, statements inside for loop do not execute and control gets transferred to statements after for loop.
  • After each iteration through the loop, increment/decrement is invoked and loop counter is updated.
  • Finally for loop control shifts to second step to evaluate the condition.

Example

public class ForLoop
{
   public static void main(String[] args)
   {
      for(int x = 1; x <= 5; x++)
      {
         System.out.println(x);
      }
   }
}


Output:

1
2
3
4
5


Also read – enhanced for loop example


Infinite for loop

Infinite for loop in java is called infinite, when condition inside for loop is always evaluated to true.

Syntax:

for ( ; ; )
{
   // code goes here
}


Example infinite for loop

public class InfiniteForLoop
{
   public static void main(String[] args)
   {
      for( ; ; )
      {
         System.out.println("Infinite for loop");
      }
   }
}


Output:

Infinite for loop
Infinite for loop
Infinite for loop
Infinite for loop
Infinite for loop…………….


Java for loop array

Here’s an example on looping array using for loop.

public class ForLoopArray
{
   public static void main(String[] args)
   {
      int[] arrNum = {6, 26, 25, 15};
      for(int a = 0; a < arrNum.length; a++)
      {
         System.out.println(arrNum[a]);
      }
   }
}


Output:

6
26
25
15


Nested for loop in java

Nested for loop is nothing but for loop inside another for loop.

Let’s learn how nested for loop work?

In nested for loop we have to manage condition, which is very important.

Also read – for-each loop in java

Because we have to see that inner for loop do not cross outer for loop.

NOTE: inner for loop executes faster than the outer for loop. Also we can have ‘n’ number of loops inside outer loop.

Now let’s see nested for loop in java example to create pattern as shown below.

public class NestedForLoop 
{
   public static void main(String[] args) 
   {
      int a, b;
      for(a = 1; a <= 5; a++)
      {
         for(b = 1; b <= a; b++)
         {
            System.out.print(b + "\t");
         }
         System.out.println();
      }
   }
}


Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5


For loop list

Now let’s learn how to iterate through java list using for loop.

import java.util.ArrayList;
import java.util.List;
public class ForLoopList 
{
   public static void main(String[] args) 
   {
      // creating list
      List<String> players = new ArrayList<String>(); 
      // adding 4 different values to list
      players.add("virat");
      players.add("dhoni");
      players.add("rohit");
      players.add("rahul");
      // iterating using for loop
      System.out.println("For loop list");
      for(int a = 0; a < players.size(); a++) 
      {
         System.out.println(players.get(a));
      }
   }
}


Output:

For loop list
virat
dhoni
rohit
rahul