In this lecture I want to go over some things that can modify how the control flow of the loop will
execute.
The first one is called the break statement.
So we saw the break statement before with our switch statements when the execution hits the break statement.
It breaks out of the switch statement entirely.
Well the same thing happens in a loop.
So let's say we had this examples let's say Konst it Max awls to 10 and do a for loop and we'll say
it calls to 1 in is less than max or less than or equal to max plus plus.
And here we're going to break halfway through the entire run of the set or if it is equal to Max over
to
say see Ouch
rekeying at
current endl and we're going to have the break statement here and then we're saying See Ouch just like
we had in the last one the current iteration is in.
And then we're going to have here or just say something simple outside
the 4
Endal.
So what this is going to do is kind of loop through 1 to Max and if the iteration variable is equal
to Max over to a max of or two in this case will be five.
We're going to break out of the loop and we're not going to do this statement any more in fact or I
can do the loop or we're just going to break out of the loop and output that we're outside the for loop
now.
So you should see iterations 1 to 4.
And then at the fifth one we're going to say breaking out or breaking at 5 and then we shouldn't see
any more of this output.
And then we're just going to see this output.
So outside the forum.
So let's see if that is what happens.
So here is current iteration is one.
And then when you say 2 3 4 and then we say breaking out at 5 and then outside the forum.
So this break statement really just broke out of the for loop.
That's that's kind of what it does.
So this is this can be useful if say were trying to find something and then we find it and then we just
kind of want to stop looping entirely so or be using break a bit later to good use.
I think so let's see the next one I want to go over which is the continue statement.
So I really use the continue statement but it it is kind of interesting.
It skips the remaining statements in the loop body once it's executed until the next loop iteration.
So let's see an example let's modify this a little bit say ran into Max equals to hundred and right
go from 1 to 100 and say hi is her modular to lose to zero so every even number
we're going to say continue
to new
immigrants are going to print out say we're going to print that that's fine and we don't need this anymore.
So what it's going to do is we're going to loop through and every time we see an even number we're going
to skip the rest of the statements in the loop.
So.
So we should see only the odd statements in the loop or the output.
So let's run this.
So here it is 1 3 5 7 9 11 13 etc. to 99.
So this is one way too.
So the statement really just skips the rest of the loop and just goes back up to the top increments
the the iteration variable and then checks again to see if it should still continue or not checks condition
and then does the next iteration.
So continues really a way to skip statements if you want.
Again I don't I rarely use this but sometimes it can be quite useful.
So
in the next lecture I want to go over some nested loops.