Hello, in this Java video we are going to cover the for each loop. If you have not checked out the video on arrays in the series, I recommend on checking that out because the for each loop is going to be using arrays and extending on what we already did. Okay, so we created an array in that video, doesn't matter what the data type is. Now we created this for loop like so to be able to traverse the array and just get all the values we simply printed them out. There's actually an easier way of doing this for common design. And instead you can do for in some languages is called for each like the actual like, loop is called for each but in some languages, the key word for it in Java is just store.
And instead of having free parts where you set of options or iterator or index value, you set up the condition The setup, you know, the actual thing that happened at the end of each pass of the loop. So in this case, you're just incrementing it by one, you simply do something like so. So you set it like this int is a little different to why were you in here. So you've actually, let me demonstrate this further. So if I get rid of this, if I change this to a float, if I change all the values, the flow should be fine, even if I don't change explicitly, but let me just make sure I do. Okay, so if we were to run this now, so using the old League, it still works.
So what I wanted to say is, this is Troy here is not sort of related to the data type of the array. It isn't, this is just an index and the index of the array of zero through two, whatever, you know, however long this is, I think like what 2468 1012. So the last index actually is index 11. But the length is 12. The index is always an integer. So this is what this is referring to.
But in the for each style of loop, you put that data toy that your array if so in this case, it's float. And if you put float, and then in here, you specify some sort of name. For each element in the array doesn't really matter, almost gonna call it other than a float, E, and then colon, and then what you put into the name of the array so AR you don't put any square brackets or anything like that? curly braces. And that's it. So what this actually does this is really cool is a shorthand version of doing this.
And inside, you can print it out to do whatever you want. So this says, go through this array, and for every single element, so this one is no relevant. This is number one. So number one, there's no more scope for every single one, and call it a every single path of the rating, System dot out dot print ln. And if we print out a, even though we're not specifying any sort of index, it still prints out all of the values if all this changes now, so you can see definitely working with this as we scroll up, it prints out the correct array. So this is a nice shorthand version of this, and this is great if you're traversing array.
If you try and do something else, then you'll probably want to fall back on the traditional You know thing to bear in mind not all programming languages have a form of for each. So sometimes you have to do this method, but in Java fortunately we do. So that's it. If you have any questions, feel free to reach out, and I look forward to seeing you in the next part of this series.