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.
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.
Let us use for loop to print "Hello World" five times on the screen.
public class MyApplication { public static void main(String[] args) { for(int x = 1; x <= 5 ; x++) { System.out.println("Hello World"); } } }
Now, if we break the contents of for loop, we get :
All together forms the for loop.
for(int x = 1; x <= 5 ; x++) { System.out.println("Hello World"); }
So in the 1st iteration,
The control gets into the for loop and checks, if the value of x is less than or equal to 5.
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.
Next, Java finds, it has come to the end of the loop.
So, it increments the value of x by 1 (And Java does the increment internally).
And the value of x becomes 2.
And the second Iteration starts.
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).
In this case the value of x is 2. Which is less than 5.
So, the control enters the for loop.
And finds the print statement,
System.out.println("Hello World");
So, it print Hello World for the second time.
Similarly, Java finds, it has come to the end of the loop.
And increments the value of x by 1.
Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.
Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.
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.
public class MyApplication { public static void main(String[] args) { for(int x = 1; x <= 5 ; x++) { System.out.println(x); } }
The above code is self explanatory.
However, we have provided the explanation below.
So in the 1st iteration,
The control gets into the for loop and checks, if the value of x is less than or equal to 5.
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.
Next, Java finds, it has come to the end of the loop.
So, it increments the value of x by 1 (And Java does the increment internally).
And the value of x becomes 2.
And the second Iteration starts.
Same way, the control comes to the for loop and checks, if the value of x is less than or equal to 5.
In this case the value of x is 2. Which is less than 5.
So, the control enters the for loop.
And finds the print statement,
System.out.println(x)
So, it print the value of x, which is 2 currently.
Similarly, Java finds, it has come to the end of the loop.
And increments the value of x by 1.
Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.
Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.
In this case, the value of x is 6. Which more than 5.
And the control comes out of the for loop.
We will be learning about it in the Arrays section of Java.