< Return to Video

Looping through Arrays (Video Version)

  • 0:01 - 0:04
    We're back with a program
    that prints out my array of friends.
  • 0:04 - 0:07
    But there's something
    that really annoys me about it.
  • 0:07 - 0:09
    Every time I add a new friend
    to the array, I have to add
  • 0:09 - 0:11
    a new text command down here.
  • 0:11 - 0:13
    So let's say I add Winston.
  • 0:13 - 0:15
    Well, he doesn't automatically show up.
  • 0:15 - 0:20
    If I want him to show up, I have to go
    and say text(myFriends[3],
  • 0:20 - 0:22
    and then change the y position
    and then we see Winston.
  • 0:22 - 0:25
    So that's way too much work.
  • 0:25 - 0:28
    I just want it so that every time
    I add someone else to the array,
  • 0:28 - 0:31
    it does the text command automatically.
  • 0:31 - 0:34
    Well, do you remember
    when we learned loops?
  • 0:35 - 0:37
    Loops were a great way
    to repeat the same bit of code
  • 0:37 - 0:39
    many times in a row.
  • 0:39 - 0:41
    Like, if we wanted to have
    a bunch of trees in a row
  • 0:41 - 0:43
    or a bunch of balloons.
  • 0:43 - 0:47
    Well, as it turns out, loops are also
    a great way to run a bit of code
  • 0:47 - 0:49
    on each element in an array.
  • 0:50 - 0:54
    In fact, you'll use a loop almost
    every time you use an array.
  • 0:54 - 0:56
    They work really well together.
  • 0:57 - 1:01
    So lets use a loop to display
    my friends' names, instead of having
  • 1:01 - 1:03
    all these text commands
    to show you what I mean.
  • 1:03 - 1:06
    So we'll start with the three questions
    we always ask ourselves
  • 1:07 - 1:08
    when we're making a loop.
  • 1:08 - 1:11
    First, what do I want to repeat?
    Well, look up here. What's repeated?
  • 1:11 - 1:13
    The text command.
  • 1:13 - 1:16
    What do I want to change each time?
    Well, let me just look
  • 1:16 - 1:20
    and see what's different, the y position
    and the current index, right?
  • 1:20 - 1:25
    So the friend num and the y position.
  • 1:25 - 1:28
    And how long should we repeat?
    Well, we keep going
  • 1:28 - 1:31
    until there are no more friends.
  • 1:33 - 1:37
    So now we know what we want,
    and we can make our loop.
  • 1:37 - 1:41
    We start off with a counter variable
    to keep track of where we are in the loop.
  • 1:41 - 1:43
    So we'll say var friendNum = 0;
  • 1:44 - 1:47
    We're going to start with zero
    because remember that 0
  • 1:47 - 1:52
    is the first element in the array, not 1.
    Then we have our while loop.
  • 1:52 - 1:57
    So we'll say
    while(friendNum < my friends.length).
  • 1:58 - 2:02
    So we're gonna compare the current
    counter variable to the total number
  • 2:02 - 2:05
    of things in the array.
    Inside the loop, that's where
  • 2:05 - 2:07
    we use our text command.
  • 2:07 - 2:11
    So we say, text(myFriends[
    and then here, instead of a number,
  • 2:11 - 2:13
    we're gonna put friendNum
    because friendNum represents
  • 2:13 - 2:14
    the current number.
  • 2:14 - 2:18
    And then we'll just put
    one position for now.
  • 2:18 - 2:23
    This has given us a little infinite
    loop error because we haven't
  • 2:23 - 2:26
    actually changed anything
    about friendNum.
  • 2:26 - 2:29
    Remember, we need to increment
    friendNum each time, otherwise the loop
  • 2:29 - 2:32
    will go on forever
    because that condition's always true.
  • 2:32 - 2:36
    I see something happened.
    Let me comment out the old code
  • 2:36 - 2:38
    so I can really see what's happened.
  • 2:38 - 2:41
    What we have is we've showed
    all the names,
  • 2:41 - 2:44
    but they're all on top of each other.
  • 2:44 - 2:45
    So, we need to change our y position.
  • 2:46 - 2:49
    Let's just say 'friendNum*30'.
  • 2:50 - 2:53
    That's good but Sophia's off the screen
    and Sophia's not going
  • 2:53 - 2:55
    to be very happy if she finds that out.
  • 2:55 - 3:00
    So, let's just add 30 to that.
    So now they're all offset by 30.
  • 3:00 - 3:04
    Beautiful! So now you see we have
    a loop displaying our array.
  • 3:04 - 3:09
    And that means if we add more people
    like OhNoes Guy, or maybe even Sal,
  • 3:09 - 3:12
    if I just add him to the array
    then Sal will be my friend.
  • 3:12 - 3:14
    Awesome! Now he's my buddy.
  • 3:14 - 3:18
    And you see that it just automatically
    shows the new friends
  • 3:18 - 3:21
    because it's always going
    through the whole array.
  • 3:21 - 3:24
    So we can delete our old code.
    We don't need that anymore.
  • 3:24 - 3:28
    And let's just go through
    this code here and review what it does.
  • 3:28 - 3:31
    So we start off
    with friendNum equal to zero.
  • 3:31 - 3:34
    We check to see if friendNum is
    less than the current length.
  • 3:34 - 3:38
    So you imagine zero is
    less than six. That's true.
  • 3:38 - 3:42
    So then we go inside here
    and we say text, my friends friendNum.
  • 3:42 - 3:44
    So that'll be my friends zero,
    the first time.
  • 3:45 - 3:48
    And then 30 plus zero times 30.
  • 3:48 - 3:53
    So it displays Sophia at 10 and 30.
    That's what it does.
  • 3:54 - 3:56
    And then friendNum++.
    So then it becomes 1.
  • 3:56 - 3:58
    And then it goes back around
    and says, "Ok, is 1 less
  • 3:58 - 4:00
    than myfriends.length?
    Yeah, it is."
  • 4:00 - 4:03
    And it keeps going,
    keeps going, keeps going.
  • 4:03 - 4:06
    And then finally we get to Sal.
    Remember, Sal is actually
  • 4:06 - 4:11
    the sixth element in the array,
    but he's index 5, since we start at zero.
  • 4:11 - 4:14
    So, is five less than six? Yes.
  • 4:14 - 4:16
    So it displays myfriends five.
  • 4:16 - 4:20
    And then it becomes six
    and we say, "Is six less than six?"
  • 4:20 - 4:22
    No. It's equal.
  • 4:22 - 4:25
    So this'll be false.
    So it will never display the sixth element
  • 4:25 - 4:29
    which is good
    because there is nothing in index six.
  • 4:29 - 4:33
    There's a sixth element,
    but nothing in index six.
  • 4:33 - 4:36
    it can be really confusing,
    the fact that it's zero and one
  • 4:36 - 4:39
    and doing all that
    but you'll get the hang of it.
  • 4:39 - 4:41
    All right, so that's our loop.
  • 4:42 - 4:45
    Now, if you want to, you can
    actually use a for loop
  • 4:45 - 4:47
    as well if you prefer for loops.
  • 4:47 - 4:52
    For for loops, we'll just say, for,
    and then, var friendNum = 0;
  • 4:53 - 4:54
    and then we have our condition
  • 4:54 - 4:58
    friendNum < myFriends.length
  • 4:58 - 5:01
    and then our increment: friendNum++
  • 5:01 - 5:07
    and then inside the for loop, we can
    just put just this line of code here.
  • 5:07 - 5:09
    and I'll just change the x
    so that you can see
  • 5:10 - 5:14
    it does exactly the same thing.
    So it's up to you which one you use,
  • 5:14 - 5:17
    but the point is to use a loop
    with your arrays
  • 5:17 - 5:20
    because it will make you so powerful.
Title:
Looping through Arrays (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:
05:22

English subtitles

Revisions