 Hello friends! I am Sanjay Gupta. I welcome you on my channel. You can watch various programming related videos on my channel. You can search it. You can subscribe it for updates. It contains more than 800 videos. You can search the keyword programming by Sanjay Gupta on YouTube to watch these videos. In this video, I am going to compare use of for and for each loop in Java to process arrays. For each loop is new in Java. It is not available in C and C++. Let's compare both the loops. Name of class is for each. Inside this class, I am going to define main method. Inside this main method, I am going to initialize an array with five values. You can also read these values from user. But as of now, for demonstration purpose, I have initialized it at my own. Now, first I am going to process this array using for loop. Then I will use for each loop for processing. So, in case of for loop, we have to use initialization first. Then we can put condition. Or you can say termination condition then increment. Inside this loop, if you want to display the values, you have to write system.out.println then arr of i. So this way for loop will display these values on output screen. So, first execute this. You can see five values, one, two, three, four, five are displayed on output screen properly with the help of for loop. Now, I am making it comment and I will be using for each loop. So, in case of for each loop, you have to declare a variable like this. And then you have to use name of array. And inside curly basis, you can do the processing. So, here you don't have to initialize the loop. You don't have to test the condition. You don't have to use the increment or decrement part. You just have to use the name of array like here I have used arr. The values of this arr array will be assigned into this a variable and you can use this a variable for processing purpose here. So, this is for loop and this is for each loop. Now I can write system.out.println and I can write a here. I have saved the code. Again, I have to compile the code and I'm going to execute it. You can see it is working properly. It is printing those five elements one, two, three, four, five on output screen. Now if you want to perform any kind of operation in for each loop, that is also possible. Like you want to perform addition operation. So, you can write like this. Inside for each loop, I have added values of a into s. After completion of this for each loop, value of s will be displayed on output screen as some. So, this way you can also do any kind of operation in case of for each loop. Again, I'm going to compile the code first and then execution. You can see it is showing some equals to 15. So, one plus two plus three plus four plus five is equals to 15. So, this way I have compared both the loops in front of you for and for each loop. You have to remember that we can only use for each loop for arrays because in another scenarios, there will be initialization and condition explicitly. But in case of array for each loop, do it its own. So, in case of array you can use for each loop but in case of any other operation you have to use for while you're doing while. So, I hope you have understood whatever I have demonstrated you in this video. If you want to watch more programming related videos, you can search the keyword Programming by Sanjay Gupta on YouTube. Thank you for watching this video.