 Hey everybody, this is Brian. Today we'll be talking about the if statement. What is the if statement? Well, it's a flow control method that allows you to make decisions. Let's just dive right in and say int x equals 12. What we're going to do is we're going to make some decisions based on the value of x. So we're going to say if and let's say x equals 12 and then you put some brackets in there. Now, this is called a code block. Anything in this code block is going to get executed if x is equal to the value of 12. So we'll just simply say system.out.printLine and we'll say x equals 12. So once again, we have a variable x and we're going to say if x is equal to 12 then execute this code. So what happens if x is not equal 12? It's going to jump right over this code. Let's run this and see it in action. x equal 12. So this is an else statement. What does the else statement do? Well, if you're a smart person, you might have already guessed that x equals 12 executes this, else execute this. To do that, we're going to change the value. We'll just say 10 just so you can see what happens here. x not equal 12. So you see it's making a decision. If x is 12, print this. If x is not 12, print that. Well, we can see x is not 12, so we'll just print out x is not 12. That's the basic foundation of decision-making right there is the if statement. Now, this may seem a little confusing, but this is the foundation of what programs run on is making decisions. It's called flow control. Now, how can you use this? Well, let's make a better example. Let's just make a boolean here. Whoops. mHungry equals false. And we'll say if mHungry equals true. Whoops. Then say else. So now we're going to make a decision. If you're hungry, do something. Otherwise, don't do something. And we'll say if you're hungry, go eat pizza because I like pizza and I'm actually getting hungry. And if you're not hungry, then well, let's say we will go run a mile. That's something I really want to do. So let's run this and see what happens. Go run a mile. Why are we going to go run a mile? Because hungry is false. Remember, we're saying hungry equal true. Now, why do we have these two dashes here? Equal equals is what that says. Why do we have those? Why don't we just say hungry equal true? Well, let's run that and find out what happens. Hungry is suddenly true, but we already said hungry is false. Well, this is an assignment operator. We're assigning this variable to the value of true. So really what we're doing here is we're saying hungry is equal true. And then we're saying if hungry is true, then I know it's kind of confusing. Basically, all you need to know is like your algebra class, anything in parentheses gets executed first. So it's going to analyze this little bit of code right here. It's going to say hungry equal true. And then it's going to say if hungry, then do something. So we want to use double quotes. Or not double quotes, double equal signs. I have pizza on the brain now. As you can see, our program is working again. And you can also nest these. Let's make another one of if we're a runner. And we'll say we are not a runner. If mrunner not equals true. So this is called a nest f then statement. It's nested inside. So let's just print some stuff out if runner. I am not a runner. And we can say I love to run. Now this looks a little bit confusing, I'm sure. Let's step through this. We've got two variables, hungry and runner. We're saying if we're hungry, we're going to go eat a pizza. If we're not hungry, it's going to check and say are you a runner? If no, not a runner. If so, I love to run. Notice this exclamation equals. What's that mean? Exclamation means not. So we're saying if it's not equal to true. Run this, see what happens. I am not a runner. So let's review this real quick. We're not hungry. We're not a runner. So it's coming here and saying nope, not hungry. Just jumping down here and saying runner not equal true. We are not a runner. Now let's change some of these things and say we are a runner. We love to run. Or at least my doctor would love it if I ran. I love to run. As you can see, we're executing this code and it's coming down here because we're not hungry and we are a runner. So it's saying I love to run. That's some basic decision making right there in action. That's how programs know how to do certain things. Like when you say close the program and it says, oh, you haven't saved your file, you should want to exit. And you say, no, no, no, that sort of thing. Let's say we are hungry. You notice how it just says go eat pizza. It ignores all this code down here because we are hungry. I hope that makes sense. If not, I recommend you make a couple little test projects and play around. Now what I want to cover real quick here are some operators. Let's go back to our handy. Oops, I screwed that up, didn't I? Let's add a little bracket in there. And we'll say int x equal three. And we'll say if x less than or equal to, whoops, x less than or equal to four, then system dot out dot print, whoops. Now you notice how this looks a little different than what we just saw. And I also misspelled int. We're just going to print out the word test for the sake of argument. So what we're saying is if x is less than or equal to four, then print out test. Is it less than or equal to four? Yes, because it's a three, so it's going to spit out the word test. Now if we change that to a five, notice how nothing happens. It just jumps right over it. So let's actually do this. We'll say greater than four. If it's greater than four, we're going to spit out the word yes. And as you see it is greater than four, so we can spit out the word yes. These are simple assignment or I'm sorry arithmetic operators. We've got greater than less than equal to not equal to. There's really more information out on the web. I'm just going to go ahead and assume that you've got some experience with this. If you don't, that is greater. That is less than. That is a sign. Whoops. That is equals. And this is not. So this would be not equal. That's the basics of it. There's more, but I'm going to let you go out and discover them on your own. I just really wanted to show you the structure of an if, then, and the else statement. All right. This is Brian. I hope you found this educational and entertaining and stay tuned.