Hey, in this lesson, we're going to be covering the JavaScript break statement and what the brake
statement does is it terminates a loop or a switch or a label, and it allows you to transfer control
from that statement and allows you to terminate the statement.
So let me give you an example of it.
So an example of having a break within a loop.
So let's set the value of X to be equal to zero and we're going to do a while and continue to loop while
X is less than 10.
And then we do need to increment X, so increment X plus one, and let's put the result here in the
console and we'll console whatever the value of access.
So put that.
So let's see what happens in the console.
So we get zero one, two, three, four, five, six, seven, eight, nine as we're starting with zero
and we're continuing the loop while it's less than 10.
So how about we add in a break so we'll add another condition and we'll check to see if X is equal to
five.
And we could do an absolute because making sure that the data type is the same as well.
And then let's add in a break here.
So it's going to break if X is equal to five and then let's output the value of X in the console after
the statement finishes running.
So after that condition finishes running.
So what do you think the value of X will be at this point?
And how much how many Xs are we going to see within the console?
So let's check it out and run it.
And if you said that at five, so once X is equal to five, it's going to stop the condition.
You're correct.
So what happened here is it ran through and it incremented the value of X, and then at this point when
X was equal to five, then it broke that.
And once it breaks it, it doesn't finish writing it out into the console.
So that's why we don't see the value of X being five in line twenty two.
But we do see it in line twenty six because that's the next statement that runs after the break.
So it runs through once X equals five, it breaks the loop and these two values don't.
These two statements don't get run or executed and we move on to the following line line twenty six
and I'll also do one for continue.
So let's check out and see if X is equal to three.
And if it is, then what we'll do is we're going to continue.
And what continue does is it's a keyword that allows us to.
Break one iteration of the statement, so we'll see what happens within the console and it's still going
to be breaking at five.
How about we change that to eight so we get more iterations of the console message?
So what do you think the output is going to be and what do you think is going to we're going to see
in the console?
Remember, we're breaking we've got to continue if X is equal to three.
So let's see what happens.
So what happened here is we ran zero one two.
So it's constantly still spinning.
And that's because we've entered into an infinite loop.
So the browser is not able to handle that.
So effectively, we shouldn't be using the continue within the while loop because it's causing an error
there.
It's causing it to.
Create an infinite loop on the continuum.
So let's save that and we'll refresh it or close it and open up a new page and we're going to try that
within the for loop and I'll open up the dev console.
So we've got the console there once again.
So we're not going to keep the continue loop in the while.
Let's try this with for loop so that I equals zero to the same format and then we'll do four.
I equals zero, and then we're going to continue our eye is less than 10 and then also increment I by
one, so do it within a for loop.
And actually, I'm going to take the value of AI and keep it within the for loop so that as we loop
through will console log whatever the value of AI is and we'll see what that looks like.
So refresh and this is the value of I outlined 19 that's being output.
So now let's check out what happens to if we have a continue and if I equals three then we continue.
So what do you think Continuos going to do here within the for loop?
Let's refresh.
And what happened here is we've got zero one two and we've skipped three.
So that continue within the for loop.
So it didn't create an infinite loop, but it allowed us to skip the one iteration.
And also let's try break.
So we'll check to see if I is equal to eight.
And if it is, then we'll run a break.
So what do you think is going to happen here and what do you think the output in the console is going
to be?
I'm going to come into these values for the console so that we only have the for loop sitting within
the console.
What do you think is going to be output in the console?
Remember, AI is starting at zero.
Let's refresh.
We've got zero one to notice three is missing because we did the continue four, five, six, seven,
and then when it hits eight, it breaks the for loop.
So that's how you can use for and continue within your JavaScript code.
So try to.