Hello will come again in this list and we are going to talk about jumping your statements.
That is essentially breaking out of a for two or essentially any kind of loop.
So imagine if you have a form that runs between 0 to 100 and then the moment reaches 75 you don't want
to continue anymore or if it reaches a certain number you don't want something to happen in that particular
number.
So how can we break out of a 4:2.
So this isn't about that the school I head into ex-cult I'm going to start a new project called John
statesman's mind jumps statement.
Here is what I'm about to do.
I'm going to say I have a for loop that goes between our certain numbers.
So here is my fault for ins.
Co. I'm going to counter this time count or below 10 counter-force increments.
So for 10 times zero to 10 I'm actually going to do this.
I'm going to say and Aslaug counter is a percentage I counter.
So I'm going to print out there as you can imagine when I run this is going to printed 10 times.
Now here's something I'm going to do I'm going to say if the counter is six.
So if you reached Number Six Here's what they want to happen.
I want to.
So let's see what this does for us.
So I'm going to run the code and the code runs.
You can see the compiler down here.
So it says counter is 0 1 2 3 4 5.
But the moment it is 6 it breaks out of the for loop and essentially comes here with this line.
So let me put something here and Aslaug and asked this.
And of the form.
Right.
So if you rerun this now you will see the error counts between 0 to 5 and then says well this is the
end of the formula.
Now that's a lot of the thing let's say in my FOR loop if my counter is going to reach has a six I want
to continue.
Let's see what continuo does continue is essentially going to skip this iteration.
It says Number Six.
Skip this part.
Let's see what you get and as you can imagine we now have 0 1 2 3 4 5 but we don't have 6 because when
time 36 we saying continue to skip this quarter the next one.
So he's going to 71 81 on the 91 and then the whole 42 is essentially finished.
Now could this be useful for us.
Let me take you back to one of the lessons we had earlier.
Earlier on we had this array array of the cities that we copy all of that.
You don't need to do this.
This is just for demonstration purposes.
When I actually comment out both of these guys and I'm going to put this in here so we have an array
with a large amount of what you're texting.
There's so many different items in it.
I want to find out the very very first time are the term Shanghai shows up.
So I'm going to go ahead and say for innocence Thring cities in cities array if City is equal to a string.
And this thing I'm looking for was Shanghai.
So if you found Shanghai then we wanted to do something.
For instance we want to say and that's the long of the foundation.
All right simple as that.
So we go to all the cities in this area and if we find Shanghai right we found Shanghai and there's
really nothing wrong about that except for something let me print another thing at the bottom of the
for loop in here.
I'm going to say and Aslaug this city is and I'm going to say the object that you find in the city.
So let's run this.
So as you can see it says in our cities Vancouver Vancouver blah blah blah blah because we found Shanghai
again.
This city is Shanghai.
The city is all part of the city they pay wash over who work cheap scoring going through all of them.
What if the moment I found Shanghai I didn't want to continue anymore.
But if I didn't want to go to everybody else.
So in that case I'm going to say if the city was trying calling say we found Shanghai and then say break
I don't want to do this anymore.
So that's one of the it says it happens fewer times it says Vancouver Vancouver.
All of these different places.
And it says we found Shanghai and then it breaks entirely out of this flow.
So we don't have that anymore and that's quite useful because you know it cuts down on the compilation
time and the computation time and also make sure that your code has a better structure but that logic
if you're looking for something in the moment you found that you don't care whether anything else.
That's it.
We don't have to do any of the other calculations we have to just jump outside of it.
Now in saying that we could also use break or continue in this that for loops as well.
So let me show you how.
Let's say I have a group of four dopes that are nested inside each other.
So for instance H is zero.
That is for a horizontal H is below 5 and H increments itself and then within that four ends work the
call equals zero call below 5 V plus plus.
And then we want to do something in here.
So we want to have five essentially five columns of five rows.
So it's going to be a matrix of 5.5.
We'll be making a lot of these in the future.
So for now I want to do this.
I want to say if we reached the tree and the H are four if we reach this particular location of break
out entirely.
Right.
So if we reached this place we want to break out of here entirely.
So if V was 3 and H was for we we want to finish this for low income to here which says.
And that's like all all is completely finished.
So what should we do.
So here's what I'm I'm going to say in every nation.
Let's print something.
And as long as we are at and whether we have to we are at our age which is that things we that is another
thing.
So what are those ideas the age we we thought that is where we are.
So then we print this.
We built this application.
It says we are at age 0 0 0 1 and then we go all the way to before and then just keeps going all the
way to age 14 and before and then it says the loop is completely finished.
But then in here I'm going to say if what are we looking for if it is tree and the edge is for if I'm
not wrong the stage is for in that case what they want to do is if they want to break.
So let's see what
so what.
The gun is here is Vegard H for v3.
What we wanted and the loop has completely finished.
This may seem it is working.
It actually is not.
Let me show you why.
Let's say I wanted to find age to age 2 and Vietri.
So I'm going to change that too.
So then I'd add one more in a slug and a slide.
When I say internal loop is finished.
So as you can imagine every time that this flop is finished we run this.
Then we go back to the outer for a loop and increment that and then we redo this whole thing.
So let's see what we get.
It says in here where we find it essentially it says our age zero before etch to reiterate this is very
wanted things to be Vietri age too.
And then the internal loop finishes.
But then it goes to H-3 and zero.
So it really starts to outperform.
But that's not what we wanted.
We wanted to completely break out and come in here.
So what could we do.
Here is something that's a very common practice and going introduce a new valuable called Boullion are
let's say completed.
And they completed by the forth is false.
So if we reach this situation if we reach where everything is the way that we want them to be here's
what we're going to do.
We're gonna say in that case a sort of a break come play Ted becomes true.
So now that complete that is true we're going to break out of this for a lot.
So let me get myself another line here.
I'm going to say brag or rather direct with proper spelling.
So break.
And then at the beginning of each of the outer four loops I'm going to go ahead and say if completed
is completed is true then branch.
So the moment we begin this complete is not true so it is not going to break so it's going to go to
this line is going to calculate that.
But then the moment the complete it becomes true from there on every time we go to a new citation that
iteration is not going to happen.
So let's say this in practice then run this and the moment they say H2 and H3 internal loop and.
And also does the for loop is completely finished.
So as you can see it's a combination of some of the tools that are there such as a break or continue.
Some of the you know the logic that we could use ourselves we say well the moment I reach a certain
this and that's when I want things to happen and based on that we decide about you know what's going
to happen next.
So two quick recap in here are use things such as continue or break to either keep and I ration of loop
or to skip the entire four loop based on a certain condition.
So you continue to say skip this one go to the next one.
You use the break to completely break out of a of a loop or isolation.
So I think that's about that.
Let's keep it up to here and I see you in the next lesson.