Java Fundamentals Nested If Statement
Transcript from the "Nested If Statement" Lesson
>> Sometimes you need to add an additional decision structure within the decision structure that you're already in. So these are called Nested If Statements. So it's a path inside of the path. So basically it says, if you make it past the first one, I'll need you to check for something else as well.
[00:00:20] So here's some example code. It says, if salary is greater than or equal to requiredSalary, go inside, and then we are met with another decision structure, this one is the if else. If the years are greater than or equal to requiredYearsEmployed, let them know they qualify for a loan, otherwise, they don't, okay?
[00:00:46] So you can nest these. If-Else-If is another decision structure. If situation A occurs, we wanna do something, else if B occurs, something else, else if C occurs, something else. So this is for when you have more than two possible paths, right? And you can go on and on with this however many you need, but it's basically a decision with more than two paths, all right?
[00:01:18] So we'll write another program. This one is saying, display a letter grade for a student based on their test score. So we're still in decision structure package, let's create a new class, we'll call this one, test results. So new Java class, TestResults. Okay, let's make our main method.
[00:01:44] And we want to go ahead and get the test score, so let's ask the user to enter your test score. And we'll make a scanner. I'm gonna read from the input, make sure we import. And this test score we're gonna read in as a, let's read it as double, just incase there is half credit, all right?
[00:02:21] So we'll call this score, and we'll say scanner.nextDouble, perfect. And let's close our scanner. All right, now we need to determine the letter grade. Now, the letter grade can be A, B, C, D, or F. This is a single letter, so I'm gonna introduce a new data type.
[00:02:48] So we've seen strings which are multiple letters. Single characters are called char, so it's C-H-A-R. And we can name it grade, okay? I don't know what the value is yet, and it's fine to declare variables and then later go ahead and set the value. So it's based on this decision structure with this variable will be initialized to.
[00:03:18] So I'm gonna say, if the score is less than 60, then the grade is equal to, and characters use a single quote, so F. All right, else and not just else, else-if. So remember when we did if-else, we had one condition, and it shared it amongst them. With the if-else-if, you add a condition on each one of these elses, except for maybe the final one, if you want.
[00:03:56] So we'll say else if, the score is less than 70, then The grade equals D. Now, let's say that the grade is 65. This first condition is true and the second condition is true, right? With this decision structure, the if-else-if decision structure, what the code is gonna do is find the first condition that is true, and it's only gonna execute that one.
[00:04:37] So even if other conditions happen to be true, it won't look at any other paths. It found the one path that it wants to take, and that's the first condition that's true, all right? Else if score is less than 80, then we have a C. Else if score is less than 90, We have a B.
[00:05:14] And then finally, so for the final else, it's up to you, you can do another else-if, or you can just do a plane else and say, if none of those were true, just do this, all right? Grade equals A. Questions on this so far? Perfect, last thing is just to print it out.
[00:05:44] So we'll say, your, oops, put it in quotes, your grade, and then append the grade. Let's run it. Someone give me a grade. A score. 0 to 100, quickly.
>> 78, so my test score is 78.
>> What should the grade be based on our code?
>> That is a C, all right. Very good, okay? One more, let's just do over 100. Okay, extra credit. Yeah, got it A, right, cool.