 Hey everybody it's Brian and welcome to the fourth C-Sharp tutorial and go ahead and go file a new project console application and today we're going to learn the if-then statement. We've already discussed variables and we're just going to say bool and we're going to say happy equals true. So we're making a boolean a true false variable named happy and we're saying it is true. So type name assignment equals true. So we have a variable called happy. Now we want to do something with happy because well we're happy so we'll say if and then you put parentheses and then you put these brackets. That's called a statement block right here and in these parentheses just like in algebra no don't worry we're not going to cover math but whatever is in parentheses is evaluated first so we need an expression term. So we're going to say if happy and you notice how it is case sensitive. C-Sharp is case sensitive. If you use divisional basic lower case happy is not the same as uppercase happy. See how it's got that regs quickly? Says the name happy does not exist. So you need to be case sensitive. In other words when in doubt make it exactly the same. So we're going to say if happy and you could leave it at this but I want to really explain it here. If happy equal true notice the double equal sign. If you do one that's the assignment operator meaning you're assigning the value. If you do two you're checking the assignment. So if we say if happy equal true then we're assigning the value. You notice how it starts complaining it gets that little green squiggly. Usually I found in life when you have a green squiggly that's usually a bad sign. So make sure you do two. So if happy equals true then what we need to do is something here. So we'll say console dot right line. I am happy. Now let's say we want to see if we're not happy. Well you could write another if statement. In other words you could just take this and say if happy and do an exclamation not equals true. That's what the exclamation means is not if happy not equal true then I am not happy. And then let's go console dot read line and let's go ahead and run this. And it says I am happy because remember we set it to true. So let's set this to false and run this and see what happens. And it says I am not happy. So that is the basic if statement. Now one thing you could do rather than having two of these is you have what's called an else. Else just simply means the opposite. So if this is not true this statement right here then it'll run this block of code right here. Sounds confusing I know but it's very simple. So we can say I am not happy. So happy equals false what's going to happen here. It's going to come it's going to hit this if statement it's going to evaluate what's in the parentheses first. So it's going to say are we happy. Well no we're not. So it's going to jump down to the else statement and it's going to execute this block of code. It's going to say I am not happy. So let's run that. Sure enough I am not happy. Well I am actually happy today so we're just going to switch this over to true. Run this and show you what happens and sure enough it says I am happy. So once again this is the if statement. All you need to know is you have the word if followed by parentheses. Whoops followed by parentheses. And anything in the parentheses is going to be evaluated first. That's called an expression. Anything in here is called an expression. It's going to evaluate that expression and if the expression is true it's going to run this block of code. Else it's going to run this block of code. Another way you could do an if statement is just say if expression and then action. So console dot write line and let's just say really happy and let's say happy. Now you notice how this looks drastically different right here. We have if we have our expression but there's no equals it's just happy. So we're saying if happy. It's the same thing as saying if true because happy equals true. Now if happy was false it would say false. But we don't want to hard code that. We want to use the variable. So we're going to say notice case sensitivity there. There we go. If happy so it's going to evaluate this expression and say is it true then run this block of code. So let's run this and it says I'm happy. Really happy. Now usually when somebody says that you should just kind of you know avoid them. They're a little too happy. They're one of those people. But anyways so you see now how the if statement can be structured in different ways. Now you may be asking yourself well why go to the problem or why go through all the trouble of using these little squiggly lines. Well what if you want to do more than one thing. We'll say very happy. So if happy equals true it's going to say I'm happy I am very happy and then it's going to exit the block of code. It's going to jump over else because well it's not false. Then it's going to say if happy console right line. And then we're going to say really really happy. This is a common mistake people make when they first start. And I want to show you this. When you run this it says I'm happy I'm very happy. That's our first if block right there. Really happy really really happy and it looks like it works fine but let's switch this over to false. Let's say you know I'm having a crummy day you know the cat's being mean the boss yelled at me my truck didn't like me whatever what do you think is going to happen. Let's run this and find out notice that it says I am not happy really really happy. Well wait a minute what. It's not supposed to print that. That's because this really really happy is not part of an if statement. Because we don't have these blocks. We can only do one line of code right behind here. That's part of the if statement right there. This guy down here has nothing to do with that if statement. So if you want to do more than one thing you need to put it within the little parentheses here. So that's all for this tutorial. Hopefully you should understand what the if block does and the basics of a variable at this point. This is Brian. Thank you for watching. I hope you found this video educational and entertaining and you can find the source code for this out on my website voidromes.com which you can find in my YouTube channel.