In several previous lectures, we've seen how we can use for loops when we know how many times we're going to iterate when we get to the for loop in our code. In this lecture, we'll look at how we can use for loops in a slightly different way to solve a standard game development problem. What's our problem? Our problem is that teddy bears don't live forever. If we have a list of teddy bears that we're maintaining, we need to remove the dead teddy bear from that list. Otherwise, we'll waste time processing those dead teddy bears every time we iterate over the list. Of course, these ideas apply to all lists, not just lists of teddy bears. Let's first look at a counterexample, the way we shouldn't try to do this. We shouldn't try to do this with a forward for loop and that's the only kind of for loop we've written so far. Let's see why. Here's the list of teddy bears we're going to process. The two teddy bears in the middle, the ones with red Xs on them are dead teddy bears that we want to remove from the list. We're going to start our loop control variable i at zero and our condition will be i less than the count of the elements in the list. Which starts as forward because we have four teddy bears in our list. Our first iteration through the loop, we look at element 0, and that's a live teddy bear so we're going to leave it alone. We'll do back to the top of the loop and we increment i to one. We look at the teddy bear at element 1, and we see that that teddy bear is dead so we remove it. One of the things we know about lists is that if we remove an element from in the middle of the list or even at the beginning of the list, the rest of the elements shift down. That happens when we remove the dead teddy bear that's currently at element one. Now we go back to the top of the for loop again and we increment i to two and count is now three because we removed a dead teddy from the list. But i, which is two, is still less than three so we're going to go into the loop again. But you'll notice that the teddy bear at element or at index 2 is the live teddy bear that used to be at the end of the list. Because the elements in the list shifted down when we removed that first dead teddy bear, we skipped over the second dead teddy bear as we were processing the list. When we're done doing this with a forward for loop, we end up with a dead teddy bear still in the list. As a reminder, this is a counterexample, this is not the way we want to do it. Here's how we should do it. Let's start with the list again, but this time we're going to work our way backwards through the list. We're no going to go forward, we're going to go backwards. We start i, our loop control variable at the count of the elements in the list minus 1 at the very end of the list. We're going to decrement, we're going to subtract 1 from i on every iteration. We're going to keep looping as long as i is greater than or equal to zero. We'll make sure we don't run off the left side of our indexing by checking i greater than equal to zero. Let's see how this works. We first look at the teddy bear at index 3, and that's a live teddy bear. We leave it alone and we come back to the top of the loop again and we decrement i, so i is now two, and that is a dead teddy bear. We remove the element 2 the dead teddy at index 2 That shifts the end of the list down, so we now have that live teddy bear in index 2. But when we loop back around, we are going to decrement i again and i is now one. Count moved down to three, but we don't care because we're going right to left. We're checking i greater than or equal to zero. We also don't care about the shifting of that live teddy that is still at the end of a list of just squished over one because we already processed those elements. Now i is one, we have another dead teddy. We remove it and now our list looks like two live teddies. We still need to look at the last one, the one all the way on the left. But now our list is two live teddies. We've decremented i to be zero. Our counters now two, but again, we don't care because we're not comparing to count in our condition. Finally, we look at the teddy bear at index 0, and it's alive teddy bear, and we just leave it alone. The good news is that we've gone backwards through our list removing dead teddies, and we made sure we removed all of them. The other clue that we have that this ray worked is we iterated four times, which means we processed each of the four teddy bears that were in the original list. In the counterexample, we only iterated three times on a list of originally forward teddy bears, so we know we missed one. To recap, in this lecture you saw how we not only can, but should use a backwards for loop if we're planning to or might remove elements from the list as we iterate over the list.