for loop in java

In this post, we will see what is for loop in java and java for loop example. Actually this is the basic most widely used loop in java.
The for loop in java is a part of Control statements. If a user wants to execute the block of code several times. It means the number of iterations is fixed. Then you should use the for loop java. JAVA provides a concise way of writing the loop structure. It consists of four parts:

1. Initialization
2. Condition
3. Body of for loop
4. Increment/decrement

for(initialization; condition; increment/decrement)
{
   // Body of for loop
}

1. Initialization: Initialization is the first part of the loop and it happens only one time. It marks the start point of the loop. Here, you can declare and initialize the variable, or you can use any already initialized variable.
2. Condition: Condition is the second part of the loop and we must provide/write a condition that must return boolean value either true or false. It is executed each time to test the condition of the loop. It continues execution until the condition is false.
3. Body of for loop: It contains a block of code that executes each time after evaluating the condition true.
4. Increment/decrement: It is used to update the index value of the variable.

Flowchart of for loop java
public class MyExample
{
    public static void main(String[] args)
    {
        for(int i = 1;  i <= 10 ; i++)
        {
            System.out.println(i);
        }
    }
}

Explanation: In this example, we want to print the table of 1 so we used for loop. Here, we declared a variable:

int i = 1;

It is the initialization part of the loop. It is the start point of the loop to print the count, so we initialized it with 1.

i <= 10;

It is the condition of for loop which will be tested/evaluate each time before execution of statements of body.

System.out.println(i);

It is the body of the loop which will execute only if the condition of loop returns true.

i++;

It is an increment/decrement part in a loop. In this example, we used the unary operator to change the index value of the variable. It will increment the value of “i” each time.

NOTE: After incrementing the value of “i” compiler again test the condition of for loop.

Flowchart of for loop java

Nested for loop in java

We can use one for loop inside the other for loop and this is called nested for loop. The JVM can enter in the inner for loop if the condition Outer for loop is true. otherwise, it will skill the inner for loop. The nested for loop used to work with two dimensions. The outer loop is used for Row and the inner loop used for the Column. The inner loop restarted each time when outer loop changes the iteration. We will see it step-by-step execution.

for(initialization; condition; increment/decrement)
{
   // Body of outer loop
   for(initialization; condition; increment/decrement)
   {
	// Body of inner loop
   }
}

Step 1: First of all, the compiler initializes the variable for outer for loop, and then it checks the condition outer for loop.

  • If the given condition returns True, then the compiler enters inside the outer for loop. Now Go to, Step 2.
  • If the given condition returns False, then the compiler doesn’t enter inside the loop and exists from the Outer Loop. will exit from For Loop.

Step 2: After entering the outer loop, the compiler starts the execution of the statement. When it finds inner for loop then it checks the condition of inner for loop.  

  • If the given condition of inner for loop returns True, then the compiler enters inside the inner for loop and executes the statement.
  • If the given condition returns False, then the compiler doesn’t enter in the inner loop and exit from the inner For Loop.

Step 3: After exits from inner for loop, the compiler repeats the Step 1.

Let’s take a simple example of nested for loop and see how does it work. In the first example, we will try to print the iterations of the outer loop and inner loop. Let’s see nested loop example

public class MyExample 
{
	public static void main(String[] args) 
	{
		// Outer for loop
		for(int i = 1;  i <= 3 ; i++)
        {
		   // Inner for loop
           for(int j = 1; j <= 3 ; j++)
           {
                System.out.println("Iteration of outer loop i: "+ i +"  Iteration of inner loop: "+ j );
           }
        }
	}
}

Output: Iteration of outer loop i: 1 Iteration of inner loop: 1
Iteration of outer loop i: 1 Iteration of inner loop: 2
Iteration of outer loop i: 1 Iteration of inner loop: 3
Iteration of outer loop i: 2 Iteration of inner loop: 1
Iteration of outer loop i: 2 Iteration of inner loop: 2
Iteration of outer loop i: 2 Iteration of inner loop: 3
Iteration of outer loop i: 3 Iteration of inner loop: 1
Iteration of outer loop i: 3 Iteration of inner loop: 2
Iteration of outer loop i: 3 Iteration of inner loop: 3

Explanation: In this example, We are using the nested for loop concept because we have two for loops, one is an outer loop and another is an inner loop.

The Outer for loop runs 1 time and perform 3 iterations but the inner for loop run 3 times and perform 9 iterations. The inner for loops re started it execution when outer for loop increment itself.

Infinite for loop: In infinite for loop the condition always returns true. Sometimes we write the condition that makes a for loop infinite.

public class ExampleOfInfiniteForLoop
 {  
	public static void main(String[] args)
	{  
	     for( int i = 0;  i >= 0 ; i++)
	     {
	    		 System.out.println(i);
	     }
	}  
}  

You can see the importance of Boolean expression and increment/decrement operation co-ordination. The initialization part set the value of variable i = 0, the Boolean expression always returns true because i is incrementing by 1. This would eventually lead to the infinite loop condition.

for( ; ; )
{
   // Body of for loop
}

1. Quiz, Read the below code and do answer.

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

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

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

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

public class ExampleOfForLoop
{  
   public static void main(String[] args)
   {
       int i = 0;
       for(; ;)
       {
          System.out.println(i);
          i++;
       }
       
   }  
}

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

public class ExampleOfForLoop
{  
   public static void main(String[] args)
   {
       for(int i = 1; i <= 3 ; i++)
       {
           for(int j = 1; j <= 3 ; j++)
           {
              System.out.print(j+" ");
           }
          System.out.println(" ");
       }
       
   }  
}

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

public class ExampleOfForLoop
{  
   public static void main(String[] args)
   {
       for(int i = 0; i < 10 ; i++)
       {
          System.out.println(i++);
       }
   }  
}

Click on anyone to know the answer.

6. Quiz, Read the below code and do answer.

public class ExampleOfForLoop
{  
   public static void main(String[] args)
   {
       for(Integer i = 0; i < 5 ; i++)
       {
          System.out.println(i++);
       }
   }  
}

Click on anyone to know the answer.

Leave a Comment