below i will provide to you the transcription of a video lesson called " Iterating through collections".. Study the content of it, find the meaning the speaker wants to convey and create a text, a lesson that will convey this meaning in an easy to understand way to college students new to the subjects discused in it. this is the transcript: ""1
00:00:00,000 --> 00:00:04,990
Arrays are one of the most common things you're find in all programming languages

2
00:00:05,000 --> 00:00:08,990
and to get the right perspective here don't think so much of the word array, but

3
00:00:09,000 --> 00:00:13,990
think of the wider idea of collections and think of how many different kinds of

4
00:00:14,000 --> 00:00:17,990
applications you have in your computer that are really collections of things.

5
00:00:18,000 --> 00:00:21,990
Your email program is a collection of emails, your address book is a collection

6
00:00:22,000 --> 00:00:27,990
of contacts, your MP3 player containing a collection of albums and playlists,

7
00:00:28,000 --> 00:00:31,990
each one being a collection of songs. They're everywhere in programming.

8
00:00:32,000 --> 00:00:36,990
And an array is the most fundamental straightforward kind of collection you can have.

9
00:00:37,000 --> 00:00:40,990
Now one of the most common things you'll ever need to do is to iterate through

10
00:00:41,000 --> 00:00:44,990
or loop through every element in an array and this is actually a very easy thing

11
00:00:45,000 --> 00:00:48,990
to do. You don't really need to know much about the array to do it.

12
00:00:49,000 --> 00:00:50,990
So let's take a look.

13
00:00:51,000 --> 00:00:54,990
So let's say I have a simple array here with five elements in.

14
00:00:55,000 --> 00:00:55,990
It doesn't matter if it's 5.

15
00:00:56,000 --> 00:00:57,990
It doesn't matter if its 500.

16
00:00:58,000 --> 00:00:59,990
I'm going to set up a basic loop to go through this.

17
00:01:00,000 --> 00:01:01,990
So I'm going to create a while loop.

18
00:01:02,000 --> 00:01:03,990
I could do a for loop as well, but this is fine.

19
00:01:04,000 --> 00:01:06,990
It's got the regular elements.

20
00:01:07,000 --> 00:01:10,990
We need to set up the index. Although it's very important when you're working

21
00:01:11,000 --> 00:01:15,990
with a basic array you start the index at 0, because our arrays are zero based.

22
00:01:16,000 --> 00:01:20,990
Than we do the usual check condition and here's kind of the magic sauce here.

23
00:01:21,000 --> 00:01:23,990
When you're doing this, you don't need to know much about the array as long as

24
00:01:24,000 --> 00:01:26,990
just asking it what is its length.

25
00:01:27,000 --> 00:01:28,990
It can tell you how long it is.

26
00:01:29,000 --> 00:01:30,990
We don't need to know this going into it. Sure,

27
00:01:31,000 --> 00:01:38,990
I could write while i < 5, but it's much easier to write while i < myArray.length.

28
00:01:39,000 --> 00:01:41,990
And of course, this is a loop, so at the end of the body of the loop we want to

29
00:01:42,000 --> 00:01:43,990
increment that index.

30
00:01:44,000 --> 00:01:48,990
But know that every time we iterate through or loop through this we can use the

31
00:01:49,000 --> 00:01:51,990
index, the value of i to access the current element.

32
00:01:52,000 --> 00:01:54,990
Let me show you a little bit about what I mean just in case that isn't clear.

33
00:01:55,000 --> 00:01:58,990
If we start to move through our code and we start to execute it, we create this

34
00:01:59,000 --> 00:02:01,990
variable i and it's equal to 0.

35
00:02:02,000 --> 00:02:06,990
We then move to the next line. We say is i less than myArray.length? We'll go

36
00:02:07,000 --> 00:02:09,990
out and look, what is myArray.length? It's going to be 5.

37
00:02:10,000 --> 00:02:12,990
There are five elements in the array.

38
00:02:13,000 --> 00:02:16,990
If you remember that .length property gives you how many elements there are.

39
00:02:17,000 --> 00:02:19,990
The highest one is of course 4. So this is true.

40
00:02:20,000 --> 00:02:22,990
We jump into the body, we start executing any of the code, and then we might

41
00:02:23,000 --> 00:02:28,990
say using alert message or something else, we want to access whatever is the first element.

42
00:02:29,000 --> 00:02:33,990
So we'll use myArray and whatever the value of i is. Well, i is 0.

43
00:02:34,000 --> 00:02:34,990
So we'll get to the first element.

44
00:02:35,000 --> 00:02:38,990
Then we'll process further, we'll get to the end of the loop, we will increment

45
00:02:39,000 --> 00:02:40,990
the index, i now becomes 1.

46
00:02:41,000 --> 00:02:45,990
We'll jump back and check the while statement again. Is 1 < 5?

47
00:02:46,000 --> 00:02:46,990
Yes, it is.

48
00:02:47,000 --> 00:02:49,990
That we'll be true and we'll start moving through and we will keep on the

49
00:02:50,000 --> 00:02:55,990
looping around, adding one to this index while we go through every element in the array.

50
00:02:56,000 --> 00:02:57,990
But at some point we will hit i++.

51
00:02:58,000 --> 00:02:59,990
It will change i to 5.

52
00:03:00,000 --> 00:03:03,990
Then we'll say is 5 < 5? No, it isn't.

53
00:03:04,000 --> 00:03:04,990
It's equal to 5.

54
00:03:05,000 --> 00:03:09,990
So this is false and I will just jump out and continue on our merry way.

55
00:03:10,000 --> 00:03:14,990
So the important things here, we're starting the index at 0.

56
00:03:15,000 --> 00:03:18,990
When we're checking the condition we're checking against the length of the array

57
00:03:19,000 --> 00:03:22,990
and we're doing a less than, not a less than or equal to, and then inside the

58
00:03:23,000 --> 00:03:25,990
loop we're using that index to access the current element.

59
00:03:26,000 --> 00:03:28,990
Now you might think, well, what so great about this?

60
00:03:29,000 --> 00:03:32,990
Well, let me show you a kind of a real example here.

61
00:03:33,000 --> 00:03:36,990
So in this simple example here I'm creating an array with five elements,

62
00:03:37,000 --> 00:03:37,990
each with the value 500.

63
00:03:38,000 --> 00:03:39,990
I want to just add them altogether.

64
00:03:40,000 --> 00:03:43,990
Now I'm doing a for loop. So here is a version of the for loop rather then the

65
00:03:44,000 --> 00:03:48,990
while loop, but I'm doing exactly the same thing that I showed in these slides.

66
00:03:49,000 --> 00:03:54,990
Start the index of at 0, ask if it's less than the length of the array, and

67
00:03:55,000 --> 00:03:59,990
add 1 to it, and then every time we go through this loop I've just got one line of code.

68
00:04:00,000 --> 00:04:03,990
I'm going to say the variable total should be set equal to whatever it currently

69
00:04:04,000 --> 00:04:07,990
is plus whatever's at the current position.

70
00:04:08,000 --> 00:04:10,990
If I'm going on the loop for the first time it's at position 0, second time

71
00:04:11,000 --> 00:04:13,990
position 1, third time position 2, and so on.

72
00:04:14,000 --> 00:04:17,990
So we should iterate around the for loop five times and then after we are done we'll

73
00:04:18,000 --> 00:04:19,990
come down and pop up this alert message.

74
00:04:20,000 --> 00:04:20,990
Well, let's check it.

75
00:04:21,000 --> 00:04:25,990
We're adding 500 five times, we're hoping that we don't get an off by one error,

76
00:04:26,000 --> 00:04:30,990
but I can hold this in my head correctly, and yes 5 times 500 is 2500.

77
00:04:31,000 --> 00:04:32,990
That looks correct.

78
00:04:33,000 --> 00:04:36,990
Here's the great thing about programming your array iterations like this.

79
00:04:37,000 --> 00:04:40,990
You don't have to know how big the array is.

80
00:04:41,000 --> 00:04:46,990
If I come up here and change this and add in a whole bunch of other numbers,

81
00:04:47,000 --> 00:04:51,990
I don't have to do anything with the rest of my code, because I wasn't programming

82
00:04:52,000 --> 00:04:53,990
it to only go through that array five times.

83
00:04:54,000 --> 00:04:57,990
I'm telling it go through the array however long it is.

84
00:04:58,000 --> 00:05:01,990
So I can save this and all I'd change was how many elements we're in the array.

85
00:05:02,000 --> 00:05:05,990
I run it again and it still works just fine.

86
00:05:06,000 --> 00:05:09,990
So keeping those couple of simple rules in place about knowing where you start

87
00:05:10,000 --> 00:05:12,990
and knowing what you're checking when you're iterating around an array can make

88
00:05:13,000 --> 00:05:23,000
it very easy to go through all the elements in a collection like this.