WEBVTT

00:01.900 --> 00:08.080
In previous video, we have talked about and described how up works and now it's time to consider other

00:08.080 --> 00:14.920
types of loops which are wild and do while while Loop does actually almost the same work as for a loop.

00:15.250 --> 00:19.210
The main difference between them is in Syntex, as I remember.

00:19.210 --> 00:26.590
In case of rollup, we had three statements in parentheses, initialization condition and increment

00:26.590 --> 00:27.360
or decrement.

00:28.150 --> 00:37.750
As for the while the counter is created outside it, then we need to write words while inside parentheses

00:38.170 --> 00:39.760
we have only the condition.

00:41.890 --> 00:48.480
And inside the Brice's, we have a block of code where we can put the increment or the of the counter,

00:48.910 --> 00:51.220
so it's right, cancel that log.

00:53.160 --> 01:03.870
I and then I plus plus reload the page and we have hear numbers from zero to 10 because in while we

01:03.870 --> 01:07.260
had condition I is less than or equal to 10.

01:07.590 --> 01:11.520
OK, as I see it plus plus is written after the council.

01:11.530 --> 01:17.340
But if we place it before council then we will get the numbers from one to 11.

01:18.370 --> 01:20.070
It seems a little bit strange.

01:20.110 --> 01:25.810
So let me explain what has happened here, as you know, in case of a look at the value of counter was

01:25.810 --> 01:28.270
updated after the execution of the code block.

01:29.020 --> 01:32.410
In this case, we see that eye is set as zero.

01:33.200 --> 01:37.450
It means that we have zero is less than or equal to that in condition.

01:38.380 --> 01:41.790
Then JavaScript engine continues reading codes line by line.

01:42.460 --> 01:46.930
It says E-Plus Plus and updates its value from zero to one.

01:48.220 --> 01:52.150
And that's the reason why we got one as the first number instead of zero.

01:53.320 --> 02:00.090
And also, that's why the result ends with 11, when it becomes 10, then the condition is true.

02:00.730 --> 02:07.570
But because of that we have a plus plus before council log that the value of AI becomes 11 for the last

02:07.570 --> 02:08.140
iteration.

02:09.410 --> 02:10.060
Makes sense.

02:11.580 --> 02:17.880
All right, this will make us unexpected results when we use while loop with a race in practice.

02:18.640 --> 02:29.250
Let's see what I mean, create a right of colors and insert here some colors, light red than green

02:29.250 --> 02:30.990
and blue.

02:33.500 --> 02:41.750
And use wire look to output the items of the array, set the counter as zero that creates while.

02:43.040 --> 02:47.510
Weather condition is less than colors that length.

02:50.330 --> 02:51.800
And inside the coal prices.

02:54.120 --> 03:01.620
Run in council colors, I and then I plus plus.

03:04.870 --> 03:08.220
Without and you, we have our colors in order.

03:09.350 --> 03:12.620
But if we place a plus plus before council, that log.

03:13.930 --> 03:14.260
That.

03:15.250 --> 03:20.980
You said that the first item escaped and after the last item we have identified.

03:22.340 --> 03:29.390
The reason is, say, for the last iteration, I became four and we know that item with index number

03:29.390 --> 03:33.380
four doesn't exist in the array and that's why we got undefined.

03:34.690 --> 03:42.880
All right, the next that I want to show you is do I loop the do loop is like the wire loop, but it

03:42.880 --> 03:47.290
executes the block of code once before, checking if the condition is true or false.

03:48.070 --> 03:53.200
If it's true, then to wire loop will continue looping as long as the condition is true.

03:54.070 --> 03:55.810
At first we write the key do.

03:56.710 --> 03:59.740
Then inside the Culebra we need to write block of code.

04:00.890 --> 04:02.600
Right, again, cost of log.

04:04.470 --> 04:05.760
Caldas, I.

04:08.020 --> 04:10.180
And then the increment I plus plus.

04:11.470 --> 04:15.730
After that, we have to right to keep keyboard while with its condition.

04:19.740 --> 04:27.900
Let's reload the page and again, we have colors from the air, right, as we said, in case of four

04:27.900 --> 04:34.070
and while loops the execution of block of code would not start and execute if the conditions are false.

04:34.740 --> 04:38.990
But in case of develop, it doesn't matter whether condition is true or false.

04:39.450 --> 04:42.150
The first iteration will be always executed.

04:43.140 --> 04:51.530
So if we make your condition false and right eye is greater than color length, then we will get all

04:51.540 --> 04:54.600
of the first item from the array, which is white.

04:56.300 --> 05:02.330
So remember that in case of the wallop, at first the code is executed and that the condition is checked,

05:03.030 --> 05:08.210
if it's true, the loop is continuing to work accordingly before the condition becomes false.

05:08.870 --> 05:11.540
This is the main difference between life and death.

05:11.560 --> 05:12.130
Wallach's.

05:13.350 --> 05:19.470
All right, so in this lecture, we have learned a while and two while loops are and how they work,

05:20.070 --> 05:20.810
let's move on.
