﻿1
00:00:04,390 --> 00:00:05,200
‫Hello and welcome.

2
00:00:05,200 --> 00:00:10,990
‫In this lecture, we're going to be looking at an even cleaner syntax for for loops which we use specifically

3
00:00:10,990 --> 00:00:13,870
‫when dealing with arrays or collection types.

4
00:00:13,870 --> 00:00:15,790
‫Let's dive in and see how it looks.

5
00:00:16,600 --> 00:00:23,680
‫So this pattern of iterating over all the actors in an array is so common that we actually have our

6
00:00:23,680 --> 00:00:29,470
‫own type of for loops specifically designed for this purpose in C++.

7
00:00:29,710 --> 00:00:35,590
‫So the basic format of this for loop, it's called a range based for loop works like this.

8
00:00:35,590 --> 00:00:42,010
‫So we start with the same keyword, we have some opening parentheses, but then instead of having these

9
00:00:42,010 --> 00:00:49,540
‫three statements, we do the following we create a variable to store the pointer or the thing within

10
00:00:49,540 --> 00:00:52,150
‫the array that we are looping over.

11
00:00:52,150 --> 00:00:57,070
‫So in our case it would be an actor pointer, which we can call the actor.

12
00:00:57,580 --> 00:01:01,840
‫Then you put a colon and then you put the array.

13
00:01:02,020 --> 00:01:03,520
‫So that would be actors.

14
00:01:03,520 --> 00:01:10,570
‫So the way this reads is essentially for actor, star, actor in actors.

15
00:01:10,570 --> 00:01:17,530
‫And what that's going to do is it's going to loop over every actor star that is in the t array.

16
00:01:17,530 --> 00:01:23,050
‫Now, this doesn't only work with T arrays, it works with all sorts of different collection types.

17
00:01:23,050 --> 00:01:29,140
‫We call them collection types because they are types that contain multiple of a certain type.

18
00:01:29,140 --> 00:01:36,310
‫So this is very useful because now we've got in each iteration of our for loop a pointer to a different

19
00:01:36,310 --> 00:01:42,460
‫actor from the array and it's just a lot cleaner and simpler and easier to read than the for loop above.

20
00:01:42,820 --> 00:01:44,470
‫So I'm not going to give you a challenge slide here.

21
00:01:44,470 --> 00:01:50,890
‫I just want you to convert from using this three statement for loop to using the range based for loop

22
00:01:50,890 --> 00:01:54,820
‫instead and test out your findings for the video and have a go.

23
00:01:57,870 --> 00:01:58,130
‫Okay.

24
00:01:58,130 --> 00:01:58,900
‫Welcome back.

25
00:01:58,910 --> 00:02:03,860
‫So I'm going to copy over this body and what am I going to need to change?

26
00:02:03,860 --> 00:02:11,300
‫Well, instead of accessing the array directly with an index, we now have our actor pointer variable.

27
00:02:11,300 --> 00:02:14,270
‫So we can just use actor, get actor, name or label.

28
00:02:14,270 --> 00:02:16,190
‫That's the actor name and that's what we're printing out now.

29
00:02:16,190 --> 00:02:18,020
‫We can get rid of the old for loop.

30
00:02:18,440 --> 00:02:22,100
‫We no longer need that and we can go ahead and compile.

31
00:02:22,100 --> 00:02:22,760
‫Once it's done.

32
00:02:22,760 --> 00:02:27,320
‫Let's go ahead and hit play, grab a gargoyle and go and step inside.

33
00:02:27,320 --> 00:02:33,350
‫And sure enough, it's printing out the gargoyle statue and the beep player when we're both in the alcove.

34
00:02:33,380 --> 00:02:34,190
‫Great stuff.

35
00:02:34,190 --> 00:02:35,750
‫I'll see you in the next lecture.

