In the last few lectures, we've explored how we can use for loops to iterate over lists. In this lecture, you will learn about for each loops which provide us another way to iterate over arrays and lists. Our starting code for this lecture is the for loops code we had where we created a deck in hand, added some cards to the hand, printed the cards, added some more cards and printed the cards again. What we're going to do in this lecture is we're going to convert each of the for loops that we can convert into a for each loop into a. For each loop. The first loop we can do that with is printing the cards in the hand. So instead of doing a for loop, I'm going to do a for each loop. And of course the syntax here is different as well. And the way this syntax works is the first thing we put is the data type for each element in the array or list. We're iterating over. So we know hand holds a set of cards. So card is the data type of each of those elements. And if you've forgotten, you could just look up here and see what data type. The handholds. The next thing we do is we provide the name of a variable that we want to use within the body of the for each loop when we're referring to that element in the list and I'll call this card. So this looks a lot like a variable declaration where we have a data type and a variable name because it essentially acts like a variable declaration. So anywhere between this open curly brace and this close curly brace, we can just access card, like we access variables. The next thing we put is the key word in because we're iterating over each of the elements in a particular array or list. And finally we put the name of the array or list. We're iterating over. Of course we have syntax errors here because we no longer have an I instead we have a card, variable so I'll change this to card and I'll change this to card. So how does this work? The first time through the loop, card is assigned the value of hand, zero the zeroth element in the hand list. Then within the body of the for each loop we reference that card, variable and then we loop back again and card gets assigned the element at index one in hand and so on. So each iteration through the loop, we're processing another element of the array or list that we're iterating over. And this automatically runs the correct number of times based on how many elements are in the array or list. Let's go ahead and run this code. And as you can see that first printing out the cards, loop works just like it did before, except this time we're using a. For each loop. Any time we use a. For each loop we could use a for loop instead, as you can see from this particular example. But the standard practice in C Sharp is when you can use a for each loop, you should use a for each loop. Let's look at the next four loop in our code here. We're not actually iterating over an array or list. We're modifying a list here, but we're not doing something to process each element in an array or list. We just start this four loop, one and we iterate five times and then we stop the for loop. So we can't convert this four loop into a for each loop because this four loop is not iterating over a list, even though it's modifying the list, it's not iterating over a list. It's just iterating five times. So this is one example where we can't convert a four loop to our, for each loop this last for loop, prints the cards in the hand. So I'm going to just come up here and take the loop that I used to print the cards in the hand and I'll paste it there and we'll test again. And that loop of course works properly as well. I want to show you another example of something we cannot do in for each loops, let's say, even though we would never actually do this, as we're printing out the cards in the hand. Let's say after printing a card, we decided we wanted to remove that card from the hand. So we could say hand dot remove, and card is the thing we're going to try to remove, I'll run the code and as you can see, we get an exception here. We have an invalid operation exception. And it says that the collection was modified and I probably didn't say this when I was covering lists, but lists are actually something called a collection class. That's why it's in the system.collections.generic name space. So the list is an example of a collection class. It's a collection of elements. That's not the point here though, with this example, the point here is that within a for each loop we're not allowed to modify the array or collection or list, right. That we're iterating over so we can't do this inside our for each loop. If in fact we need to modify the array or list that we're iterating over, we have to use a for loop we can't use a foreach loop. So I'll get rid of this bad code and test it one more time and everything still works fine. So we saw two examples of places where we can't replace a for loop with a for each loop, we couldn't replace this one because we weren't iterating over an array or list. And even though I removed the code, we couldn't use a for each loop here if we were going to modify the list. But we also saw an example here and really the same functionality here where we could replace a for loop of the for each loop and just to reiterate using a for each loop is standard practice in C Sharp when we can use it for each loop instead of a for loop to recap. In this lecture, you learned about for each loops, which provide us another way to iterate over a raisin lists. And we learned that for each loops are best practice for C Sharp programmers. If you can use a for each loop, you should. But we also learned that there are some scenarios in which you have to use a for loop. You can't actually use a for each loop.