WEBVTT

00:00.560 --> 00:04.900
In this lecture I want to go over my favorite loop before loop.

00:04.950 --> 00:13.710
So we saw in the while loop lecture this example where we count from one to some max number and you

00:13.710 --> 00:15.870
set the max to 10.

00:16.440 --> 00:23.040
Well there's a different loop called the for loop that is very good at counting.

00:23.040 --> 00:29.950
So let's try to do for loop example just a just like the one above.

00:29.970 --> 00:38.060
So you say it equals to 1 eyes less than or equal to Max

00:40.910 --> 00:46.110
and plus plus C.

00:47.750 --> 00:51.650
Current generation is

00:55.990 --> 00:58.240
in.

00:58.420 --> 01:03.640
And if we run this see 1 to 10.

01:03.780 --> 01:06.660
So we print 1 to 10 and that's the while loop.

01:06.660 --> 01:12.540
So this first 9:59 is just the while loop and the next 9:59 is our FOR loop.

01:12.550 --> 01:16.700
So it does the exact same thing as the wild.

01:17.220 --> 01:21.950
So let's unpack this syntax a little bit and trying to understand what's going on.

01:22.410 --> 01:26.240
So we have the four and then we have the parentheses just like normal.

01:26.640 --> 01:35.520
And next we have this thing called in die equals to 1 inside the for loop.

01:35.550 --> 01:40.930
So this is actually where we define our iteration very.

01:41.030 --> 01:47.450
So the iteration variables defined in the four look and this this is defined only once.

01:47.440 --> 01:56.910
It only get created one one time at the very start of the for loop and we can just set it to whatever

01:56.910 --> 01:57.350
we want.

01:57.350 --> 02:01.300
In this case it's 1 and we have a semi-colon.

02:01.300 --> 02:06.300
This is really to delimit the different sections of the form.

02:06.370 --> 02:12.560
So the first section is the creation of our iteration variable just like we did up here.

02:12.910 --> 02:22.420
But actually inside the loop itself and next is just the condition just like we had with the same condition

02:23.290 --> 02:24.190
exact same.

02:24.190 --> 02:29.450
So it's always creation of our iteration variable.

02:29.630 --> 02:35.890
Then we check the the condition to see if it's true or not.

02:37.400 --> 02:40.190
And then skip this for now.

02:41.250 --> 02:43.010
And we're going to go to here.

02:43.020 --> 02:50.860
So we just do all our statements just like normal just like we had in our while loop.

02:51.050 --> 03:01.100
And then at the end at the very end of all the statements in the for loop then we will go and do this

03:01.340 --> 03:05.280
plus plus just like we have here.

03:05.290 --> 03:08.140
So these are really just equivalent.

03:08.410 --> 03:18.070
So we do all the statements and then at the end we do plus plus and then we'll check again for the next

03:18.100 --> 03:22.750
iteration to see if the condition is true or false.

03:22.750 --> 03:32.290
So really this for loop and this while loop are equivalent and you can see that in the output.

03:32.870 --> 03:40.070
So this just another way a nice convenient way remember that programmers really like convenience.

03:40.070 --> 03:48.680
So this is a very convenient way of counting and I know the syntax is a bit strange at first but I think

03:48.680 --> 03:57.810
you'll start learning to love having this for loop instead of doing doing it this way every time.

03:57.830 --> 04:08.860
So one thing to note though is we could use this variable outside of of the Y.

04:09.180 --> 04:19.150
So in fact we declared or defined it really outside of the Waialua but this variable is actually a different

04:19.170 --> 04:24.720
arbitrary variable and the variable only for the for loop.

04:24.790 --> 04:30.310
So we can't use this variable outside of the for loop at all.

04:30.550 --> 04:34.200
So that's just one thing to keep in mind.

04:34.220 --> 04:40.520
So in the next lecture I want to go over some things that can modify the flow of our loops.
