Welcome back.
In this video I want to show you some very important keywords.
And that is break and continue.
So let's look at break first.
It's like this one here we have seen break before when we were working with switch statements.
But break also has a different use case and that is in loops.
So let's create a for loop here.
And this for loop will just count to ten and nothing else.
So very simple stuff.
And what we're going to do is we're going to display whatever the value of I is okay.
Now what we can do is we can use break in order to break out of the whole loop.
So let me show you an example.
If I is equal equal three, for example, then I'm going to say let's say CW I've had enough.
And then what I'm going to say is let's break out of it, okay.
And let me also add console dot read key here so that our solution keeps running.
Let's run this.
You will see 0123 I've had enough.
And it gets out of the loop so it doesn't execute 456789 okay so this is this break statement.
If I get rid of it you can see 0123 I've had enough 5456, seven, eight nine, 10 or 10 is not included,
but all the way up to nine.
So this break, what it does is it gets out of the current loop.
So it gets out of this loop here of this for loop here.
In our case it's very simple.
Why would we even do that?
Why would we say if I is equal to three that we break out.
We could also just make the for loop smaller.
Well yeah.
But that's a very simple example.
Let's say this depends on the user input.
And the user enters something that we want to make sure that that we break out of the loop off.
Once that happens or once our data that we don't have any control over that, that comes from a cloud
database or from a database in general, or from user input that once that meets a certain condition,
that we break out of the loop and we don't keep going with it.
Okay.
So that's one example as to where you can use break inside of for loops to get out of the current loop.
That also works for the other loop types.
So for do while loops, for while loops, as well as for each loops and for each loops that we're going
to cover later on.
Okay, so that's what this break statement does.
Let's have a look at what the continue statement does.
So therefore I'm going to add another statement in here.
And this one will be only if not continued.
That will make a lot more sense once I change this to continue here.
So you see there's this keyword continue.
And what that does is it also breaks out of the current loop, but not entirely, but only for the iteration
of the current loop.
Okay.
So what is an iteration?
An iteration is the current run through of the for loop or of the loop in general.
So let's have a look.
Let's run this.
It will make more sense once we see this.
So here only if not continued only if not continued, only if not continued.
And then at three it says I've had enough.
So at three this one was executed.
But this here was not executed.
So let me actually get rid of this here as well.
And we will see that there will be something missing.
So let me run this again.
You see zero has only if not continued.
One has it, two has it, three doesn't have it.
So after three this line here will not be executed because this continue just says, oh, you're done
for the current iteration, for the current run through of the for loop.
Now go to the next run through of the for loop.
So let me make this loop a little smaller.
Let's say we have four.
And let's say we check if two is equal or if it's exactly two.
And we're going to display the counter variable in each run.
Except for obviously when I is equal to because then we will jump out of the current iteration.
So let's just run the code and see how it does.
So I'm going to use a break point to show you okay.
So we start we get into our iteration.
We write the line, we display something onto our console.
We display zero okay.
Then we check is zero equal two.
No it's not.
We go to this line here and we display I again.
So now we have zero and zero okay.
Now we increment I by one.
So now it's going to be one.
We check is one less than four.
Yes it is.
So we run this line we say I which means one we check is one equal two.
No it isn't.
So we display it again.
Now we go and increment I by one.
Again we get into the loop because two is less than four.
And then we display the two.
So in the next point it will be executed.
We see the two.
Now we check is two equal two.
Yes it is.
So then we get into this if statement.
We get to this continue statement.
And in the next moment we're going to increment by one.
So tuck we jump directly to this increment.
We don't run this line of code anymore.
It is not executed.
We directly jump to the increment.
And then once we click the next step we can see that now it compares is three less than four.
That is true.
So what it will do is it will get into the loop.
Once again it will display three.
It will check is three equal two.
No it's not.
It will display three once again.
And there we are.
So we see that everything is displayed twice except for this two which is only displayed once because
it's displayed here at the start.
But then we continue out of the loop at this point.
So of this current iteration.
And that is pretty much what the continue statement does.
So the break statement finishes the entire for loop execution and jumps to the next line, which means
to this line down here to the red key line.
And the continue statement just goes to the next run through the next iteration of the loop that you're
currently in.
All right.
So that's it for this video.
See you in the next one.