This lesson is going to be all about loops and iteration using JavaScript in order to do a for loop,
do while loop while loop and break and continue are that are available within the loops.
So what JavaScript loops do is they allow you to run multiple iterations and you can set the parameters.
One thing to avoid with loops is infinite loop.
So you don't want them to be able to loop forever that will cross the browser.
So always make sure that you have a way out of your loop.
And that's the purpose of a loop, is to constantly increment values or numbers and then find a way
out of the loop.
So once a condition is met, so for instance, this is a for loop.
We've got a counter while the counter is less than five, it will continue.
And then once that condition is no longer true, the loop will break.
And this is true for all the loops.
So as you're iterating through the content of a loop, you also have to option to break the loop.
So you have ability to set a condition to break out of the loop so the break will stop the loop.
There's also one that's called continue true.
And then we continue.
Although this one is not commonly used, it is available, but it's not advice because you might run
into some problems with testing.
So there is a challenge for this lesson and that's to create a loop of a counter using all three different
iteration options.
So you've got the for loop, we've got the while loop and as well the do while loop.
There's an example of them.
So these are all going to output the same thing.
So each loop has a starting point.
So setting the counter to zero where looping while the counter is less than five.
And we also need a way to increase the counter.
So the counter is incrementing by one.
So this gives us our way out of the loop.
So we've got our starting point as well within the while loop or setting X to zero while X is less than
five.
And then we're incrementing X within the while as well.
We've got the do while loop, so we're checking to we're setting the value of it to zero where incrementing
I with one and then we're doing this while I is less than five.
So first let's create the most common loop.
So this is the for loop and this is the one that you're going to encounter the most.
So setting up our counter at zero.
This is our starting point for the counter.
And then next is the condition that we want to meet.
So we want to loop this while counter is less than five and then we need a way out.
So we need something different.
We need a counter updating.
So that's where we just got that counter updating, incrementing by one.
And this will give us our way out.
And I'll just write for Loop and then also add in the value of counter just after that.
So take a look in the console and we see it starts at zero.
So this is our starting value.
We can start it up one as well.
So that will start at one, two, three, four, and then we can loop while counter is less than six.
So that means that it's going to break after five.
So that was one example.
The other example is this is while loop.
So let's set our starting points.
So using X as our starting point, we're going to loop while X is less than five.
And then this is where the blockquote goes.
Always make sure that your incrementing X because this is again are way out of the loop and this is
our while loop.
And then we can see the value of X.
And depending on what we're trying to accomplish with your coding, you may choose you may prefer to
use one looping method over the other.
And also keep in mind that wherever we're producing the X increment, that's going to affect the value
of X at the time we're running the code.
So for placing it before or after, it will make a difference within the output.
And then lastly, let's do one last loop and this is the do while loop.
So this is very similar to the while loop and there is actually one important difference.
So let's set it up and we'll run through it and then we'll see what the difference there is.
So within the console log.
So this is the do while and adding in the value of ice so that we can see it.
And then the condition that's going to break this loop is while AI is less than five.
So let's try that.
And you can see that the result is the same thing where if we do the increment of AI afterwards, it's
going to work the same way as the while loop.
So the difference here is that if the starting values for the while loop are ten and for the do while
loop R ten, you're going to see one interesting difference.
And that means that the do while loop always runs at least one time, whereas the while loop, because
this condition isn't met, it never runs this block of code because the value of X is ten.
So if you need to run it at least one time.
You can use a do while loop, otherwise you could just use a while loop, or if you prefer, you can
use a for loop, so do try out all three.