Check out a free preview of the full Java Fundamentals course
The "If-Else Statement" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:
Angie adds an else statement to the previously created if statement which will be applied if the condition is not met.
Transcript from the "If-Else Statement" Lesson
[00:00:00]
>> All right let's talk about more decision structures. So there's also the if-else statement. So if a certain condition occurs, you do something, otherwise, you do something else. So it's two possible paths that can happen based on a condition, all right? So we'll write another program. This one is a quota calculator.
[00:00:27]
So all salespeople are expected to make at least 10 sales each week. For those who do, they receive a congratulatory message. For those who don't, they're informed of how many sales they were short, all right? Let's code it up. So we'll close this one, go back to our decision structure package, and we'll create another class here.
[00:00:58]
We'll call this one quota calculator. So let's create our main method. We're going to ask for some input. So, well, before we ask for the input, what do we already know? The quota needs to be what? 10, right? So let's go ahead and declare that. And then we'll ask them how many sales, right?
[00:01:39]
We'll read that in with the scanner. You're getting good with the scanner? Should be, and then we will, we forgot to close our scanner in our last program, didn't we? So what I like to do is go ahead and do it right away so I don't forget. And then go back to whatever code I want, all right.
[00:02:09]
So we've opened the scanner and we want to read in the number of sales. So we'll say, int sales = scanner.nextInt(). All right, and now our close is good. Okay, so we've said, if they did at least 10, right, we would give them a congratulatory message. If not, we would let them know that they didn't [LAUGH] do what they needed to do, right?
[00:02:42]
So we're gonna set this up with the FL structure, because that's two different things that need to occur, right? So we say if, we put our condition here, which is sales is greater than or equal to, right, the quota. Put a set of curly braces here. If this happens, we want to let them know that they did a good job.
[00:03:13]
So congrats! You've met your quota. Otherwise, so you specify otherwise with an else. The else doesn't need parentheses in a condition, because the one condition applies to both of these branches, right? So we just say else with a set of curly braces. So if this condition is true, it'll go inside of the if.
[00:03:45]
If it's not true, it'll go inside of the else. And here we'll go ahead and print, you did not make your quota. Now our instructions also said, to let them know how much they came up short, right? So we can do a little calculation before we print this out and say how short they were.
[00:04:09]
So we can say, int salesShort = quota- sales. And we can add this here. You were, now I'm adding this inside of a sentence, before, we've only appended it to the very end. So there's a couple of ways to do this. The most straightforward way is using the plus sign mid-sentence, and then saying, salesShort.
[00:04:53]
So that's the variable. And then add in another plus sign in finishing the sentence. So you wanna put spaces before and after that within your quotes. Sales short, all right. I think we're good to go here. Questions before we run? All right, let's run it. So let's do one that is under 10.
[00:05:31]
Tell us, great, you didn't make it, you were 1 sale short. Let's run again. This time we'll do right at 10. Congrats, you've met your quota. Let's do above 10. Great, you've met the quota.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops