Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - For Loop


The for loop in Java is like the while loop. But the for loop is way more powerful than the while loop.


The for loop has inbuilt features, by which you can reduce lines of code.


So far we have seen, how while loop was used to print "Hello World" 5 times.


Example :



public class MyApplication
{
    public static void main(String[] args)
    {
        int x = 1;
        while (x <= 5) {
            System.out.println("Hello World");
            x = x + 1;
        }
    }
}



The same program could be reduced to less lines of code using the for loop.


Let us see below.


Example of 'for' loop:


Let us use for loop to print "Hello World" five times on the screen.


Example :



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


Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World

Now, if we break the contents of for loop, we get :

  1. Initialisation part : It is run before the loop starts.


    Spring_Boot


    x is getting initialized with value 1.

  2. Conditional part : It states the condition for running the loop.


    Spring_Boot


    It says, continue to be in the loop until the value of x is not more than 5.

  3. Increment part : It is run each time after the loop.


    Spring_Boot


    x++ is the Increment part. i.e. Same as x = x + 1.

All together forms the for loop.


for(int x = 1; x <= 5 ; x++) {
	System.out.println("Hello World");
}

So in the 1st iteration,


1st Iteration


The control gets into the for loop and checks, if the value of x is less than or equal to 5.

Spring_Boot
Spring_Boot

In this case the value of x is 1. Which is less than 5.


So, the control enters the for loop.


for(int x = 1; x <= 5 ; x++) {
	System.out.println("Hello World");
}

and finds the print statement,


System.out.println("Hello World");

So, it prints Hello World.

Output :



  Hello World

Next, Java finds, it has come to the end of the loop.

Spring_Boot

So, it increments the value of x by 1 (And Java does the increment internally).


And the value of x becomes 2.

Spring_Boot

And the second Iteration starts.


2nd Iteration


Same way, the control comes to the for loop and checks, if the value of x is less than or equal to 5(i.e. Internally Java checks if i <= 5).

Spring_Boot

In this case the value of x is 2. Which is less than 5.


So, the control enters the for loop.

Spring_Boot

And finds the print statement,


System.out.println("Hello World");

So, it print Hello World for the second time.

Output :



  Hello World
  Hello World

Similarly, Java finds, it has come to the end of the loop.

Spring_Boot

And increments the value of x by 1.

Spring_Boot

Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.


3rd Iteration

Output :



  Hello World
  Hello World
  Hello World
Spring_Boot

4th Iteration

Output :



  Hello World
  Hello World
  Hello World
  Hello World
Spring_Boot

5th Iteration

Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World
Spring_Boot

Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.

Spring_Boot

In this case, the value of x is 6. Which more than 5.


And the control comes out of the for loop.


Now, if you see the output, Hello World is exactly printed 5 times.


So, we have seen, how to print Hello World 5 times.


Now, let us see, how can we print the numbers from 1 to 5.


Printing the numbers 1 to 5 using 'for' loop


Example :



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


Output :



  1
  2
  3
  4
  5

The above code is self explanatory.


However, we have provided the explanation below.


So in the 1st iteration,


1st Iteration


The control gets into the for loop and checks, if the value of x is less than or equal to 5.

Spring_Boot
Spring_Boot

In this case the value of x is 1. Which is less than 5.


So, the control enters the for loop.


for(int x = 1; x <= 5 ; x++) {
	System.out.println(x);
}

and finds the print statement,


System.out.println(x);

So, it print the value of x, which is currently 1.

Output :



  1

Next, Java finds, it has come to the end of the loop.

Spring_Boot

So, it increments the value of x by 1 (And Java does the increment internally).


And the value of x becomes 2.

Spring_Boot

And the second Iteration starts.


2nd Iteration


Same way, the control comes to the for loop and checks, if the value of x is less than or equal to 5.

Spring_Boot

In this case the value of x is 2. Which is less than 5.


So, the control enters the for loop.

Spring_Boot

And finds the print statement,


System.out.println(x)

So, it print the value of x, which is 2 currently.

Output :



  1
  2

Similarly, Java finds, it has come to the end of the loop.

Spring_Boot

And increments the value of x by 1.

Spring_Boot

Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.


3rd Iteration

Output :



  1
  2
  3
Spring_Boot

4th Iteration

Output :



  1
  2
  3
  4
Spring_Boot

5th Iteration

Output :



  1
  2
  3
  4
  5
Spring_Boot

Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.

Spring_Boot

In this case, the value of x is 6. Which more than 5.


And the control comes out of the for loop.


Example of 'for' 'each' loop:


We will be learning about it in the Arrays section of Java.