[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.70,0:00:07.26,Default,,0000,0000,0000,,Let's talk about loops. So I have a while loop here, and with just a few lines of code, I can write this message all the way down the screen. Dialogue: 0,0:00:07.26,0:00:10.34,Default,,0000,0000,0000,,If I go ahead and change the message like this, you know, to make it better Dialogue: 0,0:00:10.34,0:00:11.93,Default,,0000,0000,0000,,all of them change. Dialogue: 0,0:00:11.93,0:00:15.83,Default,,0000,0000,0000,,So how is this working? Well, we can revisit this code in a moment Dialogue: 0,0:00:15.83,0:00:19.42,Default,,0000,0000,0000,,But first let's take a step back and think about how we might write this program Dialogue: 0,0:00:19.42,0:00:22.47,Default,,0000,0000,0000,,using only what we know so far without using loops. Dialogue: 0,0:00:22.47,0:00:26.75,Default,,0000,0000,0000,,So, to do that, we're really just gonna write a bunch of text over and over and over again right? Dialogue: 0,0:00:26.75,0:00:32.59,Default,,0000,0000,0000,,We're gonna say text, message, I'll put it in the first place, and now it's just a matter of repeating this Dialogue: 0,0:00:32.59,0:00:36.34,Default,,0000,0000,0000,,enough times so that eventually we get all the way to the bottom. Dialogue: 0,0:00:36.34,0:00:38.95,Default,,0000,0000,0000,,And that's gonna take a lot of work, right? Dialogue: 0,0:00:38.95,0:00:42.75,Default,,0000,0000,0000,,Because the bottom is really far away. And it's even worse if you Dialogue: 0,0:00:42.75,0:00:46.76,Default,,0000,0000,0000,,then point out to me, hey, this wasn't actually 70, it needs to be closer, it needs to be at like Dialogue: 0,0:00:46.76,0:00:50.71,Default,,0000,0000,0000,,60. And now that affects this one because it also needs to be smaller Dialogue: 0,0:00:50.71,0:00:55.04,Default,,0000,0000,0000,,and all the way on, the more calls of text we have. Dialogue: 0,0:00:55.04,0:00:58.26,Default,,0000,0000,0000,,And in fact, this way it's gonna take even longer to get to the bottom. Dialogue: 0,0:00:58.26,0:01:02.18,Default,,0000,0000,0000,,So, this is really a pain, and thankfully we have loops to help us Dialogue: 0,0:01:02.18,0:01:06.21,Default,,0000,0000,0000,,From now on, any time you see repetitive code like this, your first thought should be Dialogue: 0,0:01:06.21,0:01:10.09,Default,,0000,0000,0000,,"Could I use a loop?" A loop will let us repeat this piece of code over and over and Dialogue: 0,0:01:10.09,0:01:14.25,Default,,0000,0000,0000,,over again, making just little changes each time. So here's Dialogue: 0,0:01:14.25,0:01:17.87,Default,,0000,0000,0000,,how we would rewrite this code with a loop. To get started, we need to type "while", Dialogue: 0,0:01:17.87,0:01:22.09,Default,,0000,0000,0000,,the parentheses, and the curly braces. We're gonna get this message, but Dialogue: 0,0:01:22.09,0:01:26.30,Default,,0000,0000,0000,,it's just because we're not done yet. Don't worry, it'll go away when we finish. Dialogue: 0,0:01:26.30,0:01:30.81,Default,,0000,0000,0000,,So every time you write a loop you need to answer three key questions. Dialogue: 0,0:01:30.82,0:01:33.37,Default,,0000,0000,0000,,And here they are: so, Dialogue: 0,0:01:33.37,0:01:37.37,Default,,0000,0000,0000,,the first question is, "What do I want to repeat?" And Dialogue: 0,0:01:37.37,0:01:41.47,Default,,0000,0000,0000,,whatever we want to repeat needs to go in-between these curlies. Dialogue: 0,0:01:41.47,0:01:46.20,Default,,0000,0000,0000,,So we want to repeat the "text" call in this case, so go ahead and put that in here. Dialogue: 0,0:01:46.20,0:01:49.57,Default,,0000,0000,0000,,But it's a little bit silly, right? Because right now we're just gonna be repeating Dialogue: 0,0:01:49.57,0:01:53.48,Default,,0000,0000,0000,,the same call of text over and over, which is not really any good right, we need something to be Dialogue: 0,0:01:53.48,0:01:57.71,Default,,0000,0000,0000,,changing. That brings us to question two, which is "What do I want to change each time?" Dialogue: 0,0:01:57.71,0:02:01.42,Default,,0000,0000,0000,,So we want to change this "y" position, right? We want it to become 60 and then we want it Dialogue: 0,0:02:01.42,0:02:05.50,Default,,0000,0000,0000,,to become 80. So we'll make that into a variable instead. Dialogue: 0,0:02:05.50,0:02:09.48,Default,,0000,0000,0000,,Called y, because it's the y position. So we'll go ahead and declare a variable Dialogue: 0,0:02:09.48,0:02:13.38,Default,,0000,0000,0000,,up here. I'll start it at 40. And now, finally, we just need to Dialogue: 0,0:02:13.38,0:02:17.20,Default,,0000,0000,0000,,be changing y. We can do that down here, we can say "y gets y + Dialogue: 0,0:02:17.20,0:02:21.09,Default,,0000,0000,0000,,"20" and it will be getting bigger every time. And in fact, we can use Dialogue: 0,0:02:21.09,0:02:24.74,Default,,0000,0000,0000,,our lesson from incrementing shortcuts here. We can just go ahead Dialogue: 0,0:02:24.74,0:02:29.09,Default,,0000,0000,0000,,and use the shortcut. So, this is fantastic, and we only Dialogue: 0,0:02:29.09,0:02:32.70,Default,,0000,0000,0000,,need to do question three now, which is "How long should we be repeating this?" Dialogue: 0,0:02:32.70,0:02:36.53,Default,,0000,0000,0000,,Well we want to do this over and over and over again, but we don't really want to do it forever, right? Dialogue: 0,0:02:36.53,0:02:41.25,Default,,0000,0000,0000,,If we do it forever, first, that's a really long time to wait, and second, it might even crash your browser. Dialogue: 0,0:02:41.25,0:02:44.47,Default,,0000,0000,0000,,But hopefully not. So really, we only Dialogue: 0,0:02:44.47,0:02:48.37,Default,,0000,0000,0000,,want to do this until we get to the bottom of the page, right? Which means we wanna do it Dialogue: 0,0:02:48.37,0:02:52.47,Default,,0000,0000,0000,,as long as y is less than 400. So we just put that in here, and there Dialogue: 0,0:02:52.47,0:02:56.43,Default,,0000,0000,0000,,we have it! We have this message being written all the way down the screen. Dialogue: 0,0:02:56.43,0:02:59.92,Default,,0000,0000,0000,,And you can see that this is way simpler than our previous approach, which, you know, Dialogue: 0,0:02:59.92,0:03:04.18,Default,,0000,0000,0000,,took us about as long to write, but we weren't even a quarter of the way finished. Dialogue: 0,0:03:04.18,0:03:08.09,Default,,0000,0000,0000,,So we can get rid of that, and there we have our program. Dialogue: 0,0:03:08.09,0:03:11.71,Default,,0000,0000,0000,,Now, let's try to get a better understanding of what's going on. To do that, I'm going Dialogue: 0,0:03:11.71,0:03:15.63,Default,,0000,0000,0000,,to print out y each time. I'm gonna say "y is now" and then down here Dialogue: 0,0:03:15.63,0:03:19.41,Default,,0000,0000,0000,,I'll just tack y onto the end of the message so we can see it. Dialogue: 0,0:03:19.41,0:03:23.34,Default,,0000,0000,0000,,So at the moment, the value is changing by 20, and we can change Dialogue: 0,0:03:23.34,0:03:27.26,Default,,0000,0000,0000,,that just by changing this variable here. Or you can make it, you know, like, Dialogue: 0,0:03:27.26,0:03:31.43,Default,,0000,0000,0000,,50. And now, now they're changing by 50. Similarly, Dialogue: 0,0:03:31.43,0:03:35.58,Default,,0000,0000,0000,,you can go ahead and play with these other values and have them change. And Dialogue: 0,0:03:35.58,0:03:39.20,Default,,0000,0000,0000,,you can see how that affects, you know, where the program stops. Dialogue: 0,0:03:39.20,0:03:44.01,Default,,0000,0000,0000,,So, to understand this, you can think of it sort of Dialogue: 0,0:03:44.01,0:03:47.34,Default,,0000,0000,0000,,like an "if" statement actually. We have our boolean expression in here Dialogue: 0,0:03:47.34,0:03:52.01,Default,,0000,0000,0000,,just like you learned. And then, we do the body of the, um, the statement Dialogue: 0,0:03:52.01,0:03:55.47,Default,,0000,0000,0000,,this part, only if the boolean is true, and otherwise we just jump to Dialogue: 0,0:03:55.47,0:03:59.09,Default,,0000,0000,0000,,the end. But what's interesting is that with a while loop we actually Dialogue: 0,0:03:59.09,0:04:03.86,Default,,0000,0000,0000,,have this secret thing going on at the bottom which says "go back to the start" Dialogue: 0,0:04:03.86,0:04:07.20,Default,,0000,0000,0000,,"of the loop". And what this secret instruction means Dialogue: 0,0:04:07.20,0:04:11.07,Default,,0000,0000,0000,,is that instead of leaving and just keeping on going, like with an "if" Dialogue: 0,0:04:11.07,0:04:15.03,Default,,0000,0000,0000,,statement, every time we do the loop body we're going to go back and check Dialogue: 0,0:04:15.03,0:04:19.20,Default,,0000,0000,0000,,if the condition is still true. And if it is, we're just going to repeat one Dialogue: 0,0:04:19.20,0:04:23.24,Default,,0000,0000,0000,,more time. And just like you might have guessed, the second time we repeat, we're gonna do the same thing... Dialogue: 0,0:04:23.24,0:04:27.09,Default,,0000,0000,0000,,we're gonna check hey, you know, go back to the start. Is y still less than Dialogue: 0,0:04:27.09,0:04:31.56,Default,,0000,0000,0000,,279? And if it is, we'll repeat one more time and keep checking. Dialogue: 0,0:04:31.56,0:04:34.59,Default,,0000,0000,0000,,And if it's not, we go back to the start here. And finally, Dialogue: 0,0:04:34.59,0:04:38.48,Default,,0000,0000,0000,,we'll get to escape and keep going with our program. Dialogue: 0,0:04:38.48,0:04:42.70,Default,,0000,0000,0000,,So great, there are a lot more interesting ways of using loops we're going to learn about soon, but Dialogue: 0,0:04:42.70,0:04:46.70,Default,,0000,0000,0000,,for now, you are off to an excellent start.