For loop in java

Let’s learn what is a 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 has four parts initialization of variables, condition, increment/decrement and statements. Here’s the flow of for loop.

for loop in java
  • Loop begins with an initialization where we initialize variables or variable.
  • Then condition which determines when the loop ends. The last part is increment also known as iterator step. In other words one execution of the loop step is known as one iteration.
  • In increment part we can increment by one or five or decrement by any number depending on the situation.
  • for loop iterates a block of code when the condition is true in steps and those steps are called iterations and loop stops when the condition is false.
  • Block of code is defined by curly braces in the loop body. We can have multiple statements. These multiple statements will execute every iteration.
  • Semicolons in for loop (after initialization, condition and increment/decrement) are required.

Java for loop 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


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. 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