WEBVTT

00:00.870 --> 00:05.490
So far we are OK with with loops.

00:05.490 --> 00:09.710
We know the basics at least and we're OK with our race.

00:09.840 --> 00:13.680
How about we use loops to iterate through an array.

00:14.010 --> 00:15.150
What the Start mean.

00:15.180 --> 00:21.740
It means using a loop to go through each element of an array and doing certain thing with that element.

00:21.750 --> 00:26.070
So for example imagine I'm just going to use a very simple example.

00:26.160 --> 00:32.430
Imagine that we want to go through this entire array and I want to add one to each one of these numbers

00:32.970 --> 00:37.690
so I'll show you how you can do that using wire loops and for loops.

00:38.100 --> 00:47.270
And when you do it with wire loops First let's define an index and we'll it will run on iteration.

00:47.400 --> 00:48.550
For this array.

00:49.020 --> 00:59.060
So I want it to be less than the length of the array the length of the array in this case a 6.

00:59.060 --> 01:10.620
So I want one to be 0 1 2 3 4 5 so that I can use eye to access directly the the elements of the list.

01:10.770 --> 01:12.660
I'm going to need the length of the list.

01:12.700 --> 01:17.460
So let me grab that here and just right.

01:18.510 --> 01:30.610
I'll just call it Len and well this let's write this in in English on this is how we can get the length

01:30.610 --> 01:37.770
of a list is a length property that we can use and it will always give us the length of the list.

01:37.810 --> 01:45.100
Now why am I doing that in here and not putting this directly in here I ask you with my seeing some

01:45.100 --> 01:48.930
cases on the web if you're looking for it for examples and things like that.

01:49.060 --> 01:56.620
Well calculating the length of a list is it's a mathematical operation done behind the scenes and that's

01:56.620 --> 02:00.540
something that we don't see but it's done for us.

02:00.640 --> 02:06.490
So getting the length of a list is something that consumes resources of your computer and if you want

02:06.490 --> 02:11.830
to write faster code that's not really the goal of this course but if you can do these little things

02:11.980 --> 02:13.520
it will certainly help.

02:13.540 --> 02:21.270
So we calculate the length of the list first and then we were not calculating it on each iteration.

02:21.270 --> 02:27.760
If that if this is not clear to you please feel free to ask more information about it or just don't

02:27.760 --> 02:28.350
worry about it.

02:28.350 --> 02:31.480
You can also just write list length thing here.

02:31.510 --> 02:38.130
It's not that much of a big thing like that a little a little performance tweak.

02:38.580 --> 02:39.030
All right.

02:39.040 --> 02:41.300
So I want to go through this list.

02:41.680 --> 02:46.480
Let's not forget that in a while loop you need to manually increment your index once you're going to

02:46.480 --> 02:48.400
get stuck in an infinite loop.

02:48.410 --> 02:50.650
You don't want that to happen.

02:50.930 --> 02:55.040
It's going to make your browser crash most likely.

02:55.240 --> 03:05.970
So this is how you can access the elements of the list and we can do something like this.

03:08.190 --> 03:13.140
But we've seen that this is actually equivalent to doing what I just did below with I.

03:13.270 --> 03:18.600
So in this manner we're incrementing the value in one.

03:18.810 --> 03:25.230
So I will take the value of zero in the first iteration and that will give us the first number here

03:25.680 --> 03:30.600
then I will take the value of 1 and then we'll be accessing the next one.

03:30.600 --> 03:36.100
So this how you can access each one of them because I will be taking different values.

03:36.270 --> 03:46.740
And let's just make sure that this worked by using the console and see how we incremented each element

03:46.770 --> 03:49.220
in one.

03:49.260 --> 03:53.440
Now let's do the same thing for a for loop.

03:54.180 --> 04:01.640
When I did four loops previously I showed you the year that you could do something like this.

04:02.280 --> 04:06.220
But in this case since we already defined I before.

04:06.660 --> 04:11.590
I'm just going to type I equals zero because we already find it.

04:12.190 --> 04:16.040
And is going to go while am I.

04:16.200 --> 04:20.290
It's less than the length of the array which have already defined too.

04:20.510 --> 04:23.050
So I'm reusing these variables.

04:23.690 --> 04:25.750
And what's happening in each iteration.

04:25.750 --> 04:30.970
Well we're going to increase ID in one.

04:31.890 --> 04:38.010
So within the iteration we don't need to manually increment our index because that's done for us in

04:38.010 --> 04:38.820
here.

04:39.200 --> 04:44.310
All I'm going to do in this part is just increment my list again.

04:44.680 --> 04:52.280
And at the end of the loop we're going to show this list to see how numbers were incremented and in

04:52.280 --> 04:54.940
a 1 again.

04:54.990 --> 04:59.530
So that's like a simple example of how we can iterate through a loop.

04:59.670 --> 05:06.780
This is something done something done very often when when typing codes because you might have a list

05:06.780 --> 05:12.600
of elements that you want to process that you want to save or that you went out to retrieve and modify

05:12.600 --> 05:14.590
and somehow send back to the user.

05:14.820 --> 05:17.040
So this is a very common use case.

05:17.040 --> 05:22.440
It's important that you understand that you know how to iterate through an array or through a list.
