# The For Loop in Java

\$\$/\$\$

Let us now look at another kind of loop, the for loop.

\$\$/\$\$

## Video Transcript

Till now you have learnt how to use the while loop to solve the robot and bricks problem. Now we are going to take a look at this problem wherein you have to print the list of all the even numbers between one to 10. So for that you can use another kind of loop in Java, which we call as the for loop. So basically the for loop in Java is useful in those cases in which you need to repeat a certain action. Now here again, you have to tell the computer to check for all numbers between one to 100. And in case a number is even, then you need to print it. So basically, in order to write a for loop for this, you can say that for this is the keyword for used in Java, you say for. Then you need to tell the for loop where you would be beginning executing this for loop. From what point? So in the beginning you need to start from this one. This one will go on from one, two, three till 100. So in the beginning I have to initialize a variable starting from one. Let me call it my variable. Int I is equal to one. This denotes where my starting variable is going to begin at at the beginning of this for loop, then I put a semicolon and then I have to tell this for loop till what point do I need to keep on executing this for loop? So I say that for int I equal to one and semicolon I less than equal to 100. That is, till the time I does not become 100. Till that time, keep on incrementing I by one. So notice carefully the condition within this curved braces. What happens is that in the beginning you are specifying the point where to begin. You specify that please begin from int I equals to one and then keep on repeating this loop till you reach a point where I becomes 100. And after each iteration or after each step keep increasing I by one. This increment can be denoted by I plus plus. Now, what will happen inside each iteration that you have to specify within curved braces? So I open a braces here and then I say that if I need to specify that if my number is even then printed. So I say that if I percent two is equal to equal to zero, that is the remainder. When I divide I by two is zero, then this means that it is an even number and I print i. This basically completes my if condition. Now, do you think I need to increment my I variable now in order to move on to the next step? Well, no. This for loop basically ensures that I gets incremented automatically on its own after this step is over. Because I have specified here I plus plus explicitly I don't need to increment my I from one to two. So now this for loop is complete so let us run through it and see. In the beginning my I will be one and I is less than equal to 10 is also true so then what will happen? It will check that if I person two is equal to is equal to zero so basically is one person two is it equal to zero? Well no, on dividing one by two, the remainder is not zero so then one will not be printed and my I will now move on from one it goes till two, it gets incremented to two. Then in the next iteration again it checks that if two person two is equal to is equal to zero, that is if two is divisible by two since it is true it prints two on the console so basically if this is my console, two will get printed here. Now again this I plus plus ensures that in the next step two increments from two to three. Then again it goes inside this for loop and checks this if condition it checks if three is divisible by two. Now since the remainder when three is divided by two is one which is not equal to zero so three is not printed here again in the next step three gets incremented till four. This if condition will be checked for the remainder when four is divided by two, it is indeed zero because four is divisible by two. So four will be printed here on the console. So in this way this process keeps on getting repeated till I reach my ending state and what will be my ending state? My ending state will be when my I becomes equal to 100. So basically this condition holds true. So then what happens is that if 100 is divisible by two, then print 100. So basically in the last step 100 is going to get printed here on the console. Now in the next step when this I plus plus happens that is 100 gets incremented 201. So basically the condition enclosed within these curved braces of the for loop no longer holds true because I is no longer less than equal to 100. So this entire piece of code no longer gets executed and the while loop terminates. So in this way you have seen that you first need to initialize your starting point. You need to specify the ending point where you want the for loop to end. After that you need to specify what will happen after each iteration. So I must increase from one to the next value. So we say I plus plus and then inside you can do whatever you want, that is you can check. In this case we wanted to print even numbers so we say we check for each and every number whether it is even and if it is, then we print it. So this is how you can use the for loop to print the list of all the even numbers between one to 10.

## Video Recap

• Learn to print a list of even numbers between 1 to 10 using Java's for loop.

• Initialize a variable to start the loop and specify the ending point.

• Use the "if" condition to check if a number is even or not.

• Increment the variable using "i++" after each iteration.

• The for loop automatically increments the variable, no need to increment it manually.

• The loop terminates when the condition is no longer true.

You saw how similar a for loop is to a while loop. You will be using this loop quite often from now on. Let us now try to solve the same problem using a while loop.

\$\$/\$\$

Now you know about two types of loops in Java. In the next segment, you will look at when should you use each of these loops and how they can be used interchangeably in some cases.