WEBVTT

00:00.180 --> 00:06.210
So far, you have seen that all the instructions you write in and out a program will be executed one

00:06.210 --> 00:13.140
by one, the key line to be executed and then laid three and I'm falling five, etc. But as you progress,

00:13.140 --> 00:19.650
you will want to make your program more dynamic and you will want to be able to execute only some instructions,

00:19.930 --> 00:22.990
depending on some conditions that you test.

00:23.390 --> 00:30.690
For example, you might do a certain action if a push button is priced and if it's not pressed, you

00:30.690 --> 00:32.010
would do another action.

00:32.220 --> 00:35.970
So let's write our first condition to understand how it works.

00:36.220 --> 00:40.410
So let's create a viable in the voice set, let's say temperature.

00:40.410 --> 00:45.280
We will use integer, OK, to make it simpler and let's see 30 degrees.

00:45.660 --> 00:52.350
So as you can see, this variable is declared inside the setup function, which means that the label

00:52.350 --> 00:57.990
is local to the set of function and we won't be able to use it like that in the loop function because

00:57.990 --> 00:59.170
this is a different scope.

00:59.490 --> 00:59.880
Don't know.

00:59.880 --> 01:04.640
Let's say we want to check if the temperature is equal to some value.

01:04.920 --> 01:12.360
Well, to create a condition, you can just use if the if you want or it should green then space and

01:12.360 --> 01:17.510
then you will open parenthesis and introduces, you will make a test.

01:17.980 --> 01:19.800
I'm just going to write one.

01:19.980 --> 01:24.830
Temperature is equal to, let's say, 20 degrees.

01:25.350 --> 01:25.730
All right.

01:25.740 --> 01:30.730
And you can see to test the equality we need to put to equal sign.

01:30.780 --> 01:34.710
OK, one equal sign is to assign a value.

01:34.710 --> 01:42.180
So to assign the value from the right to the step on the left to equal sine will simply test if the

01:42.180 --> 01:44.670
value on the left is equal to the value on the right.

01:44.940 --> 01:51.810
And if it's this, we return a boolean, which value is true, and if not, it will return boolean,

01:51.810 --> 01:53.520
which value is false.

01:54.010 --> 02:00.960
So now you have written your IFPRI the condition in parentheses you can go back to a new line and open

02:00.960 --> 02:09.450
close brackets so you can either open the brackets here or here on the same line as if this is just

02:09.450 --> 02:16.230
a matter of preference and readability, though not if the temperature is exactly equal to 20.

02:16.410 --> 02:18.890
And we are going to do some action here.

02:19.020 --> 02:23.720
OK, if not, then the block of code here will not be executed.

02:24.570 --> 02:26.250
Let's say say OK, print Ellen.

02:27.360 --> 02:31.110
Let's put simply, OK, let's run that program.

02:31.320 --> 02:35.420
So first we need of course, to to been.

02:41.760 --> 02:42.510
Conditions.

02:45.980 --> 02:52.520
And as you will see, well, nothing gets executed here if we use 30.

02:55.420 --> 03:04.270
We will see, OK, because the conditions here will be evaluated to true, OK, so this is great because

03:04.270 --> 03:10.410
it means you can decide to execute this block of code only if the condition here is true.

03:10.750 --> 03:12.820
So you have different operators.

03:12.820 --> 03:19.800
You have the equal equal which will test the equality, then you might want to test the inequality.

03:20.170 --> 03:24.080
In this case, you will use exclamation mark and equal.

03:24.700 --> 03:32.040
So if the temperature is different than 30 degrees of any value but here, which is different than 30,

03:32.350 --> 03:33.660
this condition would be true.

03:33.670 --> 03:35.740
And we enter this book of code.

03:36.010 --> 03:42.750
Now, you can also test with the lower than this mean strictly lower then.

03:43.510 --> 03:49.660
So if the temperature here is equal to 29 or lower, then this will work.

03:49.870 --> 03:55.210
If it's equal to 30, it will not work because this means strictly lower.

03:55.210 --> 03:58.270
Then then you have the lower are equal.

03:58.300 --> 04:01.990
So this is the bracket plus equals sign.

04:02.320 --> 04:11.440
And if the temperature is 30, that will also what you can test also with greater than the temperature

04:11.440 --> 04:13.430
is strictly greater than 30.

04:13.480 --> 04:20.180
So starting at 31, you can test with the greater are equal than 30.

04:20.560 --> 04:23.680
Now this will test just one condition.

04:23.830 --> 04:29.190
You might want to test different conditions and combine them together.

04:29.500 --> 04:36.490
For example, if you want to check if the temperature is between 20 and 29, what you could do is check

04:36.490 --> 04:42.610
first if the temperature is greater or equal than 20.

04:43.830 --> 04:54.210
OK, and then you can use what we call end Kastel to ampersands, this will combine two conditions,

04:54.690 --> 05:01.960
but you need to put this into parenthesis here and then put another condition to Currentzis and let's

05:01.960 --> 05:03.270
say temperature.

05:04.310 --> 05:06.260
Is lower than.

05:07.660 --> 05:12.140
Urges a strictly lower and well what this will do.

05:12.160 --> 05:19.180
So you entered the chief and then you see that you have and between two conditions that this condition

05:19.180 --> 05:20.190
will be evaluated.

05:20.620 --> 05:26.650
But if the temperature is greater or equal than 20 and then this one will be evaluated and still the

05:26.650 --> 05:33.460
temperature is strictly lower than 30 and then and will basically return true for the whole condition.

05:33.820 --> 05:41.340
If both conditions are true, if I put 25, this will work because this is true.

05:42.210 --> 05:43.600
OK, this is true.

05:43.870 --> 05:46.810
And thus the whole stuff is true.

05:46.810 --> 05:48.390
And then we enter this book of code.

05:48.910 --> 05:51.490
So we are going to see that also later.

05:51.940 --> 05:59.640
So you have the end operator, which will be true only if everything is true and you have the operator,

05:59.950 --> 06:01.660
but you can use two pipes.

06:02.440 --> 06:09.880
And with this operator, the whole condition will be true if at least one of the conditions is true.

06:10.630 --> 06:15.040
So if this condition is true, let's say but 35 now.

06:15.520 --> 06:17.420
So the temperature is greater than 20.

06:17.450 --> 06:21.160
OK, this is true that this condition will be false.

06:22.360 --> 06:28.320
But then with the operator, because this condition is true, then this one can be true or false.

06:28.330 --> 06:29.110
It doesn't matter.

06:29.350 --> 06:33.050
The whole condition will be true and is a book of good will be executed.

06:33.430 --> 06:33.840
All right.

06:33.850 --> 06:35.230
And that's not all.

06:35.230 --> 06:36.730
We have the conditions.

06:37.390 --> 06:43.240
We have a simple if in any condition here, you are going to test something and execute this block of

06:43.240 --> 06:50.000
code, but maybe you can also decide to execute something if the condition is not true.

06:50.020 --> 06:58.480
OK, so if this is false, then you can use the false statements just after the if block here.

06:58.960 --> 07:04.570
You create a new block of code and this will be executed in the condition here is false.

07:04.990 --> 07:07.620
Let's just put a simple condition.

07:07.840 --> 07:08.770
Temperature.

07:10.150 --> 07:15.220
Is equal to Secretary Kerry Prince, OK, and if.

07:18.280 --> 07:22.810
It's not funny we print not poking, it's upload in code.

07:23.710 --> 07:24.810
Let's see what happens.

07:27.220 --> 07:29.750
Not OK, but what happened here?

07:29.830 --> 07:30.800
We entered.

07:30.820 --> 07:34.180
If we check the condition, the condition is false.

07:34.180 --> 07:36.380
So we don't enter this book of code.

07:36.700 --> 07:38.290
Instead, we go to the ELLSON.

07:38.290 --> 07:39.600
We enter this block of code.

07:39.850 --> 07:43.980
And what you can also do is add intermediate blocks else.

07:44.080 --> 07:46.570
If so, what you can do.

07:47.740 --> 07:50.170
Is to test different conditions.

07:53.770 --> 08:00.160
So let's say we want to first test if the temperature is 40, then we print these and then the hold

08:00.700 --> 08:03.970
if as if a structure is over.

08:04.000 --> 08:06.900
So after this, we just go to line 50.

08:07.240 --> 08:12.540
But if this condition is not true, then we are going to test the next one.

08:12.760 --> 08:15.550
If this condition is true, we enter this block of code.

08:19.090 --> 08:21.670
And then it's as if.

08:23.770 --> 08:25.060
And let's see what happens.

08:28.120 --> 08:33.210
OK, we see it because this condition is false, which is the signal on this condition is true.

08:33.490 --> 08:37.300
And then we prayed that if I use another value.

08:40.700 --> 08:45.920
You can see we test for the condition here and then the condition here and then because none of the

08:45.920 --> 08:50.750
condition is true, we don't under any block of code and we enter the else block of code.

08:50.930 --> 08:51.350
All right.

08:51.350 --> 08:58.160
And one more thing to know is also, again, to pay attention to this code here, the number two available

08:58.160 --> 09:00.800
with created in the scope of this set to function.

09:00.830 --> 09:05.120
OK, you can see here we have curly brackets, which means this is a new school.

09:05.270 --> 09:06.890
This is a nested scope.

09:07.190 --> 09:12.950
So we can use the term approach available inside the scope if you want, because it is nested compared

09:12.950 --> 09:15.920
to the scope where the temperature was created.

09:16.790 --> 09:24.530
So to recap and conditions, you will use a structure when you want to do a different action depending

09:24.530 --> 09:27.620
on some conditions that you want to test.
