[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.82,0:00:04.31,Default,,0000,0000,0000,,We're back with a program \Nthat prints out my array of friends. Dialogue: 0,0:00:04.31,0:00:06.85,Default,,0000,0000,0000,,But there's something \Nthat really annoys me about it. Dialogue: 0,0:00:06.85,0:00:09.47,Default,,0000,0000,0000,,Every time I add a new friend \Nto the array, I have to add Dialogue: 0,0:00:09.47,0:00:11.40,Default,,0000,0000,0000,,a new text command down here. Dialogue: 0,0:00:11.40,0:00:13.23,Default,,0000,0000,0000,,So let's say I add Winston. Dialogue: 0,0:00:13.23,0:00:15.33,Default,,0000,0000,0000,,Well, he doesn't automatically show up. Dialogue: 0,0:00:15.33,0:00:19.92,Default,,0000,0000,0000,,If I want him to show up, I have to go \Nand say {\i1}text(myFriends[3]{\i0}, Dialogue: 0,0:00:19.92,0:00:22.39,Default,,0000,0000,0000,,and then change the y position \Nand then we see Winston. Dialogue: 0,0:00:22.39,0:00:24.84,Default,,0000,0000,0000,,So that's way too much work. Dialogue: 0,0:00:24.84,0:00:28.26,Default,,0000,0000,0000,,I just want it so that every time \NI add someone else to the array, Dialogue: 0,0:00:28.38,0:00:31.31,Default,,0000,0000,0000,,it does the text command automatically. Dialogue: 0,0:00:31.31,0:00:34.06,Default,,0000,0000,0000,,Well, do you remember \Nwhen we learned loops? Dialogue: 0,0:00:34.59,0:00:37.16,Default,,0000,0000,0000,,Loops were a great way \Nto repeat the same bit of code Dialogue: 0,0:00:37.16,0:00:38.54,Default,,0000,0000,0000,,many times in a row. Dialogue: 0,0:00:38.54,0:00:41.07,Default,,0000,0000,0000,,Like, if we wanted to have \Na bunch of trees in a row Dialogue: 0,0:00:41.07,0:00:42.75,Default,,0000,0000,0000,,or a bunch of balloons. Dialogue: 0,0:00:42.75,0:00:47.20,Default,,0000,0000,0000,,Well, as it turns out, loops are also \Na great way to run a bit of code Dialogue: 0,0:00:47.20,0:00:49.31,Default,,0000,0000,0000,,on each element in an array. Dialogue: 0,0:00:49.77,0:00:53.62,Default,,0000,0000,0000,,In fact, you'll use a loop almost \Nevery time you use an array. Dialogue: 0,0:00:53.62,0:00:56.31,Default,,0000,0000,0000,,They work really well together. Dialogue: 0,0:00:57.43,0:01:00.71,Default,,0000,0000,0000,,So lets use a loop to display \Nmy friends' names, instead of having Dialogue: 0,0:01:00.71,0:01:03.12,Default,,0000,0000,0000,,all these text commands \Nto show you what I mean. Dialogue: 0,0:01:03.35,0:01:06.49,Default,,0000,0000,0000,,So we'll start with the three questions \Nwe always ask ourselves Dialogue: 0,0:01:06.59,0:01:08.12,Default,,0000,0000,0000,,when we're making a loop. Dialogue: 0,0:01:08.12,0:01:11.24,Default,,0000,0000,0000,,First, what do I want to repeat? \NWell, look up here. What's repeated? Dialogue: 0,0:01:11.24,0:01:12.76,Default,,0000,0000,0000,,The text command. Dialogue: 0,0:01:12.76,0:01:15.82,Default,,0000,0000,0000,,What do I want to change each time? \NWell, let me just look Dialogue: 0,0:01:15.82,0:01:20.00,Default,,0000,0000,0000,,and see what's different, the y position \Nand the current index, right? Dialogue: 0,0:01:20.00,0:01:25.15,Default,,0000,0000,0000,,So the friend num and the y position. Dialogue: 0,0:01:25.31,0:01:28.22,Default,,0000,0000,0000,,And how long should we repeat? \NWell, we keep going Dialogue: 0,0:01:28.22,0:01:30.73,Default,,0000,0000,0000,,until there are no more friends. Dialogue: 0,0:01:32.60,0:01:36.82,Default,,0000,0000,0000,,So now we know what we want, \Nand we can make our loop. Dialogue: 0,0:01:36.95,0:01:40.52,Default,,0000,0000,0000,,We start off with a counter variable \Nto keep track of where we are in the loop. Dialogue: 0,0:01:40.52,0:01:43.39,Default,,0000,0000,0000,,So we'll say {\i1}var friendNum = 0;{\i0} Dialogue: 0,0:01:44.42,0:01:47.32,Default,,0000,0000,0000,,We're going to start with zero \Nbecause remember that 0 Dialogue: 0,0:01:47.32,0:01:51.82,Default,,0000,0000,0000,,is the first element in the array, not 1.\NThen we have our while loop. Dialogue: 0,0:01:51.83,0:01:57.21,Default,,0000,0000,0000,,So we'll say \N{\i1}while(friendNum < my friends.length){\i0}. Dialogue: 0,0:01:57.87,0:02:01.70,Default,,0000,0000,0000,,So we're gonna compare the current \Ncounter variable to the total number Dialogue: 0,0:02:01.70,0:02:05.36,Default,,0000,0000,0000,,of things in the array.\NInside the loop, that's where Dialogue: 0,0:02:05.36,0:02:07.23,Default,,0000,0000,0000,,we use our text command. Dialogue: 0,0:02:07.27,0:02:10.91,Default,,0000,0000,0000,,So we say, {\i1}text(myFriends[{\i0} \Nand then here, instead of a number, Dialogue: 0,0:02:10.91,0:02:13.10,Default,,0000,0000,0000,,we're gonna put {\i1}friendNum{\i0} \Nbecause friendNum represents Dialogue: 0,0:02:13.10,0:02:14.47,Default,,0000,0000,0000,,the current number. Dialogue: 0,0:02:14.47,0:02:17.90,Default,,0000,0000,0000,,And then we'll just put \None position for now. Dialogue: 0,0:02:18.17,0:02:22.74,Default,,0000,0000,0000,,This has given us a little infinite \Nloop error because we haven't Dialogue: 0,0:02:22.74,0:02:25.55,Default,,0000,0000,0000,,actually changed anything \Nabout friendNum. Dialogue: 0,0:02:25.55,0:02:28.79,Default,,0000,0000,0000,,Remember, we need to increment \NfriendNum each time, otherwise the loop Dialogue: 0,0:02:28.79,0:02:31.67,Default,,0000,0000,0000,,will go on forever \Nbecause that condition's always true. Dialogue: 0,0:02:32.02,0:02:36.34,Default,,0000,0000,0000,,I see something happened. \NLet me comment out the old code Dialogue: 0,0:02:36.34,0:02:38.32,Default,,0000,0000,0000,,so I can really see what's happened. Dialogue: 0,0:02:38.32,0:02:41.24,Default,,0000,0000,0000,,What we have is we've showed \Nall the names, Dialogue: 0,0:02:41.24,0:02:43.54,Default,,0000,0000,0000,,but they're all on top of each other. Dialogue: 0,0:02:43.54,0:02:45.49,Default,,0000,0000,0000,,So, we need to change our y position. Dialogue: 0,0:02:45.51,0:02:49.24,Default,,0000,0000,0000,,Let's just say 'friendNum*30'. Dialogue: 0,0:02:49.89,0:02:53.28,Default,,0000,0000,0000,,That's good but Sophia's off the screen \Nand Sophia's not going Dialogue: 0,0:02:53.28,0:02:55.31,Default,,0000,0000,0000,,to be very happy if she finds that out. Dialogue: 0,0:02:55.31,0:02:59.59,Default,,0000,0000,0000,,So, let's just add 30 to that. \NSo now they're all offset by 30. Dialogue: 0,0:02:59.59,0:03:03.93,Default,,0000,0000,0000,,Beautiful! So now you see we have \Na loop displaying our array. Dialogue: 0,0:03:04.36,0:03:09.05,Default,,0000,0000,0000,,And that means if we add more people\Nlike OhNoes Guy, or maybe even Sal, Dialogue: 0,0:03:09.05,0:03:12.14,Default,,0000,0000,0000,,if I just add him to the array \Nthen Sal will be my friend. Dialogue: 0,0:03:12.19,0:03:14.23,Default,,0000,0000,0000,,Awesome! Now he's my buddy. Dialogue: 0,0:03:14.23,0:03:18.03,Default,,0000,0000,0000,,And you see that it just automatically \Nshows the new friends Dialogue: 0,0:03:18.33,0:03:20.76,Default,,0000,0000,0000,,because it's always going \Nthrough the whole array. Dialogue: 0,0:03:21.00,0:03:24.18,Default,,0000,0000,0000,,So we can delete our old code. \NWe don't need that anymore. Dialogue: 0,0:03:24.18,0:03:27.89,Default,,0000,0000,0000,,And let's just go through \Nthis code here and review what it does. Dialogue: 0,0:03:27.89,0:03:31.02,Default,,0000,0000,0000,,So we start off \Nwith friendNum equal to zero. Dialogue: 0,0:03:31.46,0:03:34.35,Default,,0000,0000,0000,,We check to see if friendNum is \Nless than the current length. Dialogue: 0,0:03:34.35,0:03:37.52,Default,,0000,0000,0000,,So you imagine zero is \Nless than six. That's true. Dialogue: 0,0:03:37.86,0:03:41.94,Default,,0000,0000,0000,,So then we go inside here \Nand we say text, my friends friendNum. Dialogue: 0,0:03:41.94,0:03:44.44,Default,,0000,0000,0000,,So that'll be my friends zero, \Nthe first time. Dialogue: 0,0:03:44.61,0:03:47.59,Default,,0000,0000,0000,,And then 30 plus zero times 30. Dialogue: 0,0:03:47.59,0:03:52.80,Default,,0000,0000,0000,,So it displays Sophia at 10 and 30. \NThat's what it does. Dialogue: 0,0:03:53.77,0:03:56.35,Default,,0000,0000,0000,,And then friendNum++. \NSo then it becomes 1. Dialogue: 0,0:03:56.35,0:03:58.38,Default,,0000,0000,0000,,And then it goes back around \Nand says, "Ok, is 1 less Dialogue: 0,0:03:58.38,0:04:00.21,Default,,0000,0000,0000,,than myfriends.length? \NYeah, it is." Dialogue: 0,0:04:00.36,0:04:02.59,Default,,0000,0000,0000,,And it keeps going, \Nkeeps going, keeps going. Dialogue: 0,0:04:02.59,0:04:05.87,Default,,0000,0000,0000,,And then finally we get to Sal. \NRemember, Sal is actually Dialogue: 0,0:04:05.87,0:04:10.95,Default,,0000,0000,0000,,the sixth element in the array, \Nbut he's index 5, since we start at zero. Dialogue: 0,0:04:10.95,0:04:13.80,Default,,0000,0000,0000,,So, is five less than six? Yes. Dialogue: 0,0:04:13.80,0:04:16.03,Default,,0000,0000,0000,,So it displays myfriends five. Dialogue: 0,0:04:16.03,0:04:20.11,Default,,0000,0000,0000,,And then it becomes six \Nand we say, "Is six less than six?" Dialogue: 0,0:04:20.24,0:04:21.83,Default,,0000,0000,0000,,No. It's equal. Dialogue: 0,0:04:21.83,0:04:25.39,Default,,0000,0000,0000,,So this'll be false. \NSo it will never display the sixth element Dialogue: 0,0:04:25.39,0:04:29.07,Default,,0000,0000,0000,,which is good \Nbecause there is nothing in index six. Dialogue: 0,0:04:29.07,0:04:33.25,Default,,0000,0000,0000,,There's a sixth element, \Nbut nothing in index six. Dialogue: 0,0:04:33.25,0:04:36.41,Default,,0000,0000,0000,,it can be really confusing, \Nthe fact that it's zero and one Dialogue: 0,0:04:36.41,0:04:39.03,Default,,0000,0000,0000,,and doing all that \Nbut you'll get the hang of it. Dialogue: 0,0:04:39.03,0:04:41.37,Default,,0000,0000,0000,,All right, so that's our loop. Dialogue: 0,0:04:41.93,0:04:45.01,Default,,0000,0000,0000,,Now, if you want to, you can \Nactually use a for loop Dialogue: 0,0:04:45.01,0:04:46.72,Default,,0000,0000,0000,,as well if you prefer for loops. Dialogue: 0,0:04:46.78,0:04:51.95,Default,,0000,0000,0000,,For for loops, we'll just say, {\i1}for{\i0},\Nand then, {\i1}var friendNum = 0;{\i0} Dialogue: 0,0:04:52.66,0:04:54.43,Default,,0000,0000,0000,,and then we have our condition Dialogue: 0,0:04:54.43,0:04:57.82,Default,,0000,0000,0000,,{\i1}friendNum < myFriends.length{\i0} Dialogue: 0,0:04:57.82,0:05:01.24,Default,,0000,0000,0000,,and then our increment: {\i1}friendNum++{\i0} Dialogue: 0,0:05:01.24,0:05:06.61,Default,,0000,0000,0000,,and then inside the for loop, we can \Njust put just this line of code here. Dialogue: 0,0:05:06.61,0:05:09.48,Default,,0000,0000,0000,,and I'll just change the x \Nso that you can see Dialogue: 0,0:05:09.54,0:05:14.23,Default,,0000,0000,0000,,it does exactly the same thing. \NSo it's up to you which one you use, Dialogue: 0,0:05:14.23,0:05:17.09,Default,,0000,0000,0000,,but the point is to use a loop \Nwith your arrays Dialogue: 0,0:05:17.09,0:05:19.82,Default,,0000,0000,0000,,because it will make you so powerful.