‫Now that you know how to declare an initialize race and how to use them, it's time to use a for each
‫loop or to get to know the for each loop in order to get all the values of an array or put values into
‫an array and so forth.
‫So let's go ahead and create a new array, which I'm going to call nums.
‫So it's just going to contain some numbers and it's going to be nums equal new int let's say it has
‫ten values, so there should be ten values in that array and it all should be integers.
‫Next, I want to put values into that integer array so we get started by using the for loop here and
‫I'm just going to start off with zero I smaller ten and I plus plus.
‫So what I do here is I simply assign the value of its index to well, the value itself.
‫So let's go ahead and just say I.
‫So what that will do is at the position zero so an index zero value will be zero at index, one value
‫will be one.
‫All right.
‫So this is pretty much what we do with this for loop and all the way up to nine so until lower than
‫ten.
‫So up to nine.
‫So let's go ahead and see what that means.
‫So in order to run through the loop and see all the values, we can simply use a for loop again for
‫INT and I'm going to use j here for and j equals zero j lower ten j plus plus.
‫And now I just want to get the CW double tap and in here I get the element at the position zero.
‫So I just want to get the values out of my array and that will be equal to one.
‫What I need here is J and nums at the position of J.
‫So this is the index and that will be the actual value.
‫And then finally, at one point, we all need to wait for the output or for input of the user.
‫All right.
‫So let's just run that.
‫And then we are we get a zero element, 1 to 3 and so forth.
‫So that was simply what I've done here.
‫We could change that to let's say I don't want to enter I to it.
‫I want to enter I plus ten.
‫So let's run it again.
‫And at the position zero, we have the value of ten.
‫At the position one, we have the value of 11 and so forth.
‫All right.
‫So it doesn't have to be the same value for the index as the index itself.
‫So that's one way of doing it.
‫But what we do here is quite risky.
‫So, for example, if I just say up to 11 or below 11, so the means up to ten, I run into an index
‫out of bounds exception or actually out of range exception.
‫So what that means is it's trying to access an index of the array which is out of range, which is not
‫within the range of the array because the array only has a range of ten and we are trying to access
‫its 11th position and that's something that doesn't work.
‫So we get this error.
‫So in order to avoid that, we have two solutions.
‫One of them we use the length property, so only as long as the length is, that's how many values we
‫want to have.
‫So we run it again and now we don't get the error.
‫So now that's fine.
‫An alternative way is to use it for each loop.
‫So let's have a look at the structure for each loop.
‫First, we use the keyword for each, and then within brackets we create a new variable.
‫I'm just going to call it K.
‫And then you say in so again, a new keyword in the array name.
‫So in nums.
‫So that's the nums array name that we had here.
‫So we say create a variable called K and run through the whole array.
‫So that will do is it will simply give us the same thing actually.
‫But in order to run it properly, we need a counter because I want to know the position of it as well.
‫So I'm just going to create a counter and I'm going to use the console right line statement from here
‫once again.
‫So we have the element and now it's a J and nums, but it's the counter and K.
‫So K is actually the value of numbers at a specific index.
‫So first at index zero, then it's index one, then at index two and so forth.
‫So how many entries we have in nums?
‫It's going to loop through all of them and it's just going to in each loop say that K is the current
‫value of the index that it's at.
‫So it's first at index zero, then at index one, index two and so forth.
‫So if you run that again.
‫We see we get 10 to 19 and then again 10 to 19.
‫All right.
‫So this for each loop here, it's pretty similar to this.
‫For loop, it does the same thing.
‫So it simply runs through the array and it can be used quite effectively, which is pretty cool.
‫So what's the whole purpose of for each against the for loop?
‫So the idea here is that K has actually the same type as the values of the array itself.
‫In this case, we always have an integer as the loop value.
‫So the loop value is this j or in usual it's an I for example here in this for loop.
‫So it's always an integer when we loop with a for loop, but with a for each loop, the data type is
‫always the one of the array.
‫Then you don't need to specify the bounds, so you didn't need to tell when it starts and when it ends,
‫it always knows it just by the array itself.
‫So the array, let's say, has five values, then it knows, okay, it needs to run five times.
‫Now in terms of performance, in most cases, a for loop is faster than E for each loop.
‫But if you want to dig deeper into it, you should simply check it out yourself.
‫So create complex for each and for loops and test them against each other to see what performs better.
‫All right.
‫Now, a little challenge for you.
‫I'm just going to type it out here.
‫All right.
‫So that one's pretty simple.
‫Create an array with five of your best friends.
‫The data type should be clear and then create a for each loop which greets all of them.
‫So post the video and try to do that.
‫All right.
‫I hope you tried it.
‫So I'm just going to get rid of all this code here.
‫And I'm going to create an array.
‫String, my friends, is equal to Michael.
‫Lutz, Elia, Andy and Daniel.
‫Right.
‫So these are my friends.
‫And now I created for each loop which runs through all of them.
‫So string name in my friends.
‫And now let's greet all of them.
‫So, CW double tap console, right?
‫Hi there.
‫And I'm going to use zero.
‫Come on, my friend.
‫And this one going to be the name.
‫And finally at the end console, the right key so that we can see or can finish the program or solve.
‫So let's run that.
‫So there we are.
‫Hi there, Michael, my friend.
‫Hi there, my friend.
‫Either my friend and so forth.
‫So now I read it to all of my friends and I didn't need to do that manually.
‫One by one.
‫I simply wrote a for each loop for that and I greeted all of them.
‫So that's already a very simple bot.
‫What we simply agreed our friends, and it could be more complex so we could, let's say, create a
‫list of emails here, and then we could create a email service or mailing service which will then create
‫an email and send it out to all of them one by one.
‫And we'll do it with a for each loop automatically.
‫All right.
‫So you've seen how to use race, but there is a lot more to know about the race.
‫And we're going to have a look at that in the next video.
‫So see you there.