WEBVTT

00:00.110 --> 00:02.690
Almost done with the programming basics.

00:02.720 --> 00:06.200
Now let's look at loops.

00:06.200 --> 00:08.750
And again let's first understand why.

00:08.840 --> 00:12.590
Here is a basic example that I'm going to run.

00:13.560 --> 00:14.880
So loops.

00:15.510 --> 00:18.360
And as you can see, this is quite stupid.

00:18.360 --> 00:24.150
But we want to print hello ten times and you can see hello ten times.

00:24.150 --> 00:28.440
And here it's stupid because we just need to write println.

00:28.440 --> 00:29.910
Hello ten times.

00:29.910 --> 00:33.270
What happens if I need to print hello a thousand times?

00:33.270 --> 00:39.930
Well, whenever you need to repeat something more than two times, this would be best to use a loop.

00:39.930 --> 00:42.990
So here I'm going to show you two kinds of loop.

00:42.990 --> 00:45.330
The for loop and the while loop.

00:45.330 --> 00:48.240
Let's first see how to use the for loop.

00:48.240 --> 00:51.870
So I'm going to just remove all of that.

00:52.200 --> 00:53.910
And let's use a for loop.

00:54.180 --> 00:56.820
So you can start with four.

00:56.850 --> 00:58.950
You can see the keyword for in green.

00:58.950 --> 01:01.080
Then you open the parenthesis okay.

01:01.080 --> 01:04.110
And then I'm going to write it and explain it just after.

01:04.110 --> 01:07.650
So int I is equal to zero.

01:08.760 --> 01:12.030
And then semicolon I lower than ten.

01:12.030 --> 01:20.220
Semicolon I plus plus close the parentheses I open curly brackets, I press enter.

01:20.220 --> 01:26.910
This automatically closes the curly brackets block and adds a two space indentation.

01:26.910 --> 01:30.900
And then I can just put this right here.

01:32.770 --> 01:33.100
Right.

01:33.100 --> 01:35.410
And this let's let's run this.

01:35.410 --> 01:40.180
This will do the exact same thing, but with just three lines of code.

01:40.540 --> 01:43.120
Okay, you can see hello ten times.

01:43.420 --> 01:47.500
So here a little bit more because we already had I'm going to clear the output.

01:50.500 --> 01:51.430
Okay ten times.

01:51.430 --> 01:55.210
We have already had data from the previous run on the serial monitor.

01:55.510 --> 01:55.930
So.

01:55.930 --> 01:57.400
Well, how to create a follow?

01:57.430 --> 01:58.900
This is quite simple.

01:58.900 --> 02:05.200
You have three different things you need to give in the parenthesis, separated by semicolons.

02:05.230 --> 02:07.330
First is where you start.

02:07.330 --> 02:09.040
So here I create.

02:09.040 --> 02:13.300
You can see directly inside the int I create a variable.

02:13.300 --> 02:15.400
So I declare it and initialize it.

02:15.400 --> 02:20.980
So this will be the index we are going to use for the loop.

02:20.980 --> 02:24.040
And I initialize it here to zero okay.

02:24.040 --> 02:25.930
So that is the starting point.

02:26.050 --> 02:33.040
And then the second stuff in the for loop is the condition for the loop to continue.

02:33.040 --> 02:37.240
So here the condition is I is strictly lower than ten.

02:37.240 --> 02:44.230
So basically as long as I is strictly lower than ten we are going to execute this block of code.

02:44.230 --> 02:51.880
And the third parameter here is what we do with the variable with the index every time.

02:51.880 --> 02:55.540
And here I do I plus plus I plus plus.

02:55.540 --> 03:02.170
So I plus plus is the same as if you do I is equal to I plus one.

03:02.680 --> 03:05.920
This will simply add one to the variable.

03:06.490 --> 03:08.560
And this is specific to c and C plus plus.

03:08.560 --> 03:10.030
And it's quite convenient.

03:10.030 --> 03:13.030
So what we do we start at zero.

03:13.030 --> 03:15.970
We check if zero is strictly lower than ten.

03:15.970 --> 03:20.140
Yes we enter the four block and then we come back to this.

03:20.290 --> 03:23.590
So what we do is we do I plus plus.

03:23.590 --> 03:25.840
So now I is equal to one.

03:25.840 --> 03:27.820
Is one strictly lower than ten.

03:27.820 --> 03:28.300
Yes.

03:28.300 --> 03:33.910
So we enter this block etc. etc. until we reach value ten.

03:33.910 --> 03:38.680
So when I is equal to ten this condition will not be true okay.

03:38.680 --> 03:42.130
Because ten is not strictly lower than ten.

03:42.130 --> 03:47.230
And then we don't enter the block and we resume the program here on line seven.

03:47.230 --> 03:53.110
And as you can see here, a particularity is that we start to count at zero.

03:53.110 --> 03:53.440
Okay.

03:53.440 --> 03:55.360
In programming, take this habit.

03:55.360 --> 04:01.990
You don't start to count at one like you would do in real life, but you start to count at zero.

04:01.990 --> 04:06.460
So to count from 1 to 10, you will actually count from 0 to 9.

04:06.460 --> 04:06.940
Great.

04:06.940 --> 04:11.980
And we are going to come back to this for loop quite a few times in this course because this is very,

04:11.980 --> 04:12.910
very useful.

04:12.910 --> 04:16.630
And now another loop you can use is while.

04:16.930 --> 04:23.680
So I'm going to do the exact same thing we did here, which is to print that ten times but using a different

04:23.680 --> 04:25.030
way with the while loop.

04:25.510 --> 04:30.310
So first I'm going to create an int I.

04:30.940 --> 04:32.980
So I'm creating an index okay.

04:33.310 --> 04:36.400
And then the while basically while condition.

04:36.400 --> 04:43.330
So while I is lower strictly than ten I'm going to open curly brackets.

04:43.630 --> 04:46.450
And I'm going to do this.

04:46.840 --> 04:49.510
So while I is strictly lower than ten I print.

04:49.510 --> 04:50.260
Hello.

04:50.260 --> 04:55.390
So here let's say hello four and hello while so we can make the difference.

04:55.750 --> 04:59.650
And what I need to do is I plus plus okay.

04:59.650 --> 05:02.710
And don't forget the semicolon after each statement.

05:04.120 --> 05:06.700
Let's run that code here.

05:06.700 --> 05:10.390
So I'm going to clear the output and upload.

05:13.520 --> 05:17.750
Okay, you can see hello for ten times and hello while ten times.

05:18.080 --> 05:23.270
So you can see the while is a little bit different because we just test one condition and then we go

05:23.270 --> 05:24.170
inside the block.

05:24.170 --> 05:31.760
So if you want to use a counter like we used in the for, you would need to create a counter here before

05:31.760 --> 05:32.360
the while.

05:32.360 --> 05:37.850
And then you of course need to increase the counter inside the while because if you don't increase it

05:37.850 --> 05:40.820
well, I will always be lower than ten.

05:40.820 --> 05:41.060
Okay.

05:41.060 --> 05:42.110
If you don't increase I.

05:42.140 --> 05:44.900
So this condition will just run indefinitely.

05:44.900 --> 05:49.160
And this is what we call an infinite loop.

05:49.430 --> 05:54.560
So an infinite loop is when you are stuck in one loop and you can't get outside of the loop.

05:54.560 --> 06:01.430
So here, because we always increase by one, at some point I will be equal to ten and this condition

06:01.430 --> 06:02.210
will be false.

06:02.210 --> 06:05.060
So we continue.

06:05.060 --> 06:08.270
So we resume the program at line 30 okay.

06:08.270 --> 06:10.160
And here in the while we use a counter.

06:10.160 --> 06:12.230
But you can actually use something different.

06:12.230 --> 06:16.730
For example while while the push button is pressed do this.

06:16.730 --> 06:21.380
And then once the push button is not pressed anymore we just leave the while.

06:21.410 --> 06:21.920
Great.

06:21.920 --> 06:25.490
So now when to use a for loop versus a while loop.

06:25.490 --> 06:31.310
Basically, if you know how many times you need to go through the loop, use a for loop.

06:31.370 --> 06:38.930
For example, if you need 100 samples from a sensor, then go with a for loop and use a while loop when

06:38.930 --> 06:42.080
you don't necessarily know when you have to stop.

06:42.560 --> 06:49.700
For example, if you need to read data from the same sensor but stop whenever the value reaches a certain

06:49.700 --> 06:50.660
threshold.

06:50.660 --> 06:57.890
So for the example of this lesson, we can clearly see that a for loop was the most appropriate, since

06:57.890 --> 07:01.610
we knew exactly how many times we had to repeat the action.
