 Hey, everybody and welcome to the 12th lamp tutorial today We're going to be covering the if and else statements. Now. These are conditional statements. Now. What does that mean? Conditional statements. Well, if you ever had a parent look at you and say if you don't do this This is what's gonna happen or if you do that something bad is gonna happen or something good. What's called a condition? Sometimes people caught conditional love. I love my cat, but if it bars on the carpet, I'm suddenly mad at it So alright We're gonna look at the structure of an if statement. You can see the syntax is if and then in parentheses condition Code to be executed if condition is true and then semicolon That looks really complex, but it's actually very simple. Here's an example if D dollar sign or the D variable and up here. We're signing at the date If the day is Friday echo have a nice weekend So what you're seeing here is you're saying if the start of our conditional statement our condition If it evaluates to true the code is executed also There's an if else if condition execute this code, but if this is not true Then jump down here to else this code block right here for example Day equals Friday if day equals Friday have a nice weekend. Otherwise have a nice day and then there's the if else if else Sounds pretty confusing, but it's actually pretty simple same thing if then execute this code. Otherwise else if Execute this code else execute this code now. What that means. Let's look at this example here The D variable is the day if the day equals notice the two equal signs. We discussed that in our operators Tutorial if the day is Friday have a nice weekend else if the day is Sunday Have a nice Sunday else. Just have a nice day. So let's actually jump in and write some code ourselves here Got our handy little Program. Well, let's make a variable Let's call it age and I am 38 years old So just enter whatever your age is and we're gonna say if age equals 38 Echo You are still young. I know some of you out there that are younger than me are going. No, you're not Shame on you. Be nice to your elders so If age is 38, which it is then you are still young. So let's actually change this Let's say if age Greater than 38 You are older than me And we're going to say Whoops last track of my mouse there Else notice how it pops up else or else if Echo Don't forget your semi colon You are not older than me You are not older than me. Why because 38 We're saying if the age is greater than 38 You are older than me. Otherwise Now you see why the else if is there Let's do another example here. Let's just change this to well, it's the retirement age in the United States is 65 So actually, let's do it this way leave a current age. Sorry about that I got I had to I had to bribe my cats to leave me alone with catnip and apparently the catnip's wearing off Because they're both sitting here messing with me So if it's over 65 age is greater than 65 then echo you were retired else You are not retired. Let's see here You are not retired Now I'm going to go over this a little bit more just in case you're new to programming If you are not new to programming this should look very familiar if you're used to a C style language like Java C sharp C Plus plus you're probably screaming at me to get to the point All right. Our age variable is 38 We've assigned it using the equal sign now. We're saying if age is Greater than 65 you are retired We should probably say a greater than or equal to because 65 is a retirement age Else you are not retired because it's 38 it hits this if statement evaluates this condition in parentheses and Remember your math classes from school anything in parentheses will get executed first So that evaluates defaults, so it jumps right over This block of code down to this else statement and an else is kind of a catch-all saying okay nothing happens So do this So then this block of code is executed, which is why it says you are not retired. Let's change our variable to 72 Suddenly you are retired because it evaluated and says is age 72 greater than or equal to 65 Yes, it is so we're going to execute this code Let's switch this back down to our current age here, and we're going to throw in an else if Now notice what the else if We put in the parentheses again. Why because we have to evaluate a condition so we're going to say Age greater than or equal to 55 we'll say echo You are almost retired Now if we save that it just says you're not retired because we're 38 Remember it's going to say if age Evaluate or just say if evaluate this condition age greater than or equal to 65 Does not equal true, so we're going to jump over this then it's going to do this else if if age greater than or equal to 55 It's going to do this, but it didn't evaluate to true, so it's jumping down here now. Let's change this to 55 You are almost retired. So what's going on here is we're saying if evaluating this condition That's not equaling true. So we're jumping down to the else if Evaluating this condition and this time it's true because we are 55 and It's executing this code In case you haven't figured it out by now the if statement is the real workhorse of any programming language Programs are basically decisions Do I get up on time? You know, it's an if statement if I get up late Am I going to get in trouble with my boss if I get up early? Am I going to get a promotion from my boss? Where I work not likely, but you never know Anyways, that's all for this tutorial. I hope you found this educational and entertaining and remember programming is not hard