Java Lesson 4 | Conditional Statements
Hey, this is Jake. I'm a special guest on Mac Heads 101. This will be my fourth Java tutorial, and today I'm going to be talking about conditional statements.
What that is, is you give it a condition, and if the condition is true, it executes a certain block of code. If it's false, it doesn't execute that block of code. So, the program we're going to be making here is you enter a price you paid for your new MacBook, and it tells you if it was a MacBook, a MacBook Air, or a MacBook Pro.
Since we're going to be getting input here, our first thing we're going to do is import java.util.Scanner
. Then, I'm going to create that scanner object. Scanner, I'm just going to call scan = new Scanner(System.in)
.
Alright, so now I'm going to make prompts to ask them how much the new MacBook was. So, "How much did you pay for your new MacBook?"
Alright, so now we're going to create a double for the price: double price = scan.nextDouble();
. So, what that's going to do is it's going to ask them how much they paid for their new MacBook, and then whatever they entered is stored in the double price.
So, we went over that in the last tutorial, but now what we're going to do is get to the meat of this tutorial, which is a conditional statement. The first one I'm going to show is if.
So, you type if
and then in the parentheses you put your condition. Then, in these curly braces, you put what you want to execute if the condition is true. So, let's say if price == 999
, System.out.println("MacBook, that's how much it cost!");
.
So, what I did here, let me explain. The equals equals— you see, if you use an equal sign, just one equals, it will assign price the value of 999 because equals will assign values.
Equals equals is used in comparing. So, if I want to see if price is equal to 999, I use two equal signs. And I'll share a couple more ways to compare right now!
!=
equals means not equal, so let's just say here, if it's not equal to 999, System.out.println("You did not buy a MacBook!");
.
Another way is the less than, greater than, less than or equal to, greater than or equal to. Okay, so I'd say here, "MacBook."
Alright, now the next thing is the else if statement, which is like you make an if statement, and then it checks if that is true. If that's not true, then it goes down to see if there's an else if statement. If the else if statement checks if that is true, so else if (price < 999)
, System.out.println("You did not buy a MacBook :(")
because they should buy a Mac.
So, the next thing I'm going to talk about is the or. So if I wanted to, I could do this and then say if what they paid was over 2500, they probably didn't buy a MacBook. Is that the price of the most expensive MacBook Pro?
Or instead of making two conditional statements, I can do this. I put two vertical bars, which is another word for or essentially. So, or (price > 2499)
, System.out.println("You did not buy a MacBook!");
.
So, what or means essentially is execute this block of code if either condition is true, and I put an ampersand sign. That means only execute this block of code if both conditions are true.
So, for now, else if (price == 1499 || price == 1799)
, System.out.println("You bought a MacBook Air!");
.
Now, I'm going to make one more else if and then I'm going to talk about just the else statement. This is to see if they bought a MacBook Pro. If price == 1699 || price == 2299 || price == 2499
, then System.out.println("You bought your MacBook Pro!");
.
Okay, so now we have the else statement, which goes down and checks if this is true, or no. If nothing else is true, what do you want to do? If nothing is true, and that's what goes in the else block, and there's no condition because, for obvious reasons, System.out.println("Re-enter your price.");
.
This probably meant they entered something like 998 or something by accident, like "Re-enter your price."
So, I'm just going to run this and show you what it does. "How much did you pay for your MacBook?" I paid nine hundred ninety-nine dollars. "Well, you bought a MacBook!"
"How much did you pay for your new MacBook?" Well, I paid ten dollars. "What? You didn't buy a MacBook!" Clearly, because it's only 10 dollars!
"How much does it cost?" Let's say I paid two thousand two hundred ninety-nine dollars. "Well, you bought your MacBook Pro!" Cool beans for you.
Um, and let's just test the last one. I paid $1,500. "Re-enter your price."
Alright, so yeah, that is conditional statements, and I will see you next time!