< Return to Video

Intro to While Loops (Video Version)

  • 0:01 - 0:07
    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.
  • 0:07 - 0:10
    If I go ahead and change the message like this, you know, to make it better
  • 0:10 - 0:12
    all of them change.
  • 0:12 - 0:16
    So how is this working? Well, we can revisit this code in a moment
  • 0:16 - 0:19
    But first let's take a step back and think about how we might write this program
  • 0:19 - 0:22
    using only what we know so far without using loops.
  • 0:22 - 0:27
    So, to do that, we're really just gonna write a bunch of text over and over and over again right?
  • 0:27 - 0:33
    We're gonna say text, message, I'll put it in the first place, and now it's just a matter of repeating this
  • 0:33 - 0:36
    enough times so that eventually we get all the way to the bottom.
  • 0:36 - 0:39
    And that's gonna take a lot of work, right?
  • 0:39 - 0:43
    Because the bottom is really far away. And it's even worse if you
  • 0:43 - 0:47
    then point out to me, hey, this wasn't actually 70, it needs to be closer, it needs to be at like
  • 0:47 - 0:51
    60. And now that affects this one because it also needs to be smaller
  • 0:51 - 0:55
    and all the way on, the more calls of text we have.
  • 0:55 - 0:58
    And in fact, this way it's gonna take even longer to get to the bottom.
  • 0:58 - 1:02
    So, this is really a pain, and thankfully we have loops to help us
  • 1:02 - 1:06
    From now on, any time you see repetitive code like this, your first thought should be
  • 1:06 - 1:10
    "Could I use a loop?" A loop will let us repeat this piece of code over and over and
  • 1:10 - 1:14
    over again, making just little changes each time. So here's
  • 1:14 - 1:18
    how we would rewrite this code with a loop. To get started, we need to type "while",
  • 1:18 - 1:22
    the parentheses, and the curly braces. We're gonna get this message, but
  • 1:22 - 1:26
    it's just because we're not done yet. Don't worry, it'll go away when we finish.
  • 1:26 - 1:31
    So every time you write a loop you need to answer three key questions.
  • 1:31 - 1:33
    And here they are: so,
  • 1:33 - 1:37
    the first question is, "What do I want to repeat?" And
  • 1:37 - 1:41
    whatever we want to repeat needs to go in-between these curlies.
  • 1:41 - 1:46
    So we want to repeat the "text" call in this case, so go ahead and put that in here.
  • 1:46 - 1:50
    But it's a little bit silly, right? Because right now we're just gonna be repeating
  • 1:50 - 1:53
    the same call of text over and over, which is not really any good right, we need something to be
  • 1:53 - 1:58
    changing. That brings us to question two, which is "What do I want to change each time?"
  • 1:58 - 2:01
    So we want to change this "y" position, right? We want it to become 60 and then we want it
  • 2:01 - 2:06
    to become 80. So we'll make that into a variable instead.
  • 2:06 - 2:09
    Called y, because it's the y position. So we'll go ahead and declare a variable
  • 2:09 - 2:13
    up here. I'll start it at 40. And now, finally, we just need to
  • 2:13 - 2:17
    be changing y. We can do that down here, we can say "y gets y +
  • 2:17 - 2:21
    "20" and it will be getting bigger every time. And in fact, we can use
  • 2:21 - 2:25
    our lesson from incrementing shortcuts here. We can just go ahead
  • 2:25 - 2:29
    and use the shortcut. So, this is fantastic, and we only
  • 2:29 - 2:33
    need to do question three now, which is "How long should we be repeating this?"
  • 2:33 - 2:37
    Well we want to do this over and over and over again, but we don't really want to do it forever, right?
  • 2:37 - 2:41
    If we do it forever, first, that's a really long time to wait, and second, it might even crash your browser.
  • 2:41 - 2:44
    But hopefully not. So really, we only
  • 2:44 - 2:48
    want to do this until we get to the bottom of the page, right? Which means we wanna do it
  • 2:48 - 2:52
    as long as y is less than 400. So we just put that in here, and there
  • 2:52 - 2:56
    we have it! We have this message being written all the way down the screen.
  • 2:56 - 3:00
    And you can see that this is way simpler than our previous approach, which, you know,
  • 3:00 - 3:04
    took us about as long to write, but we weren't even a quarter of the way finished.
  • 3:04 - 3:08
    So we can get rid of that, and there we have our program.
  • 3:08 - 3:12
    Now, let's try to get a better understanding of what's going on. To do that, I'm going
  • 3:12 - 3:16
    to print out y each time. I'm gonna say "y is now" and then down here
  • 3:16 - 3:19
    I'll just tack y onto the end of the message so we can see it.
  • 3:19 - 3:23
    So at the moment, the value is changing by 20, and we can change
  • 3:23 - 3:27
    that just by changing this variable here. Or you can make it, you know, like,
  • 3:27 - 3:31
    50. And now, now they're changing by 50. Similarly,
  • 3:31 - 3:36
    you can go ahead and play with these other values and have them change. And
  • 3:36 - 3:39
    you can see how that affects, you know, where the program stops.
  • 3:39 - 3:44
    So, to understand this, you can think of it sort of
  • 3:44 - 3:47
    like an "if" statement actually. We have our boolean expression in here
  • 3:47 - 3:52
    just like you learned. And then, we do the body of the, um, the statement
  • 3:52 - 3:55
    this part, only if the boolean is true, and otherwise we just jump to
  • 3:55 - 3:59
    the end. But what's interesting is that with a while loop we actually
  • 3:59 - 4:04
    have this secret thing going on at the bottom which says "go back to the start"
  • 4:04 - 4:07
    "of the loop". And what this secret instruction means
  • 4:07 - 4:11
    is that instead of leaving and just keeping on going, like with an "if"
  • 4:11 - 4:15
    statement, every time we do the loop body we're going to go back and check
  • 4:15 - 4:19
    if the condition is still true. And if it is, we're just going to repeat one
  • 4:19 - 4:23
    more time. And just like you might have guessed, the second time we repeat, we're gonna do the same thing...
  • 4:23 - 4:27
    we're gonna check hey, you know, go back to the start. Is y still less than
  • 4:27 - 4:32
    279? And if it is, we'll repeat one more time and keep checking.
  • 4:32 - 4:35
    And if it's not, we go back to the start here. And finally,
  • 4:35 - 4:38
    we'll get to escape and keep going with our program.
  • 4:38 - 4:43
    So great, there are a lot more interesting ways of using loops we're going to learn about soon, but
  • 4:43 - 4:47
    for now, you are off to an excellent start.
Title:
Intro to While Loops (Video Version)
Description:

This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/

more » « less
Video Language:
English
Duration:
04:47

English subtitles

Revisions