[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:01.46,0:00:03.04,Default,,0000,0000,0000,,Now that you know about while loops, Dialogue: 0,0:00:03.01,0:00:05.24,Default,,0000,0000,0000,,let's talk about\Nanother kind of loop, for loops. Dialogue: 0,0:00:05.81,0:00:07.40,Default,,0000,0000,0000,,Here's a for loop I made to praise Dialogue: 0,0:00:07.40,0:00:10.53,Default,,0000,0000,0000,,the most delicious kind of pizza,\Npineapple pizza. Dialogue: 0,0:00:10.54,0:00:12.96,Default,,0000,0000,0000,,You might think, "I liked while loops," Dialogue: 0,0:00:12.97,0:00:15.13,Default,,0000,0000,0000,,"Why should I care about\Nthis new kind of loop?" Dialogue: 0,0:00:15.14,0:00:17.33,Default,,0000,0000,0000,,Well, soon you will see\Nthat a for loop is a way Dialogue: 0,0:00:17.33,0:00:20.63,Default,,0000,0000,0000,,to write simple kinds of while loops\Nin a bit of a shorter way. Dialogue: 0,0:00:20.64,0:00:22.69,Default,,0000,0000,0000,,So, before we inspect this for loop, Dialogue: 0,0:00:22.69,0:00:26.32,Default,,0000,0000,0000,,let's go ahead and look at a while loop\Nwhich will be a bit more familiar. Dialogue: 0,0:00:26.32,0:00:28.53,Default,,0000,0000,0000,,We'll get back to this for loop\Nin a moment. Dialogue: 0,0:00:28.53,0:00:31.27,Default,,0000,0000,0000,,So, just like you saw\Nin the intro to while loops, Dialogue: 0,0:00:31.25,0:00:33.58,Default,,0000,0000,0000,,We start with this variable {\i1}y{\i0}, Dialogue: 0,0:00:33.60,0:00:36.73,Default,,0000,0000,0000,,and we write a message\Nusing that location {\i1}y{\i0}. Dialogue: 0,0:00:36.73,0:00:38.50,Default,,0000,0000,0000,,We say that we want to keep going Dialogue: 0,0:00:38.50,0:00:42.77,Default,,0000,0000,0000,,as long as {\i1}y{\i0} is less than 300\Nand change {\i1}y{\i0} by 40 each time. Dialogue: 0,0:00:42.77,0:00:46.67,Default,,0000,0000,0000,,Now, just to reinforce each of these,\Nwhich should be review, Dialogue: 0,0:00:46.67,0:00:50.60,Default,,0000,0000,0000,,we can say that "Pineapple pizza is\Nthe best" is an important message, Dialogue: 0,0:00:50.60,0:00:52.40,Default,,0000,0000,0000,,so we probably want to be\Nwriting it more. Dialogue: 0,0:00:52.40,0:00:55.27,Default,,0000,0000,0000,,so we should start higher\N--so we change that--, Dialogue: 0,0:00:55.27,0:00:58.47,Default,,0000,0000,0000,,it should go on for longer,\Nso we make it bigger, Dialogue: 0,0:00:58.47,0:01:01.64,Default,,0000,0000,0000,,and it should be spaced\Ncloser together --don't you think?-- Dialogue: 0,0:01:01.64,0:01:03.61,Default,,0000,0000,0000,,so that's a little bit better. Dialogue: 0,0:01:03.61,0:01:07.50,Default,,0000,0000,0000,,Now, let's see how we can change\Nthis while loop into a for loop. Dialogue: 0,0:01:07.51,0:01:09.57,Default,,0000,0000,0000,,I'm going to comment out this while loop, Dialogue: 0,0:01:09.57,0:01:14.03,Default,,0000,0000,0000,,just so you can see that it's doing\Nwhat I'm saying it's going to do. Dialogue: 0,0:01:14.03,0:01:19.84,Default,,0000,0000,0000,,And we'll create a for loop\Nand some space to fill in later. Dialogue: 0,0:01:19.84,0:01:22.54,Default,,0000,0000,0000,,Something really important,\Nkind of different for a for loop, Dialogue: 0,0:01:22.54,0:01:26.38,Default,,0000,0000,0000,,it's not just one thing that goes in here,\Nit's actually three things. Dialogue: 0,0:01:26.38,0:01:27.93,Default,,0000,0000,0000,,We tell the computer that Dialogue: 0,0:01:27.93,0:01:31.20,Default,,0000,0000,0000,,by using these semicolons\Nto separate the three parts, Dialogue: 0,0:01:31.54,0:01:34.47,Default,,0000,0000,0000,,we'll have one part here,\None part here, one part here, Dialogue: 0,0:01:34.47,0:01:37.33,Default,,0000,0000,0000,,and then, the inside of the loop,\Njust like we had before. Dialogue: 0,0:01:37.33,0:01:39.58,Default,,0000,0000,0000,,So, what goes in each of these parts? Dialogue: 0,0:01:39.58,0:01:45.87,Default,,0000,0000,0000,,You can think of it as a start,\Nor initialization, Dialogue: 0,0:01:45.87,0:01:47.97,Default,,0000,0000,0000,,then we'll have a semicolon, Dialogue: 0,0:01:47.97,0:01:50.99,Default,,0000,0000,0000,,then, some sort of instructions\Non how long to repeat, Dialogue: 0,0:01:50.100,0:01:55.97,Default,,0000,0000,0000,,and then we're going to have\Nsome sort of change happening. Dialogue: 0,0:01:57.17,0:01:59.63,Default,,0000,0000,0000,,So, how does that correspond\Nwith this while loop? Dialogue: 0,0:01:59.63,0:02:01.07,Default,,0000,0000,0000,,Really concretely, we can say Dialogue: 0,0:02:01.08,0:02:04.13,Default,,0000,0000,0000,,that the start sets up\Nthis variable {\i1}y{\i0} here, Dialogue: 0,0:02:04.13,0:02:08.88,Default,,0000,0000,0000,,so let's copy and paste that over\Nto this first part of the for loop. Dialogue: 0,0:02:08.88,0:02:13.40,Default,,0000,0000,0000,,Similarly, we can say\Nthis middle part is saying Dialogue: 0,0:02:13.40,0:02:17.20,Default,,0000,0000,0000,,how long we should keep going for\Nand that goes in the middle. Dialogue: 0,0:02:17.20,0:02:24.74,Default,,0000,0000,0000,,We say the change happens at the end here,\Nwe'll put that at the end of the for loop. Dialogue: 0,0:02:25.70,0:02:29.52,Default,,0000,0000,0000,,These three parts always have to occur\Nin this order in your for loop. Dialogue: 0,0:02:29.52,0:02:32.07,Default,,0000,0000,0000,,You always have to start by saying Dialogue: 0,0:02:32.07,0:02:34.29,Default,,0000,0000,0000,,this is what the variable\Nshould start out as. Dialogue: 0,0:02:34.30,0:02:35.57,Default,,0000,0000,0000,,Here it's 27. Dialogue: 0,0:02:35.58,0:02:41.56,Default,,0000,0000,0000,,Then you say how long to keep going for.\NRepeat as long as it is less than 354. Dialogue: 0,0:02:41.56,0:02:44.50,Default,,0000,0000,0000,,Then you need to put\Nhow you are going to be changing things. Dialogue: 0,0:02:44.50,0:02:47.84,Default,,0000,0000,0000,,So here we are going to be changing\Nby increasing {\i1}y{\i0} by 24. Dialogue: 0,0:02:48.75,0:02:50.94,Default,,0000,0000,0000,,Last, you just have to do Dialogue: 0,0:02:50.94,0:02:52.87,Default,,0000,0000,0000,,whatever you wanted to do\Ninside the for loop. Dialogue: 0,0:02:52.87,0:02:54.67,Default,,0000,0000,0000,,So we'll go ahead\Nand write that text there. Dialogue: 0,0:02:54.67,0:02:57.34,Default,,0000,0000,0000,,And there we have have it,\Npineapple pizza is the best. Dialogue: 0,0:02:57.34,0:02:59.23,Default,,0000,0000,0000,,Everybody will know. Dialogue: 0,0:02:59.24,0:03:03.40,Default,,0000,0000,0000,,Now, let's think a bit more closely about\Nwhat's happening with this for loop. Dialogue: 0,0:03:03.40,0:03:07.77,Default,,0000,0000,0000,,If we wanted to change where it started,\Nwell, we just change the start here. Dialogue: 0,0:03:07.77,0:03:11.17,Default,,0000,0000,0000,,If we wanted to change where it ended,\Nwe just change this endpoint. Dialogue: 0,0:03:11.17,0:03:16.03,Default,,0000,0000,0000,,If we wanted to change the spacing,\Nwe would just change the increment number. Dialogue: 0,0:03:17.37,0:03:20.64,Default,,0000,0000,0000,,We can also change\N--just like with the while loop -- Dialogue: 0,0:03:20.64,0:03:23.44,Default,,0000,0000,0000,,what value we have for {\i1}x{\i0}. Dialogue: 0,0:03:28.45,0:03:30.54,Default,,0000,0000,0000,,One thing that can be confusing\Nabout a for loop Dialogue: 0,0:03:30.87,0:03:32.97,Default,,0000,0000,0000,,is to remember what these semicolons do. Dialogue: 0,0:03:32.97,0:03:37.16,Default,,0000,0000,0000,,You need to remember that they always need\Nto be there to separate the 3 parts. Dialogue: 0,0:03:37.17,0:03:38.87,Default,,0000,0000,0000,,If we don't have them there, Dialogue: 0,0:03:38.87,0:03:41.46,Default,,0000,0000,0000,,then we are going to get\Nsome weird error messages Dialogue: 0,0:03:41.47,0:03:42.60,Default,,0000,0000,0000,,about our for loop, Dialogue: 0,0:03:42.60,0:03:45.63,Default,,0000,0000,0000,,so whenever you see\Nthat just double-check that you have them. Dialogue: 0,0:03:45.64,0:03:48.98,Default,,0000,0000,0000,,You also may get overenthusiastic\Nand add an extra one at the end, Dialogue: 0,0:03:48.98,0:03:53.65,Default,,0000,0000,0000,,but if you just remember that semicolons\Nare only there to separate the 3 parts, Dialogue: 0,0:03:53.65,0:03:56.87,Default,,0000,0000,0000,,then we can say that this last semicolon\Nisn't separating anything, Dialogue: 0,0:03:56.87,0:04:01.16,Default,,0000,0000,0000,,it's just trailing on there, so we can\Nget rid of it because we don't need it. Dialogue: 0,0:04:01.16,0:04:04.33,Default,,0000,0000,0000,,Now, I know you're probably\Ngetting a little bit tired of seeing Dialogue: 0,0:04:04.33,0:04:06.50,Default,,0000,0000,0000,,these for loops getting converted\Ninto while loops and back, Dialogue: 0,0:04:06.70,0:04:08.64,Default,,0000,0000,0000,,let's just do it one more time, Dialogue: 0,0:04:08.64,0:04:10.56,Default,,0000,0000,0000,,so you can see a for loop really is Dialogue: 0,0:04:10.56,0:04:13.29,Default,,0000,0000,0000,,just another way of writing\Na simple kind of a while loop, Dialogue: 0,0:04:13.29,0:04:15.77,Default,,0000,0000,0000,,and you'll be really confident\Nin understanding Dialogue: 0,0:04:15.77,0:04:18.50,Default,,0000,0000,0000,,how to go from a for loop\Nback into a while loop. Dialogue: 0,0:04:18.80,0:04:22.86,Default,,0000,0000,0000,,You can do that with any kind\Nof for loop, not just this one. Dialogue: 0,0:04:22.86,0:04:24.85,Default,,0000,0000,0000,,The first thing that we do is think about Dialogue: 0,0:04:24.86,0:04:28.19,Default,,0000,0000,0000,,where to put this first value\Nthat we have inside of our for loop. Dialogue: 0,0:04:28.70,0:04:31.06,Default,,0000,0000,0000,,Since it's just initializing\Nthis variable, Dialogue: 0,0:04:31.06,0:04:34.06,Default,,0000,0000,0000,,remember that it has to go outside\Nnot inside the while loop, Dialogue: 0,0:04:34.06,0:04:35.80,Default,,0000,0000,0000,,and it should go before as well, Dialogue: 0,0:04:35.80,0:04:38.36,Default,,0000,0000,0000,,because we need to be using it\Nduring our while loop. Dialogue: 0,0:04:38.37,0:04:40.63,Default,,0000,0000,0000,,Then you think about\Nwhere should this condition go, Dialogue: 0,0:04:40.63,0:04:41.83,Default,,0000,0000,0000,,--that's pretty easy--, Dialogue: 0,0:04:41.83,0:04:45.94,Default,,0000,0000,0000,,usually the stop condition,\Nor the repeat until condition, Dialogue: 0,0:04:45.94,0:04:47.57,Default,,0000,0000,0000,,needs to go inside here, Dialogue: 0,0:04:47.57,0:04:51.87,Default,,0000,0000,0000,,we're gonna say while {\i1}y{\i0}\Nis less than 313 we'll keep going. Dialogue: 0,0:04:52.87,0:04:54.33,Default,,0000,0000,0000,,Finally, the change. Dialogue: 0,0:04:54.33,0:04:56.57,Default,,0000,0000,0000,,We always put the change\Nat the end of the while loop, Dialogue: 0,0:04:56.57,0:04:59.64,Default,,0000,0000,0000,,in the loops that we've seen,\Nso we should go and do that here. Dialogue: 0,0:04:59.64,0:05:04.24,Default,,0000,0000,0000,,Now, we just need to move this text call\Ninside, and there we have it. Dialogue: 0,0:05:04.70,0:05:09.80,Default,,0000,0000,0000,,Comment out this whole loop, and you see\Nthat we made the exact same thing happen. Dialogue: 0,0:05:11.56,0:05:13.18,Default,,0000,0000,0000,,Hopefully by now, you can see Dialogue: 0,0:05:13.18,0:05:15.68,Default,,0000,0000,0000,,that this new for loop\Nisn't actually necessary. Dialogue: 0,0:05:16.13,0:05:20.10,Default,,0000,0000,0000,,We could go through our entire programming\Nlives writing loops that looked like this. Dialogue: 0,0:05:20.10,0:05:23.97,Default,,0000,0000,0000,,But, it is a bit impressive\Nhow much more concise this for loop is. Dialogue: 0,0:05:23.97,0:05:27.60,Default,,0000,0000,0000,,It makes it a bit more clear,\Nonce you're used to it, what's going on. Dialogue: 0,0:05:27.60,0:05:29.40,Default,,0000,0000,0000,,How we're starting a variable, Dialogue: 0,0:05:29.40,0:05:31.09,Default,,0000,0000,0000,,we're saying how long to keep going, Dialogue: 0,0:05:31.09,0:05:33.37,Default,,0000,0000,0000,,and then changing that variable here. Dialogue: 0,0:05:33.37,0:05:36.73,Default,,0000,0000,0000,,So, if you really hate for loops,\Nyou don't have to use them ever, Dialogue: 0,0:05:36.73,0:05:38.46,Default,,0000,0000,0000,,but you should be used to seeing them. Dialogue: 0,0:05:38.46,0:05:40.21,Default,,0000,0000,0000,,And if they're confusing, just remember Dialogue: 0,0:05:40.21,0:05:42.96,Default,,0000,0000,0000,,you can convert them back\Ninto a while loop like this. Dialogue: 0,0:05:42.96,0:05:46.50,Default,,0000,0000,0000,,And if you're feeling adventurous,\Ntry using a for loop in your new program, Dialogue: 0,0:05:46.50,0:05:48.66,Default,,0000,0000,0000,,or going back to some of your old,\Nsimple while loops, Dialogue: 0,0:05:48.66,0:05:52.10,Default,,0000,0000,0000,,and converting them\Ninto for loops, if appropriate. Dialogue: 0,0:05:52.10,0:05:56.70,Default,,0000,0000,0000,,A final note: you cannot always convert\Na while loop into a for loop, Dialogue: 0,0:05:56.70,0:05:59.30,Default,,0000,0000,0000,,only when they're\Nin a really simple form like this, Dialogue: 0,0:05:59.30,0:06:01.94,Default,,0000,0000,0000,,where it's really clear\Nwhat variable you're starting out with, Dialogue: 0,0:06:01.94,0:06:05.53,Default,,0000,0000,0000,,how long it's going for,\Nand how it's changing.