Return to Video

Nested For Loops (Video Version)

  • 0:02 - 0:04
    A gem, a pretty sweet gem!
  • 0:04 - 0:06
    But you know what's better
    than a single gem?
  • 0:06 - 0:08
    Would be a whole row of gems!
  • 0:08 - 0:10
    And of course, now we know
  • 0:10 - 0:13
    the best way to make a row of gems
    would be with a loop.
  • 0:13 - 0:16
    So let's use a for loop
    to draw 12 gems in a row.
  • 0:16 - 0:20
    Going from left to right
    across the screen.
  • 0:20 - 0:22
    Ohh like that.
  • 0:22 - 0:26
    So that is a for (var i equal 0
  • 0:26 - 0:31
    i is less than 12; i plus plus).
  • 0:31 - 0:35
    And then we will take this line
    and move it inside here.
  • 0:36 - 0:38
    So now we have 12 gems,
  • 0:38 - 0:40
    but they're actually all piled
    right on top on each other.
  • 0:40 - 0:43
    Remember, we want them
    going across the screen.
  • 0:43 - 0:46
    That means we want
    to be changing the x.
  • 0:46 - 0:50
    And 36 is the x right now,
    but we want it to be different each time.
  • 0:50 - 0:52
    That means we want it
    to be dependent on i.
  • 0:52 - 0:56
    So what we can do
    is simply say; i times 36.
  • 0:56 - 1:02
    So the first one is at 0,
    and then 36, then 72, and etc., etc.
  • 1:03 - 1:05
    Cool! Now we have a row of gems.
  • 1:05 - 1:06
    And this kind of reminds me
  • 1:06 - 1:08
    of those those scenes
    in Indiana Jones or Aladdin,
  • 1:08 - 1:11
    where the hero discovers
    that underground treasure trove of gems,
  • 1:11 - 1:14
    but they usually find
    way more gems than this.
  • 1:14 - 1:17
    Not just a row of gems
    but a pile of gems.
  • 1:17 - 1:20
    So how could we actually make gems
  • 1:20 - 1:24
    going all the way down the screen too?
  • 1:25 - 1:30
    Well, we could start by just repeating
    the for loop, and copy pasting it,
  • 1:31 - 1:33
    and then changing this y each time.
  • 1:35 - 1:38
    And, so we'll change it
    to 60 and then 90.
  • 1:38 - 1:42
    So now we have three rows of gems
    --and that's cool--
  • 1:42 - 1:44
    but this is also getting really boring
  • 1:44 - 1:49
    because all I'm doing is copying
    and pasting and changing this one little thing.
  • 1:49 - 1:53
    And normally, in the past, when we found
    ourselves writing repetitive code like that
  • 1:53 - 1:56
    we would be like, "Oh, maybe
    we should just use a loop instead,"
  • 1:57 - 1:59
    but we are already using a loop;
  • 1:59 - 2:01
    so what's the solution
    to avoid writing this,
  • 2:01 - 2:04
    you know, doing
    this repetitive copy-paste?
  • 2:04 - 2:10
    Well, it is something we call
    "nested for loops": loop within a loop.
  • 2:10 - 2:12
    So what we are going to do
    is make an outer loop,
  • 2:12 - 2:15
    and that's what's going to take care
    of going down the screen,
  • 2:15 - 2:19
    and then our inner loop is going
    to keep taking care of what it's doing now
  • 2:19 - 2:21
    which is going from left to right.
  • 2:22 - 2:23
    Let me show you what I mean.
  • 2:24 - 2:30
    So for - and we use a different
    variable j since we're already using i
  • 2:30 - 2:36
    so for(var j equals 0, and we'll say
    j less than 13 and j plus plus).
  • 2:37 - 2:41
    So that is going to be our outer loop,
    in charge of going top to bottom.
  • 2:42 - 2:45
    And then we're just going to take
    one of our previous for loops,
  • 2:45 - 2:49
    and put it inside there
    and fix the indenting,
  • 2:50 - 2:52
    and we'll delete these old ones.
  • 2:53 - 2:55
    So now what we have
  • 2:55 - 2:58
    is we've got them all piled
    on top on the same row.
  • 2:59 - 3:02
    So the thing is that we want
    to change the y, right?
  • 3:02 - 3:04
    That's what we were changing before
    when we were copying pasting,
  • 3:04 - 3:06
    and right now, the y is always 90.
  • 3:06 - 3:09
    We want to y to change for each row.
  • 3:09 - 3:15
    So just the way x is dependent on i,
    we want the y to be dependent on j.
  • 3:16 - 3:19
    So we can go ahead and change this
  • 3:19 - 3:24
    to something like, maybe j times 30.
  • 3:25 - 3:29
    Tada! Yeahh! So many gems! Allright!
  • 3:30 - 3:32
    Lets walk through what this does again.
  • 3:32 - 3:37
    The outer loop creates this variable j
    and increments it up to 13.
  • 3:38 - 3:43
    In each execution of that outer loop,
    it runs this inner loop.
  • 3:44 - 3:48
    The inner loop creates
    the variable i which goes up to 12.
  • 3:49 - 3:50
    And for each execution of the inner loop,
  • 3:50 - 3:54
    it draws an image on the x and y
    which are based off of i and j.
  • 3:55 - 3:59
    And this i changes a lot more
    frequently than the j because of that.
  • 4:00 - 4:01
    To try and understand this even better,
  • 4:01 - 4:04
    let's try and actually visualize
    the i and j values.
  • 4:05 - 4:09
    So what I will do is comment out image,
  • 4:09 - 4:12
    and then set a fill color,
  • 4:12 - 4:15
    and I'm going to use a text command
    to show the value of j.
  • 4:15 - 4:20
    So text j and then I will put it
    at the appropriate spot here.
  • 4:22 - 4:26
    Now we can see j going from 0 to 12.
  • 4:26 - 4:30
    This is basically where our rows of gems
    were positioned as well.
  • 4:32 - 4:35
    And now we will visualize "i,
    and see how that changes.
  • 4:35 - 4:39
    So for i, let's make it a different color.
  • 4:43 - 4:46
    Then we will go put the i somewhere.
  • 4:46 - 4:49
    And we will change its x
    so that it goes across the screen.
  • 4:49 - 4:53
    We will do the same thing for the y.
  • 4:55 - 5:00
    Now we can see that the i
    is going from 0 to 11.
  • 5:00 - 5:04
    And the i, as I was saying,
    changes a lot more frequently.
  • 5:04 - 5:09
    And this line of code gets executed
    a lot more times than this line of code,
  • 5:09 - 5:14
    because this line of code is executed
    for every execution of this inner for loop
  • 5:14 - 5:16
    whereas this line of code
    only gets executed
  • 5:16 - 5:18
    for every execution of the outer loop.
  • 5:20 - 5:22
    So this visualizing of the i and j,
  • 5:22 - 5:24
    hopefully helps you understand
  • 5:24 - 5:26
    what's going on with
    these nested for loops better.
  • 5:27 - 5:30
    Now let's bring back our gems,
    because they are kind of cooler!
  • 5:30 - 5:34
    So there's a lot you can do
    with nested for loops
  • 5:34 - 5:37
    If you just think about
    everything in the world
  • 5:37 - 5:40
    that looks like a two-dimensional grid,
    like a chess board, a quilt,
  • 5:40 - 5:44
    the stars on the US flag,
    cool patterns and wallpapers;
  • 5:45 - 5:48
    to start off your imagination,
    just play around with this code,
  • 5:48 - 5:50
    like by changing the image.
  • 5:50 - 5:53
    I will start off
    by changing it to a heart!
  • 5:54 - 5:58
    To show you how much
    I love nested for loops! Aww!
Title:
Nested For Loops (Video Version)
Description:

This is a video for subtitling purposes. See real version at: https://www.khanacademy.org/cs/nested-for-loops-2/4838213478776832

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

English subtitles

Revisions