For loop in java

Let’s learn for loop in java.

For loop in java

Loops in java executes a group of statements or instructions whilst some condition is true. Here are three types of loops in java with example,

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

In this post we are going to learn for loop. For loop is used when you have to repeat a task “n” number of times. That is, we know to print some statement.

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

Let’s practice loop programs,


Enhanced for loop example

Enhanced for loop traverse the elements or each element of an array sequentially. This loop is helpful in iterating array and collections.

Also read – java while loop example

Syntax

for(Type var: array)
{
   // code goes here
}

public class EnhancedForLoop
{
   public static void main(String[] args)
   {
      int num[] = {2,4,6,8,10};
      for(int even : num)
      {
         System.out.println(even);
      }
   }
}



Output:

2
4
6
8
10


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
}

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

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

Let’s learn how nested for loop work?

In nested for loop we have to manage condition, which is very important. Because we have to see that inner for loop do not cross outer for loop.

Also read – for each 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,

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