WEBVTT

00:00.460 --> 00:07.740
The saw the last section the pattern for control flow where it took different actions based on a condition.

00:07.800 --> 00:09.460
So look something like this.

00:09.480 --> 00:19.850
We had the word in the parentheses which contained the condition that condition of course evaluates

00:19.850 --> 00:21.980
to TRUE or FALSE.

00:22.210 --> 00:28.350
And then you had the statement block where you had all your statements

00:30.880 --> 00:32.040
in the last section.

00:32.040 --> 00:36.210
This was the if statement and the switch statement.

00:36.480 --> 00:43.650
But in this section we want to take a look at how we can do statements multiple times.

00:43.650 --> 00:48.150
And if you remember in the first section we call this a loop.

00:48.180 --> 00:58.710
So in this lecture I went to go over what's called the while loop and the while loop has this syntax.

00:58.980 --> 01:09.390
So the key word you just have the key word while while here and the while has a condition just like

01:09.390 --> 01:10.120
an IF statement.

01:10.140 --> 01:17.070
But this condition is really what I talked about in the first section which is the stopping condition

01:17.670 --> 01:22.320
or the condition which is long as this is true.

01:22.550 --> 01:26.710
It will do the statements essentially.

01:26.880 --> 01:34.530
So remember that the we have to have we have to have this stopping condition otherwise the loop will

01:34.620 --> 01:37.020
go infinitely.

01:37.050 --> 01:44.970
So the dition is really any boolean condition you want and here's how I kind of think about it.

01:44.970 --> 01:52.590
So while the condition is true let me just put true here.

01:52.860 --> 02:03.940
It will do one iteration that is one set of all of these statements so do these statements one time

02:04.810 --> 02:12.080
and then it'll go back up and check the condition again to see if it's still true.

02:12.820 --> 02:17.920
And if it is it will again do one more iteration of all these statements.

02:17.920 --> 02:21.610
But if it's false it'll sort of exit this while loop.

02:21.610 --> 02:29.890
It won't do any of the statements in this statement block and it'll just continue on doing the rest

02:29.890 --> 02:32.510
of the program.

02:32.510 --> 02:37.420
So let's take a look at an example.

02:37.560 --> 02:49.990
Let's say we had a variable I would call or set it to be 1 in one of the constants and we'll call this

02:50.000 --> 02:52.410
maximal set to 10.

02:52.870 --> 03:04.500
And the condition is while it is less than or equal to 10 or Max don't use magic numbers.

03:05.410 --> 03:20.290
So it will just say seek out the current iteration is an analyst say I.

03:20.820 --> 03:24.090
So really just printing out 1 to 10.

03:24.650 --> 03:33.080
And then here we need the we need to increment I because otherwise if he didn't i will always have the

03:33.080 --> 03:41.670
value 1 and since one is less than or equal to Max this will always be true.

03:41.750 --> 03:53.180
So we loop infinitely if we didn't have this I so this really controls when the loop stops.

03:53.210 --> 03:59.640
Plus plus in its run it and see what happens.

03:59.640 --> 04:00.800
So here we go.

04:01.050 --> 04:08.250
So says the current iteration is 1 and then 2 and so on up to 10.

04:08.250 --> 04:22.870
So one thing to note here is when just after Princess this last line here and the I variable is 10 so

04:22.870 --> 04:25.790
when it's 10 it'll print this out.

04:25.810 --> 04:30.380
And then it will increment the variable which will be 11.

04:30.590 --> 04:39.230
Now it'll go back up to the while condition and it will actually check the condition one last time.

04:39.230 --> 04:42.750
So 11 is not less than or equal to 10.

04:42.770 --> 04:51.280
Therefore this condition becomes false and then it will break out into the rest of the program.

04:52.050 --> 04:59.420
And so it always does it one more time to check to see if it's false or not.

05:02.460 --> 05:04.050
So here

05:06.600 --> 05:13.740
the the while loop is really just another control flow structure but instead of making one decision

05:14.310 --> 05:21.030
it really does make the same decision multiple times until it's false.

05:21.030 --> 05:23.930
That's kind of the gist of the While loop.

05:23.950 --> 05:26.720
So let's see another example.

05:26.780 --> 05:29.490
So let's say we had some

05:33.530 --> 05:38.860
in it to be zero for now and we have Max to be 100.

05:38.950 --> 05:46.450
And what we're going to do is we're going to sum up all the even numbers from 1 to Max.

05:47.050 --> 05:48.580
So do this

05:52.100 --> 05:59.600
say if it is modulus called zero.

05:59.610 --> 06:13.820
So this means if it's even if it is even then right some are going to say plus equals.

06:13.980 --> 06:26.720
So this going to sum all the even eyes and then we're going to increment K and we're on to see out some

06:28.700 --> 06:38.050
of all even numbers from 1 to and really see Max here.

06:42.160 --> 06:42.880
Is

06:46.190 --> 06:46.590
some

06:51.080 --> 06:53.380
in is going to run this.

06:53.390 --> 07:04.700
So the sum of all even numbers from 1 to 100 is 2500 and 50 and notice we're using a constant.

07:04.710 --> 07:14.570
So you change this in you one to 1000 in there you go see.

07:14.580 --> 07:16.320
See the power of Constance.

07:16.330 --> 07:20.260
We to change one thing and then it changed the rest.

07:20.660 --> 07:29.310
That's why we introduced Constance her useful so the while loop really is the mother of all loops.

07:29.310 --> 07:33.060
And in fact we don't really need any other loops at all.

07:33.060 --> 07:43.560
However there are some that are make things a bit more convenient and Runcie to other loops at have

07:43.560 --> 07:48.160
some special purpose or make things a little bit different than the wild.

07:48.580 --> 07:50.520
And we're going to see that in the next lecture.
