Hey, everyone, in this video, we're going to talk about break and continue.
These statements are going to be used mainly with loops, and their purpose is to either interrupt the
loop or just jump one iteration.
So let's start with the break statement, which can be used to interrupt the loop or a switch statement.
I haven't talked about the switch statement yet.
If you want to know more, you can just click on this link that I left here.
Maybe later in the course.
I'm going to explain this with more details.
But for now, let's just see the example using a loop.
So going to Visual Studio, I'm just going to start a while loop here.
So I'm going to create a variable.
So far, X equals zero.
So while X is less than 10, we are going to do something and what am I going to do here is just sending
X to the console and then I'm going to increment the value of X. We don't have to go back to our Web
page to know what's going to happen.
We are just going to send Axe to the council until X is nine.
All right.
So what if we wanted to interrupt this loop earlier?
If we don't want to change the condition here, we can just use a break statement.
So I'm just going to start a conditional here.
And I'm going to say that if X equals five.
Then I just want to stop this loop, so I'm just going to use the brake statement.
So no matter what is the condition for the loop, when our cold hits this brake statement, it is just
going to go out of the loop.
So going back to our page, refreshing it.
We can see that now.
We stopped at 4:00.
This is because when we sent four to the console right after this, we incremented the value of X,
which became five.
So then we entered here and broke out of the loop.
I know this sounds like a silly example, because after all, why didn't we just change it here instead
of using the brake statement?
But this was just an example of how you can interrupt a loop.
There will be situations in which you are going to want to do this.
So let's go now to the next example.
The continue is a bit different.
We are going to jump out of the loop, just like with the brake statement.
But the difference is that we are not going to interrupt the loop.
We are just going to jump the current iteration.
So for this example, I'm going to write some code that is going to send to the console only the even
numbers from zero to twenty.
So I'm going to start a loop.
And instead of sending all the numbers to the console, I'm just going to send the even numbers.
So how can we do this before we start writing this, I just want to talk to you about the modulus operator,
which is this one.
This operator serves to return the remainder of a division.
So if I divide four by two, I will have no remainder.
The result is going to be two and the remainder is going to be zero.
So if I try to do four, modulus two, we can see that the remainder is zero.
But if I try to divide four by three.
I'm going to have the remainder of one, if I tried to divide ten by three, I will have a remainder
of one.
So this is how the modulus operator works.
So this is useful to identify even and odd numbers, because any even number that we do, Modulus two,
is going to return in zero.
So we can try 10, 12, 14, 16.
This is always going to return zero because there's no remainder when we divide an even number by two.
But any odd number.
So let's try 11, it's going to have the result of one, because every odd number that we divide by
two is always going to have a remainder of one.
So this is how this operator works.
So now that we know this, let's try to write a loop that's going to print only the even numbers.
So going to Visual Studio Code.
So I'm going to start with a variable called NUM, this is going to start at zero and then I'm just
going to start a while loop.
So while numb is less than or equal to 20, because I want to print all the even numbers all the way
up to 20, so this is going to be the condition to enter the loop.
And this time I'm going to do a bit differently.
I'm going to explain this later.
So I'm going to increment right at the beginning of the loop.
So I'm just going to do num plus plus and then I'm just going to do console.
That log.
And I'm going to send the number.
All right, so if we do this, we know what's going to happen, but let's try it out.
We can see that we just sent all the numbers to the console all the way up to twenty, actually twenty
one.
So yeah, since I'm incrementing the value at the beginning of the loop, then we can just use number
is less than 20.
So let's try again.
All right, so now it's stopping at 20, but we just want to print the even number, so how can we do
this?
There are two ways of doing this.
So first, let's start by using the continued statement.
So instead of just doing consolidated that log, let's make a test first.
So let's start a conditional here.
And we are going to test if num modulus to.
It's not.
Zero.
So we know that every even number that we do, modula Stu, is going to return in zero.
So if this doesn't happen, it means that this is an odd number.
So if this is an odd number, we don't want to keep running this iteration.
So we are just going to use the continue statement.
So when the interpreter hits this line, it's just going to jump out of the loop.
So it's not going to execute this line in any other line after this.
So after to continue, it's just going to jump out of the loop and start again.
When it hits the continue again, it jumps out of the loop and starts again.
So this is how the continuous statement works.
So if we do this.
And refresh the page, we can see that now we only printed the even numbers.
Of course, this is not the only way of doing this, because after all, we could have done it like
this, just getting the console that long and putting it inside here and then doing the opposite.
So if num modulus two equals zero, then we do console that log.
If not, we just do nothing.
So if we refresh the page, we can see that now we have the same result.
So like I always say, there's never just one way of solving a problem.
There's never just one solution.
But I thought it would be interesting to show you how to solve this using the continuous statement.
So you know how this works.
All right.
So I just wanted to quickly show you these two examples.
Maybe we're going to see these statements later in the course.
But for now, that was out.
So I'll wait for you in the next video.