WEBVTT

00:00.360 --> 00:05.760
In this lecture I want to go over some things that can modify how the control flow of the loop will

00:05.760 --> 00:07.180
execute.

00:07.260 --> 00:10.680
The first one is called the break statement.

00:10.680 --> 00:19.040
So we saw the break statement before with our switch statements when the execution hits the break statement.

00:19.040 --> 00:22.350
It breaks out of the switch statement entirely.

00:22.560 --> 00:24.720
Well the same thing happens in a loop.

00:24.750 --> 00:37.320
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

00:37.330 --> 00:51.370
it calls to 1 in is less than max or less than or equal to max plus plus.

00:52.030 --> 01:05.080
And here we're going to break halfway through the entire run of the set or if it is equal to Max over

01:05.080 --> 01:05.740
to

01:08.460 --> 01:10.230
say see Ouch

01:13.230 --> 01:14.880
rekeying at

01:18.510 --> 01:29.610
current endl and we're going to have the break statement here and then we're saying See Ouch just like

01:29.610 --> 01:39.320
we had in the last one the current iteration is in.

01:39.910 --> 01:46.550
And then we're going to have here or just say something simple outside

01:48.890 --> 01:49.680
the 4

01:53.420 --> 01:54.940
Endal.

01:54.960 --> 02:07.140
So what this is going to do is kind of loop through 1 to Max and if the iteration variable is equal

02:07.140 --> 02:12.210
to Max over to a max of or two in this case will be five.

02:12.360 --> 02:20.540
We're going to break out of the loop and we're not going to do this statement any more in fact or I

02:20.540 --> 02:29.150
can do the loop or we're just going to break out of the loop and output that we're outside the for loop

02:29.150 --> 02:29.980
now.

02:30.230 --> 02:36.520
So you should see iterations 1 to 4.

02:36.820 --> 02:43.660
And then at the fifth one we're going to say breaking out or breaking at 5 and then we shouldn't see

02:43.660 --> 02:47.170
any more of this output.

02:47.170 --> 02:50.570
And then we're just going to see this output.

02:50.860 --> 02:51.910
So outside the forum.

02:51.940 --> 02:54.000
So let's see if that is what happens.

02:55.110 --> 02:58.670
So here is current iteration is one.

02:58.680 --> 03:05.900
And then when you say 2 3 4 and then we say breaking out at 5 and then outside the forum.

03:05.910 --> 03:12.360
So this break statement really just broke out of the for loop.

03:12.540 --> 03:15.990
That's that's kind of what it does.

03:16.930 --> 03:26.320
So this is this can be useful if say were trying to find something and then we find it and then we just

03:26.320 --> 03:36.650
kind of want to stop looping entirely so or be using break a bit later to good use.

03:36.650 --> 03:45.860
I think so let's see the next one I want to go over which is the continue statement.

03:46.000 --> 03:51.680
So I really use the continue statement but it it is kind of interesting.

03:52.120 --> 03:59.790
It skips the remaining statements in the loop body once it's executed until the next loop iteration.

03:59.830 --> 04:08.530
So let's see an example let's modify this a little bit say ran into Max equals to hundred and right

04:08.530 --> 04:21.740
go from 1 to 100 and say hi is her modular to lose to zero so every even number

04:25.340 --> 04:29.240
we're going to say continue

04:34.870 --> 04:35.460
to new

04:38.110 --> 04:46.870
immigrants are going to print out say we're going to print that that's fine and we don't need this anymore.

04:49.340 --> 04:57.950
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

04:57.950 --> 05:03.670
to skip the rest of the statements in the loop.

05:03.710 --> 05:04.310
So.

05:04.460 --> 05:10.490
So we should see only the odd statements in the loop or the output.

05:10.490 --> 05:12.700
So let's run this.

05:12.730 --> 05:22.020
So here it is 1 3 5 7 9 11 13 etc. to 99.

05:22.300 --> 05:25.120
So this is one way too.

05:25.360 --> 05:35.110
So the statement really just skips the rest of the loop and just goes back up to the top increments

05:35.110 --> 05:42.130
the the iteration variable and then checks again to see if it should still continue or not checks condition

05:42.940 --> 05:47.820
and then does the next iteration.

05:48.140 --> 05:54.270
So continues really a way to skip statements if you want.

05:54.350 --> 05:58.930
Again I don't I rarely use this but sometimes it can be quite useful.

05:59.010 --> 05:59.580
So

06:06.110 --> 06:11.900
in the next lecture I want to go over some nested loops.
