Everyone in this video, we're going to talk about conditionals.
I already talked a little bit about it.
I showed you a simple example and now we're going to see some other use cases for conditionals.
So conditionals are used to determine if a block of code should or should not be executed depending
on a given set of conditions.
So let's see what this means.
Let's just copy all this and then we are going to make some changes to this code.
So here this is very simple.
We just have a variable called age.
Now we are just testing if this is less than 18.
If it is, we are going to show under age.
If it's not, we are going to show over age.
But now let's say we want to add more conditions.
So I'm just going to change the age to 70.
So now I just want to create another group, which will be the senior group.
So people above 65 years old will be part of the senior group.
People above 18 will be adults and people below 18 is going to be under age, so let's get started with
under 18 under age.
This is good.
And now we can include an else if.
So by doing this, this is just like starting another if, but this is part of the same structure.
So now the second condition is going to be.
If age.
Is greater than or equal to.
Sixty five in this case, we are going to show.
Senior.
So now we just covered these two cases, so any other thing that happens will mean that the person is
between 18 and 65 years old because we already covered below 18 and above 65.
So now any other age?
We can use it inside the house.
So now we are going to do console dialogue and just show adult.
So by doing this in this case, our age variable is 70, so if we refresh the page now it's showing
senior.
Let's try to change it to 14.
Now it's showing on the rage and let's change it to 18.
And now it's showing adult.
So now we've just added one more condition to our structure, so one important thing is that no matter
how many conditions we have, so we could have the first if statement and then 10 different else if
statement.
So no matter how many we have the moment when JavaScript finds a true value.
So let's say it finds it here, it's going to execute this code and then just ignore all the rest.
So when JavaScript finds a true value, it just stop testing all the rest of the conditions.
If there's no true value in any of the conditions, then the code inside the statement will be executed.
However, the statement, it's not mandatory.
So in some cases we just don't want and we just need to execute something if some condition is true.
If not, we just don't want to do anything.
So this is possible as well.
Now let's see how we can test multiple conditions at the same time using the end and or operators.
So when we use and which can be represented by these two and symbols when we use it, all the conditions
need to be true.
So the code is executed.
On the other hand, when using your.
We just need one condition to be true, to execute the code, so let's do an example so we can understand
better how it works.
So let's analyze the grade and absences of a student and determine if he's been improved in the course
to be improved.
The student needs the grade to be seven or higher and the number of absences, five or lower.
So let's just copy this.
And this one as well.
Let's go to Visual Studio Code, we can just comment all this shift at a.
All right, so now first, let's solve the problem using end so we know that in order to be approved,
the student needs the grade to be seven or higher and the number of absences, five are lower.
So we need to test two conditions at the same time.
How do we do this?
We just start an if statement.
Then the first condition is going to be that the grade is.
Greater than or equal to seven.
And now we are testing multiple conditions that should happen at the same time, so we are going to
use the end operator here and we also need the absences to be.
Less than or equal to five.
If those things happen at the same time, so like I said, when we use the end operator, we need all
the conditions to be true and we don't have to use only two conditions.
We could add more here if we wanted.
For this, we only need two conditions.
So the grade and the absences.
So if this is true and this is also true, then we are going to go inside here.
So then we can do cancel that log and we can write the student has been approved otherwise.
So inside the else statement, we can print.
This student has failed, so in this case, we have seven and three, so the student should be approved.
Let's test this out.
I'm just going to save this.
Refresh the page.
And now we can see that the student has been approved.
So now let me change the number of absences to six, and now the grade is the same, but now we have
absences above five.
So if we.
Test this now.
Now the student has failed so we can see that this is working.
Now let's do the same thing.
Using the OR operator.
So this time, what we need to do is a bit different because we only need one of the two conditions
to return.
True.
So in this case, instead of testing, if the student went well, we're just going to do the opposite.
So if the grade is below seven.
Or the absences are.
Above five, then we know that the student has failed, so if only one is true, if this one is true,
no matter if the absences are good, because this student will fail.
So in this case, we just have to change this.
So in the first statement, the student will fail and in the second one, the student will be approved.
Now, before saving this, I just forgot to change the operator, so now we are using or so if this
happens or this happens, then the student is going to fail, save this, refresh the page, and now
the student has failed.
Let's try something else.
Like let's try to grade five and the absences three.
The student should fail again because of the grade.
Refresh the page now we can see that this is working and now let's try.
Eight and three, and if we do this now, the student has been approved.
All right.
So these were the examples I wanted to show for now, before we finish this subject, I still need to
talk about two important things.
So the first one is evaluating values as conditions.
And I also want to talk about another way of writing conditionals, which is by using the ternary IF.
So let's do this in a separate video so this one doesn't get too long, so I'll wait for you in the
next video so we can continue with this.