Welcome back.
Now it's finally time we can finally get to the fourth type of loops, and that is therefore each loop.
But before we do that, I want to show you something manually first, because there is this, uh, video
game, I think it's mafia, uh, Mafia two, where you come out of jail and now you're trying to do,
well, lead a normal life.
And the game really forces you to lead the normal life where you are carrying boxes from A to B, and
that's everything you do for the entire day.
So the game actually forces you for a few minutes to carry boxes from A to B to really get bored by
it.
And then the alternative is to go back and become a gangster again.
So that's, uh, something that we're going to do here.
So we're going to do the normal thing before we see how we can do it.
The gangster move basically using for each loops.
So what I want to do is I want to display all days of the week, okay.
I want to display them onto the console.
So let's use this example here right line.
And let's do that.
It's very simple right.
It's just copy and paste.
Very easy.
But copy and paste is okay for a bit but it's not okay for a lot.
So when you're coding and you're copy and pasting away then you are writing very inefficient code in
the sense of it takes a lot of space inside of your IDE.
It's not that it necessarily means that it's going to run slowly, but it's just not, well, readable
code.
So what we are doing here is we're just displaying all days of the week.
So let's run this real quick.
And then we are.
Today is Sunday Monday Tuesday Wednesday Thursday Friday Saturday.
So that's nice.
So now the thing is we can by the way, also know how many items are inside of an array, even if we
don't declare the array ourselves, so to speak.
So here in our case, we know exactly how many items there are, right?
We can count them.
It's very simple.
But what if this wasn't just seven items?
What if this was a thousand items or 10,000 items?
What if this whole array data is not built by ourselves, but we download it from somewhere?
Well then tough luck, because what can you do now?
Well, the thing is, first of all, you can find out the length of the array.
So that's something we can do.
So let me show you how you can say length of weekdays array is and then you can use the length property.
So an array has this thing called property called length.
We'll get us the total number of elements in all the dimensions of the array.
Now all the dimensions.
Wow okay.
We haven't looked at multi-dimensional arrays yet.
We will do that later on.
But for now what we see here is we have weekdays, and those weekdays have a certain length, which
means there are a certain amount of items inside of this array called weekdays.
So this length gives us the amount of items in an array.
Okay, let's run this.
And it's going to say length of weekdays.
Arrays is seven.
Well not well written but whatever of the weekdays array is and then the length.
So now we could use this length in order to iterate through the weekdays with a for loop.
So we have seen how for loops work.
And that is an approach we can use.
It's not the most efficient one, but if I were to use it like this and I could just say okay for int
I and you can see the IDE already knows exactly what I want to do.
So that's great.
So what I did manually here, now we just did with this for loop.
So we are using a for loop where we're starting at zero.
So at index zero because an array starts at index zero.
And then we iterate all the way until less than the length of the array.
Because if the array is seven in terms of its length then the biggest index.
So the highest index that there is is six.
So that's why we're using I less than weekday start length and I plus plus.
So this will do the same thing for us.
And it will just show us every day of the week length of the weekdays.
So sun mon Tue and so forth, we could go ahead and increase whatever we're writing here.
But basically speaking, this is what we can achieve with the for loop.
But there is an even nicer way and that is that you use a for each loop.
So the for each loop looks like this.
So you have a var item in the array.
So here whatever array slash list you have.
So in our case I want to go through my weekdays.
So weekdays is the array.
And now this var item is just going to be every single item of the weekdays that there are.
So now I can just go ahead and use Console.WriteLine to display item.
So let me get rid of.
This for loop.
Therefore I'm commenting it out for now.
So this for each loop will now achieve the same thing.
Let me run this and it will say sun Mon Tue wed thu Friday.
Tut.
Okay, so another way of calling this var item would be string de.
Okay.
And then just use de here.
It was just something with naming right.
You can name it whatever.
But it's really important that the data type that you use here is the same as the data type of the array
itself.
So the array has the data type string array.
But the string is what we're looking at.
So string de is going to be the individual day of the current iteration.
So the current run through of our weekdays array.
So let's actually add a breakpoint here and debug this.
So length of weekdays.
And let me put it like this.
So we can read it better.
So I just go in it says okay.
Now it looks at the weekdays and then it goes into in and then it goes into the string de.
So it first looks at the array then says okay in the array there is a day.
Now let's look at the first day.
So it executes this Console.WriteLine day where day starts with sun.
So in the first iteration it is sun.
Why is it sun?
Because the first item, the first entry inside of our weekdays array is sun.
So then the iteration is done.
It checks again.
So do we still have items inside of our weekdays.
And then it just says okay we do.
Now let's go ahead and use the next item and call it day.
So the string day variable is only available inside of these curly brackets here.
So now they will be Munn okay.
So we get into the line of code and you can see if you hover over day you can see that it is in fact
Munn.
So now it just goes through every single item inside of our weekdays until there are no more weekdays.
Okay.
So let me click through it.
We are at Friday now we're at Saturday.
It executes it one more time.
And now if I go to the next line it will check.
Is there still an item inside of weekdays that I haven't covered?
It says no, there isn't.
So it gets out of this for each loop and gets to the next line.
Okay.
And that is what's happening here okay.
So this is the for each loop which allows us to just say okay, just go through the list.
However many items there are in this list, if it's an array, if it's a list, doesn't matter.
In this case we're only looking at arrays because that's what we what we are looking at at this moment.
But we can see that for each item inside of the array, we can do something now with it using the for
each loop.
So that's what the for each loop is really for.
It's not about creating a counter, it's not about any of that.
It's really about iterating through a collection of items.
Okay, so that's it for this video.
Now you know how for each loops work.
And we have seen a little more about the array data type.
So see you in the next video.