Welcome back.
And this video I would like to introduce to break and continue keywords.
Those two keywords are super useful when it comes to for loops or loops in general because they allow
us to get out of the current iteration or get out of the loop entirely.
If a certain condition is met.
So here I have this full loop.
Let's just use a print statement here where I'm just going to print I so this will print all the way
up to 19.
So from one to 19, let's look at it and we could see one to 19.
So that's cool and all.
So now let's look at what we can do with a break keyboard.
So what I'm going to say is let's divide it by two and then check if that is going to be five.
So basically, it's 10 that will be the case and that case of breakout.
So just break, which means that finished his follow up or don't iterate through the follow up anymore.
Basically continuing the code down here.
So print done with the loop.
OK, let's run it again.
You could see it's running all the way to 10 and then it breaks out.
So let's run it again and then you will see that it says done with a loop.
OK, so that's cool.
So basically, what happens here is once brake is executed, it gets out of this entire loop.
So this follow up is done and it continues to code down here.
So after the loop.
Now, this is very useful if you get this data from an external source or if this is dependent on the
user input, for example, or something like that in our case, it's rather useful, you could say,
because well, why not just say until 11:00 here that would do the same thing and you wouldn't have
to work with breaks.
But this is not the point here.
The point is really that you can use the break word in order to jump out of a for loop or a when expression
when a certain condition is met.
And that would usually be a condition that you have no control over.
That is either random or it has to do with data that you get or something else.
OK, so that's the break word.
Now let's look at the continu keyword.
And therefore, I will need to push this print statement further down.
And now let's use continue.
Like, so OK, let's run it again and see what's going to happen.
So you see one two nine is there.
There's No 10, there is No.
11, and then it goes from 12 to 19 and it's done with a loop.
So what happened here?
The thing is, 10 divided by two is going to be five, right?
So 10 divided by two is five.
So this if statement is true.
So it goes into this continue.
What continue then does is it says, OK, stop with the current iteration and go to the next iteration.
So skip the code that is inside of this for loop and then go to the next step.
So it goes from one to two to three, and this has never met, right, this condition.
But then at the point when it's 10, this condition is met and it just says, OK, skip the current
iteration, go to the 11th step.
OK, then it's checking 11 divided by two is equal equal five.
And it says true because 11 divided by two is five point five.
But because we're working with integers here, it doesn't save the point five, so it just saves five
in there.
So it is five equals five.
So it's met as well.
When I is equal.
11 So then it just says Continue.
So skip the current iteration.
Don't execute this code anymore and go back to the next iteration.
OK, so that's basically what's happening here.
It's super interesting, but yeah, that's really here.
So 10 divided by two is five.
And then.
Eleven divided by two is 5.5, which is five in terms of an int because integers cannot stored is zero
point five thingI, right?
So just cuts off whatever comes after the DOT.
It cuts it off and that's why the 11 is not appearing here either.
So it just continued through it, basically skipping this part where the value is actually being printed
onto the console.
All right.
And that's pretty much it for the brick and continue keywords.