So far, you have seen that all the instructions you write in and out a program will be executed one
by one, the key line to be executed and then laid three and I'm falling five, etc. But as you progress,
you will want to make your program more dynamic and you will want to be able to execute only some instructions,
depending on some conditions that you test.
For example, you might do a certain action if a push button is priced and if it's not pressed, you
would do another action.
So let's write our first condition to understand how it works.
So let's create a viable in the voice set, let's say temperature.
We will use integer, OK, to make it simpler and let's see 30 degrees.
So as you can see, this variable is declared inside the setup function, which means that the label
is local to the set of function and we won't be able to use it like that in the loop function because
this is a different scope.
Don't know.
Let's say we want to check if the temperature is equal to some value.
Well, to create a condition, you can just use if the if you want or it should green then space and
then you will open parenthesis and introduces, you will make a test.
I'm just going to write one.
Temperature is equal to, let's say, 20 degrees.
All right.
And you can see to test the equality we need to put to equal sign.
OK, one equal sign is to assign a value.
So to assign the value from the right to the step on the left to equal sine will simply test if the
value on the left is equal to the value on the right.
And if it's this, we return a boolean, which value is true, and if not, it will return boolean,
which value is false.
So now you have written your IFPRI the condition in parentheses you can go back to a new line and open
close brackets so you can either open the brackets here or here on the same line as if this is just
a matter of preference and readability, though not if the temperature is exactly equal to 20.
And we are going to do some action here.
OK, if not, then the block of code here will not be executed.
Let's say say OK, print Ellen.
Let's put simply, OK, let's run that program.
So first we need of course, to to been.
Conditions.
And as you will see, well, nothing gets executed here if we use 30.
We will see, OK, because the conditions here will be evaluated to true, OK, so this is great because
it means you can decide to execute this block of code only if the condition here is true.
So you have different operators.
You have the equal equal which will test the equality, then you might want to test the inequality.
In this case, you will use exclamation mark and equal.
So if the temperature is different than 30 degrees of any value but here, which is different than 30,
this condition would be true.
And we enter this book of code.
Now, you can also test with the lower than this mean strictly lower then.
So if the temperature here is equal to 29 or lower, then this will work.
If it's equal to 30, it will not work because this means strictly lower.
Then then you have the lower are equal.
So this is the bracket plus equals sign.
And if the temperature is 30, that will also what you can test also with greater than the temperature
is strictly greater than 30.
So starting at 31, you can test with the greater are equal than 30.
Now this will test just one condition.
You might want to test different conditions and combine them together.
For example, if you want to check if the temperature is between 20 and 29, what you could do is check
first if the temperature is greater or equal than 20.
OK, and then you can use what we call end Kastel to ampersands, this will combine two conditions,
but you need to put this into parenthesis here and then put another condition to Currentzis and let's
say temperature.
Is lower than.
Urges a strictly lower and well what this will do.
So you entered the chief and then you see that you have and between two conditions that this condition
will be evaluated.
But if the temperature is greater or equal than 20 and then this one will be evaluated and still the
temperature is strictly lower than 30 and then and will basically return true for the whole condition.
If both conditions are true, if I put 25, this will work because this is true.
OK, this is true.
And thus the whole stuff is true.
And then we enter this book of code.
So we are going to see that also later.
So you have the end operator, which will be true only if everything is true and you have the operator,
but you can use two pipes.
And with this operator, the whole condition will be true if at least one of the conditions is true.
So if this condition is true, let's say but 35 now.
So the temperature is greater than 20.
OK, this is true that this condition will be false.
But then with the operator, because this condition is true, then this one can be true or false.
It doesn't matter.
The whole condition will be true and is a book of good will be executed.
All right.
And that's not all.
We have the conditions.
We have a simple if in any condition here, you are going to test something and execute this block of
code, but maybe you can also decide to execute something if the condition is not true.
OK, so if this is false, then you can use the false statements just after the if block here.
You create a new block of code and this will be executed in the condition here is false.
Let's just put a simple condition.
Temperature.
Is equal to Secretary Kerry Prince, OK, and if.
It's not funny we print not poking, it's upload in code.
Let's see what happens.
Not OK, but what happened here?
We entered.
If we check the condition, the condition is false.
So we don't enter this book of code.
Instead, we go to the ELLSON.
We enter this block of code.
And what you can also do is add intermediate blocks else.
If so, what you can do.
Is to test different conditions.
So let's say we want to first test if the temperature is 40, then we print these and then the hold
if as if a structure is over.
So after this, we just go to line 50.
But if this condition is not true, then we are going to test the next one.
If this condition is true, we enter this block of code.
And then it's as if.
And let's see what happens.
OK, we see it because this condition is false, which is the signal on this condition is true.
And then we prayed that if I use another value.
You can see we test for the condition here and then the condition here and then because none of the
condition is true, we don't under any block of code and we enter the else block of code.
All right.
And one more thing to know is also, again, to pay attention to this code here, the number two available
with created in the scope of this set to function.
OK, you can see here we have curly brackets, which means this is a new school.
This is a nested scope.
So we can use the term approach available inside the scope if you want, because it is nested compared
to the scope where the temperature was created.
So to recap and conditions, you will use a structure when you want to do a different action depending
on some conditions that you want to test.