Welcome back.
So in this lesson we get to cover one of the most important concepts in all of programming.
And in fact I might argue that it's actually the most important concept which is conditionals.
And the reason I say that conditionals are so important is that they are the one way that you add decisions
to your code.
It's how you add logic to your programming so give you a few examples of real world uses for conditionals
.
Let's take an example of user logon.
So when you go to log in on a website you type a password in that form and there is code that checks
that password that you typed in against the password in the database.
And if they match you log into the site and you get redirected to another page.
And if they don't match you usually get an error message or some sort.
So there's two different paths given the same exact code.
Another example would be when you buy something online when you type in your credit card information
.
If it works if the transaction goes through you might get a confirmation email you see a success screen
.
And if there's some problem with your information or you don't have funds in the account you're going
to get an error message.
Again two different outcomes.
So now let's go see conditionals in action.
So there are three key words that I need you to remember throughout this lecture which are if else if
and else.
So these are the three javascript conditional keywords and yes I know LCF is technically two English
words but to javascript that is just one key word.
So the example that we're going to use to illustrate conditionals is that of a bouncer at a concert
venue.
So the idea is that we're going to have an age variable and if that age if a person is younger than
18 Unfortunately that person cannot get into our venue if that person is older than 18 but they're not
yet 21.
That means that they can enter but we have to draw an X in their arm or in their hand because they cannot
drink.
And then lastly anyone else if you're greater than 21 you can get in and drink.
So those are the three things that we're going to try and write with code.
So before we move on we're going to do a quick recap of the Boolean logic operators from the last video
.
And the reason that we're doing that is that we actually use our three keywords that I mentioned at
the beginning if LCF and else in conjunction with these boolean operators to make our decisions to add
the logic.
So to recap we've got less than greater than less than or equal to greater than or equal to.
Pop quiz here.
What's the difference between these two.
So Pozzi video and try and remind yourself the difference between chirpily calls and double equals.
So the difference is that double equals performs type coercion while Tripoli equals as strict equals
.
So type coercion.
Let's start there.
Double equals means that the number one is actually going to be equal to the string one.
When we used double equals because javascript does not care about type.
However when we do triple equals which is strict It does care about type and value together.
So one is triple equal to the number one vote one would not be triple equal to the string one.
And then we've got these two operators and and or.
And remember that and is only true if both sides are true however or is true.
If one of the sides evaluates to TRUE.
OK so now let's actually move on to writing our logic.
So the first thing we're going to see is the if key word the if keyword takes a single expression instead
of parentheses which it will evaluate to be true or false.
And if it evaluates to be true whatever code we provide inside the curly braces in this case a console
like statement will be executed.
So this constant alt log will only be executed well it will only be printed out if age is less than
18.
If it is greater than 18 if it's equal to 18 we will not see this text printed up.
So that's the first part that's our bouncer checking if you are too young to get it.
So next up we're going to learn about else.
And the way that LCF works is that it follows an if statement.
It is a secondary condition.
So in this case we're checking if age is less than 18.
So if someone is too young to enter the venue if they're less than 18 they can't get in.
The next thing to check only after we check the first one is are they older than 18.
But less than 21.
So LCF works the exact same way as if we pass an expression in parentheses that will evaluate to be
true or false if it's true.
The code inside the curly braces will be executed if it is not true if it's false nothing happens.
So again LCF works just like if.
Except that it must follow in their statement.
So we have this line here.
Age greater than 18 and age is less than 21 which is actually a little bit redundant.
So think about this for a second.
Plus a video and try to identify how we could refactor this.
So this is a really minor change but you'll notice I got rid of if age is greater than 18 right here
.
And less than 21 and just turned it into if else LCF age is less than 21.
So again just to reiterate we had this before and we refactored it into this.
And the reason that we did it is it's redundant.
The only way that this code is going to run is if this first if statement is false.
So what that means is that this code here will only run if already we've established that age is not
less than 18 or that is greater than or equal to 18.
So if you are greater than or equal to 18 and less than 21 you can enter but you cannot drink.
So the last step is the.
Otherwise section which is.
Else so else allows us to just have something else happen any other time.
So in this case if you're not less than 18 and you're not between 18 and 21 that means that you can
come on in and you can drink and so else does not take an expression in parentheses.
It's different in that we just write else and then whatever we put in said it curly braces will only
be run as a last ditch effort.
So this will be run first and if nothing is printed out if this is not true then we'll move on to this
.
If this is not true then we'll move on to this.
But remember if along the way either of these is true then everything stops and this else will not run
.
So if this is true this code is done.
If this elf's if is false.
But this is true or else never runs.
So remember these are three different paths for one decision here.
OK so your exercise here there's a few of them is to take the code that I just gave you with the bouncer
for a concert venue.
And to add a few let's say quirky other conditions.
So if age is net negative we want to make sure that we print an error message.
If age is 21.
Exactly.
We want to print happy 21st birthday give them a free shot.
And if age is odd you should print your age is odd.
So you have to do a little bit of math there to figure out if it's odd.
And then the last one which is a bonus is to figure out if age is a perfect square and if you don't
remember what a perfect square is you can look that up but it's basically a number whose square root
the two factors are the exact same number.
So four times for 16 so 16 is a perfect square four is a perfect square because two times two is four
.
All right so that's a super bonus.
Have fun with this and I'll see in the next video.