‫Welcome back.
‫In this video, we're going to have a look at break and continue.
‫And I told you that in this video would come a challenge.
‫But before we can go to the challenge, we need to have those two concepts.
‫We will need break and continue to do the following challenge.
‫So let's go ahead and create a little program which will do just that.
‫So let's create a for loop four and counter equals zero counter below ten counter plus plus.
‫So nothing too fancy yet.
‫And then let's simply write that on to the console.
‫So counter should be written on to the console.
‫So now what I want to have in this for loop is an if statement.
‫So if counter is equal to three, what I want to do is I want to type something onto the console or
‫write something onto the console right line at three we stop.
‫So that's the simple statement that it should do.
‫And now in order to stop, because usually this for loop would go from 0 to 9.
‫And now what I want to do is I want to stop it at three.
‫So what that will do is it will break here.
‫We have seen break before when we use switch statements or switch case.
‫And in this case we want to use break in a for loop what break will do.
‫It will run or get out of the current loop or the current context.
‫And in this case it's the for loop.
‫So it will simply finish the for loop at that point.
‫So whenever break is executed, we get out of the loop and then the code below.
‫In this case, line 24 would be executed and that line should simply be our read statement so that we
‫see what's happening on the console.
‫All right.
‫So let's run that and see what's happening.
‫And that we are we see zero one, two, three, at three we stop.
‫So that's exactly what happens.
‫So what the actual flow is, we check is zero below ten.
‫That's fine.
‫All right, let's write zero onto the console, then let's check if counter which is zero is equal to
‫three.
‫Well, that's not the case.
‫So this if statement is not true, let's go to the next iteration.
‫So now counter is at one.
‫We check again.
‫Okay, everything fine?
‫We just execute this line.
‫Next we check a two same thing and now counter is three.
‫So we have the iteration where counter is three.
‫So we write three onto the console and then we check is counter three and yes, it's true.
‫So this code will be executed.
‫So we write at three, we stop and next we break.
‫So now we get out of this loop and we don't execute the four, five, six, seven, eight, nine anymore.
‫So that's what you can do with break.
‫And I told you that we also will check out continue.
‫So let's have a quick look at what continue will do.
‫And I'm going to stop the execution here and let's go into continue.
‫What continue will do is it will simply skip the current entry.
‫So let me cut that under our if statement because what that will do now is it will only write afterwards.
‫So after this, if statement will make more sense once we run the code, we see here zero one, two,
‫three row there is no three.
‫It just says at three we stop.
‫Or what I could say is we skip the three, so let's enter that.
‫We skip number three
‫and that we are 012 and we skip number three.
‫So now what you could also check here is what if it's modulo two?
‫So we use modulo two is equal zero double equal, by the way.
‫And now let's run it again.
‫And we we get we skip number three, one, three and so forth.
‫So what we could actually write here is next one is an odd number.
‫So let's do that.
‫Now comes an odd number.
‫And let's run it again.
‫And we see now comes an odd number one.
‫Now comes an odd number three and so forth.
‫So each time that we don't have an odd number, which is the case here, we continue.
‫So we jump out of this current iteration of the for loop and jump to the next iteration of the for loop.
‫All right.
‫So it's simply stopping at this point for that iteration and it doesn't execute the code that follows.
‫So in our case, only that code follows.
‫So this console, that right line counter follows after the Continue statement.
‫Great.
‫So that's just a little information introduction into break and continue.
‫We're going to use that in further lectures.
‫So that's why I wanted to show it to you now.
‫Great.
‫So in the next video, you'll finally get the challenge that I promised and see you there.