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