All right, let's discuss another very important thing in JavaScript and also in all programming languages,
it's time to talk about loops.
If you want to run the same code over and over again, each time with a different value, then you have
to use loops.
Looping means repeating a block of code when a condition is through.
So loops check condition.
OK, let's actually see what I'm talking about to demonstrate how powerful tool of the loop is, let's
create an array.
And place inside it some names, John Niek, then Bob Michael.
And Mary.
Now, suppose that we want to output and cancel each item of array at the same time.
So what can we do?
We have to access on each item and then lock them in council separately like this.
Cancel that log array with index number zero.
Let's duplicated four times and change the index numbers.
Revolt and you see that we have the names from Iraq, the result, what we wanted to get.
How do you think is that the best way?
Of course not, because we had to type a lot.
We use specified index number for each item in order to access on that.
Here we had just only five items and was a bit easy to do that.
But imagine that sometimes we may have dozens of them.
Then it will be crazy, right?
So we need something else and it's loop to avoid using such inconvenient way and so much typing.
We can simply use falbe at first.
Let's demonstrate how we had to write it syntactically.
We need to use keyword for.
Followed by parentheses in which we had to write some expressions and conditions, first of all, we
need to create variable which will be initialized or counter.
In most cases, the name of the variable is AI, which stands for initialization.
So if we want to output names from Array, starting with John, then we need to assign to the variable
idea value zero, because as you know, John is the first item of array and its index number is zero
after the end of the first date, but into place semicolon, which is followed by the condition.
So array consists of five items because of that.
The last item Mary has the index number for then we need as a condition to write I is less than five.
And the last statement we need to write I plus plus.
It's actually an arithmetical operator and it's called increment operator, and it's the same as I equals
to I plus one using increment operator, after each step, the value is increased by one.
All right, the last we have to do is to run these in Castle, White Castle, that log.
Array and in square brackets, right?
The variable I.
Reload and you see that we have again, the names from Array for Loop gave us the same results.
So I believe you guessed that it's more powerful, dynamic and flexible way.
OK, let's dig deeper.
What has happened here and how far Loop actually works.
In general, one follow up is on global level, it means that it's not inside the function.
Then JavaScript engine immediately executes the code.
We don't need to call it as it was in case of functions.
Each step in a loop.
I mean, the whole cycle is called as an iteration at first JavaScript engines is that variable, i.e.
equals to zero.
Then it checks the condition.
Because of that, zero is less than five.
The condition is true and it points in console.
First item from a right, John, before the first iteration is and it is updated to one using increment
plus plus operator.
So update of variable i.e. from zero to one happens exactly after the executing of the code block inside
Calabrese.
In this case, JavaScript engine prints the value, John, which has the index number zero after that
starts second iteration.
And as we said, AI is already updated to one.
One is less than five.
The condition is are going through and JavaScript engine prints, the item of array which has the index
number one, and it's Nick.
Then again, it goes back to AI plus plus updates.
I add in credibility to this third iteration and in the same way the third item, Bob, is printed.
So loop is continued while the condition becomes false, so when it becomes five, the condition is
false and the loop stops execution, you may wonder why JavaScript engine doesn't go back to first expression
where variable equal to zero after each iteration.
If it acts like this, then after each iteration I would be zero again and we would get infinitely the
first item of rate, John, and that would crash the browser.
So that's why the engine executes this expression allowance at the beginning of the loop, and also
that's the reason why it's called initialization.
All right, imagine that we don't know how many items the array has and also we want to output all of
them.
So what can we do?
The solution is to use the property of array, which I hope you remember from the previous videos,
and it's called length, which you see here we have five items and therefore length of the array is
five.
So we can use eight instead of five.
Revolt and you see that we have the same output.
It's better to use land property because it will work, whether you know or not, how many items the
area has.
All right.
Let's make our example a bit complex.
And also, I would like to show you some features of the statements that we can use with loops, gets
rid of this line and use if statement.
Suppose that Bob is my brother and I want to find it in the right names using loop and then output it
right.
If statements inside the loop and insert the following condition array I triple equals to Bob.
So this condition will be true when loop, which is the third iteration and when the condition is true,
let's print out the following.
Right.
Cancel that log error.
I plus is my brother.
Reload and we have Bob is my brother in order to output other items as well.
We need to right outside if statement cancel that log array I.
So here we have all existing items now, suppose that we want to stop the loop when the condition is
true inside, if statement four, that we can use the brake statement.
So when JavaScript engines is the brake statement, it stops looping and it doesn't matter whether the
condition is true or false, insightfully reload and you see the first two items of the array, then
the output.
Bob is my brother and after it, the for loop is stopped.
Suppose that we want to output all the items in array except Bob.
For that we can use another statement which is called Continue.
So that's right.
Continue reload.
And we have all the items except Bob.
All right.
Let's see what continue exactly.
Does the continuous statement breaks one iteration in the loop if a specified condition occurs and continues
with the next iteration in the loop.
So in this case, when the condition inside, if statement is true, then it executes this line of code,
then JavaScript engine is going to output, Bob.
But it says the continuous statement, therefore it breaks the current iteration and moves directly
to the next iteration.
And Prince Michael and then Merry.
So this line of code is executed because it's written before continuous statement, if we please continue
before that line, then it won't be executed.
And you see that Bob is my brother no longer appears.
All right, in this lecture, we have learned what up is and how it works.
Let's move on to the next video.