< Return to Video

For Loops (Video Version)

  • 0:01 - 0:03
    Now that you know about while loops,
  • 0:03 - 0:05
    let's talk about
    another kind of loop, for loops.
  • 0:06 - 0:07
    Here's a for loop I made to praise
  • 0:07 - 0:11
    the most delicious kind of pizza,
    pineapple pizza.
  • 0:11 - 0:13
    You might think, "I liked while loops,"
  • 0:13 - 0:15
    "Why should I care about
    this new kind of loop?"
  • 0:15 - 0:17
    Well, soon you will see
    that a for loop is a way
  • 0:17 - 0:21
    to write simple kinds of while loops
    in a bit of a shorter way.
  • 0:21 - 0:23
    So, before we inspect this for loop,
  • 0:23 - 0:26
    let's go ahead and look at a while loop
    which will be a bit more familiar.
  • 0:26 - 0:29
    We'll get back to this for loop
    in a moment.
  • 0:29 - 0:31
    So, just like you saw
    in the intro to while loops,
  • 0:31 - 0:34
    We start with this variable y,
  • 0:34 - 0:37
    and we write a message
    using that location y.
  • 0:37 - 0:38
    We say that we want to keep going
  • 0:38 - 0:43
    as long as y is less than 300
    and change y by 40 each time.
  • 0:43 - 0:47
    Now, just to reinforce each of these,
    which should be review,
  • 0:47 - 0:51
    we can say that "Pineapple pizza is
    the best" is an important message,
  • 0:51 - 0:52
    so we probably want to be
    writing it more.
  • 0:52 - 0:55
    so we should start higher
    --so we change that--,
  • 0:55 - 0:58
    it should go on for longer,
    so we make it bigger,
  • 0:58 - 1:02
    and it should be spaced
    closer together --don't you think?--
  • 1:02 - 1:04
    so that's a little bit better.
  • 1:04 - 1:08
    Now, let's see how we can change
    this while loop into a for loop.
  • 1:08 - 1:10
    I'm going to comment out this while loop,
  • 1:10 - 1:14
    just so you can see that it's doing
    what I'm saying it's going to do.
  • 1:14 - 1:20
    And we'll create a for loop
    and some space to fill in later.
  • 1:20 - 1:23
    Something really important,
    kind of different for a for loop,
  • 1:23 - 1:26
    it's not just one thing that goes in here,
    it's actually three things.
  • 1:26 - 1:28
    We tell the computer that
  • 1:28 - 1:31
    by using these semicolons
    to separate the three parts,
  • 1:32 - 1:34
    we'll have one part here,
    one part here, one part here,
  • 1:34 - 1:37
    and then, the inside of the loop,
    just like we had before.
  • 1:37 - 1:40
    So, what goes in each of these parts?
  • 1:40 - 1:46
    You can think of it as a start,
    or initialization,
  • 1:46 - 1:48
    then we'll have a semicolon,
  • 1:48 - 1:51
    then, some sort of instructions
    on how long to repeat,
  • 1:51 - 1:56
    and then we're going to have
    some sort of change happening.
  • 1:57 - 2:00
    So, how does that correspond
    with this while loop?
  • 2:00 - 2:01
    Really concretely, we can say
  • 2:01 - 2:04
    that the start sets up
    this variable y here,
  • 2:04 - 2:09
    so let's copy and paste that over
    to this first part of the for loop.
  • 2:09 - 2:13
    Similarly, we can say
    this middle part is saying
  • 2:13 - 2:17
    how long we should keep going for
    and that goes in the middle.
  • 2:17 - 2:25
    We say the change happens at the end here,
    we'll put that at the end of the for loop.
  • 2:26 - 2:30
    These three parts always have to occur
    in this order in your for loop.
  • 2:30 - 2:32
    You always have to start by saying
  • 2:32 - 2:34
    this is what the variable
    should start out as.
  • 2:34 - 2:36
    Here it's 27.
  • 2:36 - 2:42
    Then you say how long to keep going for.
    Repeat as long as it is less than 354.
  • 2:42 - 2:44
    Then you need to put
    how you are going to be changing things.
  • 2:44 - 2:48
    So here we are going to be changing
    by increasing y by 24.
  • 2:49 - 2:51
    Last, you just have to do
  • 2:51 - 2:53
    whatever you wanted to do
    inside the for loop.
  • 2:53 - 2:55
    So we'll go ahead
    and write that text there.
  • 2:55 - 2:57
    And there we have have it,
    pineapple pizza is the best.
  • 2:57 - 2:59
    Everybody will know.
  • 2:59 - 3:03
    Now, let's think a bit more closely about
    what's happening with this for loop.
  • 3:03 - 3:08
    If we wanted to change where it started,
    well, we just change the start here.
  • 3:08 - 3:11
    If we wanted to change where it ended,
    we just change this endpoint.
  • 3:11 - 3:16
    If we wanted to change the spacing,
    we would just change the increment number.
  • 3:17 - 3:21
    We can also change
    --just like with the while loop --
  • 3:21 - 3:23
    what value we have for x.
  • 3:28 - 3:31
    One thing that can be confusing
    about a for loop
  • 3:31 - 3:33
    is to remember what these semicolons do.
  • 3:33 - 3:37
    You need to remember that they always need
    to be there to separate the 3 parts.
  • 3:37 - 3:39
    If we don't have them there,
  • 3:39 - 3:41
    then we are going to get
    some weird error messages
  • 3:41 - 3:43
    about our for loop,
  • 3:43 - 3:46
    so whenever you see
    that just double-check that you have them.
  • 3:46 - 3:49
    You also may get overenthusiastic
    and add an extra one at the end,
  • 3:49 - 3:54
    but if you just remember that semicolons
    are only there to separate the 3 parts,
  • 3:54 - 3:57
    then we can say that this last semicolon
    isn't separating anything,
  • 3:57 - 4:01
    it's just trailing on there, so we can
    get rid of it because we don't need it.
  • 4:01 - 4:04
    Now, I know you're probably
    getting a little bit tired of seeing
  • 4:04 - 4:06
    these for loops getting converted
    into while loops and back,
  • 4:07 - 4:09
    let's just do it one more time,
  • 4:09 - 4:11
    so you can see a for loop really is
  • 4:11 - 4:13
    just another way of writing
    a simple kind of a while loop,
  • 4:13 - 4:16
    and you'll be really confident
    in understanding
  • 4:16 - 4:18
    how to go from a for loop
    back into a while loop.
  • 4:19 - 4:23
    You can do that with any kind
    of for loop, not just this one.
  • 4:23 - 4:25
    The first thing that we do is think about
  • 4:25 - 4:28
    where to put this first value
    that we have inside of our for loop.
  • 4:29 - 4:31
    Since it's just initializing
    this variable,
  • 4:31 - 4:34
    remember that it has to go outside
    not inside the while loop,
  • 4:34 - 4:36
    and it should go before as well,
  • 4:36 - 4:38
    because we need to be using it
    during our while loop.
  • 4:38 - 4:41
    Then you think about
    where should this condition go,
  • 4:41 - 4:42
    --that's pretty easy--,
  • 4:42 - 4:46
    usually the stop condition,
    or the repeat until condition,
  • 4:46 - 4:48
    needs to go inside here,
  • 4:48 - 4:52
    we're gonna say while y
    is less than 313 we'll keep going.
  • 4:53 - 4:54
    Finally, the change.
  • 4:54 - 4:57
    We always put the change
    at the end of the while loop,
  • 4:57 - 5:00
    in the loops that we've seen,
    so we should go and do that here.
  • 5:00 - 5:04
    Now, we just need to move this text call
    inside, and there we have it.
  • 5:05 - 5:10
    Comment out this whole loop, and you see
    that we made the exact same thing happen.
  • 5:12 - 5:13
    Hopefully by now, you can see
  • 5:13 - 5:16
    that this new for loop
    isn't actually necessary.
  • 5:16 - 5:20
    We could go through our entire programming
    lives writing loops that looked like this.
  • 5:20 - 5:24
    But, it is a bit impressive
    how much more concise this for loop is.
  • 5:24 - 5:28
    It makes it a bit more clear,
    once you're used to it, what's going on.
  • 5:28 - 5:29
    How we're starting a variable,
  • 5:29 - 5:31
    we're saying how long to keep going,
  • 5:31 - 5:33
    and then changing that variable here.
  • 5:33 - 5:37
    So, if you really hate for loops,
    you don't have to use them ever,
  • 5:37 - 5:38
    but you should be used to seeing them.
  • 5:38 - 5:40
    And if they're confusing, just remember
  • 5:40 - 5:43
    you can convert them back
    into a while loop like this.
  • 5:43 - 5:46
    And if you're feeling adventurous,
    try using a for loop in your new program,
  • 5:47 - 5:49
    or going back to some of your old,
    simple while loops,
  • 5:49 - 5:52
    and converting them
    into for loops, if appropriate.
  • 5:52 - 5:57
    A final note: you cannot always convert
    a while loop into a for loop,
  • 5:57 - 5:59
    only when they're
    in a really simple form like this,
  • 5:59 - 6:02
    where it's really clear
    what variable you're starting out with,
  • 6:02 - 6:06
    how long it's going for,
    and how it's changing.
Title:
For Loops (Video Version)
Description:

more » « less
Video Language:
English
Duration:
06:07

English subtitles

Revisions