‫Hello and welcome.
‫In this lecture, we're going to be looking at an even cleaner syntax for for loops which we use specifically
‫when dealing with arrays or collection types.
‫Let's dive in and see how it looks.
‫So this pattern of iterating over all the actors in an array is so common that we actually have our
‫own type of for loops specifically designed for this purpose in C++.
‫So the basic format of this for loop, it's called a range based for loop works like this.
‫So we start with the same keyword, we have some opening parentheses, but then instead of having these
‫three statements, we do the following we create a variable to store the pointer or the thing within
‫the array that we are looping over.
‫So in our case it would be an actor pointer, which we can call the actor.
‫Then you put a colon and then you put the array.
‫So that would be actors.
‫So the way this reads is essentially for actor, star, actor in actors.
‫And what that's going to do is it's going to loop over every actor star that is in the t array.
‫Now, this doesn't only work with T arrays, it works with all sorts of different collection types.
‫We call them collection types because they are types that contain multiple of a certain type.
‫So this is very useful because now we've got in each iteration of our for loop a pointer to a different
‫actor from the array and it's just a lot cleaner and simpler and easier to read than the for loop above.
‫So I'm not going to give you a challenge slide here.
‫I just want you to convert from using this three statement for loop to using the range based for loop
‫instead and test out your findings for the video and have a go.
‫Okay.
‫Welcome back.
‫So I'm going to copy over this body and what am I going to need to change?
‫Well, instead of accessing the array directly with an index, we now have our actor pointer variable.
‫So we can just use actor, get actor, name or label.
‫That's the actor name and that's what we're printing out now.
‫We can get rid of the old for loop.
‫We no longer need that and we can go ahead and compile.
‫Once it's done.
‫Let's go ahead and hit play, grab a gargoyle and go and step inside.
‫And sure enough, it's printing out the gargoyle statue and the beep player when we're both in the alcove.
‫Great stuff.
‫I'll see you in the next lecture.