Transcripted Summary

Nested if statements are paths inside of paths.

Nested If Statements

If a certain situation occurs, check for the next situation.

If a certain condition is met, it goes inside the if block and is immediately faced with yet another if statement. Let's look at an example where this might be needed.



To qualify for a loan there are two conditions that must be met. The person must make at least $30,000 and they must have also been at their job for 2 or more years.

Let's code this. I’m going to make a new class in chapter3 package called LoanQualifier.

I paste our instructions as comments

package chapter3;


import java.util.Scanner;

/*
 * NESTED IFS:
 * To qualify for a loan, a person must make at least $30,000
 * and have been working at their current job for at least 2 years.
 */
public class LoanQualifier {

}

Then create our main method.

public class LoanQualifier {

    public static void main(String args[]){

    }
}

Let's think through our design and add the steps as comments.

So first we are going to initialize what we know. Get what we don't. Make decision.

public static void main(String args[]){

    //Initialize what we know


    //Get what we don't


    //Make decision
}

All right. So, what do we know? Well, we know that the required salary is $30,000 and we also know that they have to be there for 2 years or more.

Let's go ahead and add statements for these.

Both variables are integers so we can define their data type as int.

public static void main(String args[]){

    //Initialize what we know
    int requiredSalary = 30000;
    int requiredYearsEmployed = 2;


    //Get what we don't


    //Make decision
}

So that's what we do know. Now let's get what we don't know.

What don't we know?

We don't know how much they make, nor do we know how many years they've been on their job so we need to get both of those.

Let's first ask them about their salary.

public static void main(String args[]){


    //Initialize what we know
    int requiredSalary = 30000;
    int requiredYearsEmployed = 2;


    //Get what we don't
    System.out.println("Enter your salary:");


    //Make decision
}

Now we want to read this answer. We're going to use a variable for salary. What should the data type be?

Well we could use int because that's what we used for requiredSalary since it didn’t have decimal numbers. But what if their salary is something with decimal numbers? We want to be able to account for that.

So we'll use the double data type. And if they happen to input a number with no decimals that's okay. The double can still hold integer numbers as well. So, this covers both cases.

public static void main(String args[]){

    //Initialize what we know
    int requiredSalary = 30000;
    int requiredYearsEmployed = 2;


    //Get what we don't
    System.out.println("Enter your salary:");
    Scanner scanner = new Scanner(System.in);
    double salary = scanner.nextDouble();

    //Make decision

}

Now, we need to get the next part which is how many years have they worked at their job.

We'll prompt them again, then input a value for the years. The years can also be a double if they worked like 2.5 years, for example.

public static void main(String args[]){

    //Initialize what we know
    int requiredSalary = 30000;
    int requiredYearsEmployed = 2;


    //Get what we don't
    System.out.println("Enter your salary:");
    Scanner scanner = new Scanner(System.in);
    double salary = scanner.nextDouble();

    System.out.println("Enter the number of years with your current employer:");
    double years = scanner.nextDouble();

    scanner.close();

    //Make decision

}

Now it's time to make the decision.

What we have here is not an if-else like we saw before and it's not as simple as just using two standalone ifs. What we need in this case is a way to make sure that both of these conditions were met. One way that we can do this is with the nested ifs.

So, we can check for the first condition. Let's do that.

//Make decision
if(salary >= requiredSalary){

}

We're not ready to do anything else because we have to check the next condition, which is have they been on their job for at least 2 years. So we can place the second if block inside of the first one. This is considered nested ifs. If the first condition is true, you are immediately faced with the next if and then we can make a decision

if(salary >= requiredSalary){

     if(years >= requiredYearsEmployed){

    }
}

If we’ve arrived inside of the inner if block then we know both conditions were true and therefore the requirement is met.

if(salary >= requiredSalary){

    if(years >= requiredYearsEmployed){
        System.out.println("Congrats! You qualify for the loan");
    }
}

Now, I'm going to add an else statement.

if(salary >= requiredSalary){

    if(years >= requiredYearsEmployed){
        System.out.println("Congrats! You qualify for the loan");
    }
    else{

    }
}

This else statement goes with the 2nd if statement. Even though there's two if statements here, the else will go with the immediate if that it follows.

So, if we find ourselves inside of this else it means that the condition, years >= requiredYearsEmployed, was false. But it also means that the condition, salary >= requiredSalary is true because if we look at our curly braces, we are still inside of the first if statement meaning that it was true.

So, we know if we get to this else statement, that they met the required salary but were lacking the years.

if(salary >= requiredSalary){

    if(years >= requiredYearsEmployed){
        System.out.println("Congrats! You qualify for the loan");
    }
    else{
        System.out.println("Sorry, you must have worked at your current job "
            + requiredYearsEmployed + " years.");
    }
}

Notice with the else statement, it spans two lines here. That’s because I was running out of space in the editor. To make things readable you can always go to the next line. Just use the semicolon when the statement is complete.

Reminder: Using Variables

Notice I filled in the variable requiredYearsEmployed in the else block. I could've just hard coded the number 2 in. But, the whole purpose of using the variables is so that if I ever wanted to update this program (e.g. change the required years to 1 year), I wouldn't have to go searching all through the program, I could just do it in one place. So we'll use our variable.

Let’s add an outer else statement

I'm going to come outside of the _if _statements. I want to add an else for the first if statement. You want to make sure the indentations line up as well so that it's really easy to read.

If we come to this point that means that the salary was not what it needed to be. So, we can print something to that effect.

if(salary >= requiredSalary){

    if(years >= requiredYearsEmployed){
        System.out.println("Congrats! You qualify for the loan");
    }
    else{
        System.out.println("Sorry, you must have worked at your current job "
            + requiredYearsEmployed + " years.");
    }
}
else{
    System.out.println("Sorry, you must earn at least $"
        + requiredSalary + " to qualify for the loan");
}

All right. So, there is our whole program.



# LoanQualifier.java

package chapter3;

import java.util.Scanner;

/*
 * NESTED IFS:
 * To qualify for a loan, a person must make at least $30,000
 * and have been working at their current job for at least 2 years.
 */
public class LoanQualifier {

    public static void main(String args[]){

        //Initialize what we know
        int requiredSalary = 30000;
        int requiredYearsEmployed = 2;

        //Get what we don't
        System.out.println("Enter your salary:");
        Scanner scanner = new Scanner(System.in);
        double salary = scanner.nextDouble();

        System.out.println("Enter the number of years with your current employer:");
        double years = scanner.nextDouble();

        scanner.close();

        //Make decision
        if(salary >= requiredSalary){
            if(years >= requiredYearsEmployed){
                System.out.println("Congrats! You qualify for the loan");
            }
            else{
                System.out.println("Sorry, you must have worked at your current job "
                + requiredYearsEmployed + " years.");
            }
        }
        else{
            System.out.println("Sorry, you must earn at least $"
                    + requiredSalary + " to qualify for the loan");
        }

    }
}

Let's run it.

We enter 25000 for the salary and 2 for the years. The output is

Sorry, you must earn at least $30000 to qualify for the loan

So, we know since the first condition was not met, it went into the outer else statement.



Okay. So, let's run this again where we qualify for the loan. We enter 30000 for the salary and 2 for the years. Output is

Congrats! You qualify for the loan



Let's go greater. Let's say $35000. And 3 years. Okay. Great.

Congrats! You qualify for the loan

And you see we can exercise this in many different ways. Let’s do the salary (30000) but not the years (1.5)

Sorry, you must have worked at your current job 2 years

So that is the nested ifs.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR