yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 4 | Conditional Statements


4m read
·Nov 3, 2024

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!

More Articles

View All
Is the Metaverse a dystopian nightmare? | Matthew Ball
It’s common to hear the criticism that the Metaverse is an inherently dystopic ideal. And the challenge, of course, with that is while many people are familiar with the term, there is disagreement over whether or not this is just some conquest to techno-f…
EXCLUSIVE: Fur Seals Are Back From the Brink on California Islands | National Geographic
The northern fur seal was a top predator in this area, and 150,000 of them were removed from the ecosystem. My name is Jim Teats, and I’m a biologist for Point Blue Conservation Science. I work on Southeast Farallon Island, which is 30 miles west of San F…
Introduction to agreement | The parts of speech | Grammar | Khan Academy
Hi Garans, today I want to talk about this idea in English that we call agreement. So, I’m going to teach you how to be agreeable and make it so that all of your sentences get along really well. Let me give you an example: the dog barks as opposed to the…
TIL: These Spiny Sea Creatures Can Regrow Lost Body Parts | Today I Learned
There’s an incredible group of animals out there called the echinoderms. They can actually regenerate a lost body part. So, a kind of derm essentially just means spiny skin, so derm like dermis, so skin, and a chi know is sort of spiny. So, sort of spiny …
A host of heroes - April Gudenrath
Transcriber: Andrea McDonough Reviewer: Bedirhan Cinar “I need a hero!” So many people in distress have said this, but why? What kind of hero do we need, and do we even really need a hero at all? Well, if you look at any piece of literature written for p…
Food Too "Ugly" to Sell Becomes a Feast for 5,000 People | National Geographic
Feeding the 5,000 is a celebration of the solutions to food waste, where we feed 5,000 people a delicious meal made entirely out of food that would otherwise have gone to waste. America is a country which has a massive problem of food waste. Forty percent…