Welcome back, round lesson nine, if else statements, so I showed you a little sneak peek of these
in the last video, and you're actually going to be using these types of condition checks, lots and
lots in JavaScript, and it's what's called control flow.
So control flows when you're checking if one thing is true, then cool, execute one set of code.
And if it's not true, then execute a different set of code.
So you can use multiple checks, you can say if the is it just Cascade's like a waterfall, it's going
to check this one.
If that's false, it'll check that one.
If that's false, it'll check everything else.
Basically, it'll just go to the final set value so you can always set these up.
So there is no code that gets run if you just choose to do an if statement or just if and else if that,
it won't actually execute the else.
But if you have the else in there, it will always execute this if everything else is false.
OK, so the else is when it does not meet the condition else if is when you want to check for a second
or third or fourth or etc condition.
OK, so let's take a look and play with this.
So let's just set an initial value here.
Let's just do a pet name of Fido, OK?
And let's give this mood.
Let's give our Fido a mood of happy.
Great.
Fido was very happy.
That is a permanent mood because we use the concept.
So let's go ahead and check to see what the mood is.
Is the mood exactly this string right here, these five characters in succession and nothing else?
If so, then let's council log.
Let's council log.
That name
is a
mood.
His pet name is totally a mood dog.
You.
All right, let's try that out.
So if we just do the if by itself, then it's going to check it.
If it's true, it'll execute this line of code so you can just use it if it's just by themselves, nothing
else.
Whatever you want to just say, make a certain line of code conditional, totally cool.
And there are other ways to do that as well.
OK, but what if we gave this and else condition the U.S., if that if this value up here wasn't true,
then let's just say something else.
Let's just say
let's see.
Will hopefully be happy soon.
OK, so that's totally fine and obviously this is still true.
But if we change this too sad or poor Drogo.
Fido will hopefully be happy soon.
Well, not with this constant one.
Let's change that to a and maybe we can change it.
Now, let's give this another value, let's give it a value of what is hungry, oops, is hungry, equal,
true.
OK, so this dog is hungry.
Let's check if it's hungry.
So it's just sometimes easier just to copy and paste or actually not not hungry as a as a string.
Let's just all we have to check is he's hungry.
Remember Truthy and falsey values.
We don't have to do with truthiness obviously, because the value here is explicitly true.
So if that is true, then console log.
Let's say is hungry.
OK, else will hopefully eat soon.
OK.
All right, so if it's not angry and hopefully it's harmless, you see?
Fido's Hungaria.
If you make that false.
Hopefully to see so depending on the conditional expression, if this conditional expression is true,
the one inside these parentheses, then it will execute this line of code.
So is is Hungary true?
No.
Is Hungary is false.
Is the mood happy?
No, the mood is sad.
OK, so if this doesn't trigger, then it will move on to the condition if one exists.
Let's look at another example to let's to weight equals forty five.
And let's do you know what this do to animals Fido's wait
here and then let's do whiskers.
We.
It's the cat's whiskers, whiskers, and let's say, yeah, that's a good number, so we can use comparison
operators like we had in one of the previous lessons to see if one is bigger than the other.
So let's do Fido wait greater than.
Keep that in is greater than Whiskas, wait.
So if that's the case, then we can consider log.
Fido weighs more,
weighs more than whiskers.
And then we could do else if then we could check for similar condition, but this time we're going to
check for if they're exactly the same weight, OK?
So if they have the same weight.
Then to another value in say.
They wear the same.
Fido and Whiskers weigh the same amount,
we see this fine and then we'll have an LST condition.
So logically, if Fido doesn't weigh more than whiskers and they don't weigh the same, then what is
the only possible other condition?
The only other logical condition that that could happen, assuming obviously these both exist, which
in our case they do, is that Whiskers weighs more than Fido.
OK, so what if Fido lost a ton of weight and then Whiskers gained a ton of weight?
If that were to happen, then whiskers would weigh more than Fido.
If we change your values back then, Fido is more than whiskers.
For some reason, we've got a real chunky whiskers.
Then they would weigh the same.
OK, so there you go, using comparison operators, if else, if and else to do comparisons and decide
which line of code we want to.
Well, we'll find out.
It'll tell us, hey, this line of code is the one that's true.
This line of code is the only one that's true, et cetera.
OK, so that is, if else comparison operators and JavaScript control flow.
And we're going to learn more about control flow with other ways of comparing between multiple things
in another video.
Thank you.