Transcripted Summary

The if-else statement works as two separate paths within a program.

The If-Else Statement

If a certain situation occurs, do something. Otherwise, do something else.

With the if statement that we looked at in the last section, we took a quick detour when a condition was met, but then we got back on a common path.

However, with the if-else statement, it differs in that it has two distinct paths, and the call will either go down one path or the other. After this branch-off is completed, then the program resumes onto the common path.



For this new scenario, we have two different situations to take care of, those who have met their quota and those who have not.

Let's code this using the if-else statements.

In our editor, I'm going to add another class to the same package.

This one we'll call it QuotaCalculator

And let's go ahead and add our comment, to state what we're doing. So, for this one we're using the if-else.

package chapter3;

/*
 * IF ELSE
 * 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 are informed of how many sales they were short.
 */

public class QuotaCalculator {

}

Okay, so we're going to of course add our default method:

public class QuotaCalculator {

    public static void main(String args[]){

    }
}

And let's go ahead and put our design in comments — initialize values we know, get the values we don't, make a decision on the path to take and then output.

    public static void main(String args[]){
        
        //Initialize values we know

        //Get unknown values

        //Make a decision on the path to take – Output 
    }

All right, so let's see, what do we know here?

Well, we do know that the quota is 10.

//Initialize values we know
int quota = 10;

But we don't know how many sales they made this week.

So, let's ask them.

//Get unknown values
System.out.println("Enter the number of sales you made this week:");

And we'll create a Scanner to get input and read that value into a variable that we'll call sales. From Scanner, we’ll call nextInt() because the number we want to read in is an integer. Then after reading the input, we close the scanner.

Scanner scanner = new Scanner(System.in);
int sales = scanner.nextInt();
scanner.close();

Now we're ready for the decision part.

With the if-else, you're essentially saying, "Do you want to go down this path or this other path?"

Let’s start our if statement

//Make a decision on the path to take – Output 
if(sales >= quota)

We’re using a new operator here: >=. This represents “greater than or equal to”.

If this condition is true, we’ll print the results.

So, our print statement goes inside of curly braces.

if(sales >= quota){
    System.out.println("Congrats! You've met your quota");
}

However, if it's not true, we need to do something else differently. So, it's not just a matter of continuing on with the program like we did last time, we have to actually do something if this is false.

That's where the else statement comes in.

So, we type in else followed by curly braces.

else{

}

Notice, the else doesn't have parentheses for the condition because it's not needed. The condition that’s specified with the if works on both this if and else together. And this is saying, "If this condition is true, go inside of the if block. Else, if this condition is false, go inside of the else block." Each of these paths represents a branch.

Inside of the else statement, we want to let them know that they were short, but the directions also say to let them know how many sales they were short. Remember, as a programmer, it's very important to pay attention to the instructions of what it is that you're supposed to be doing.

Let’s use the minus sign to do subtraction, and then store this calculation in a variable called salesShort.

if(sales >= quota){
    System.out.println("Congrats! You've met your quota");
}
else{
    int salesShort = quota - sales;
}

Then here we can append that variable’s value to a String that we print out

else{
    int salesShort = quota - sales;
    System.out.println("You did not make your quota. You were " + salesShort + " sales short");
 }

All right let's run this.



# QuotaCalculator.java

package chapter3;

import java.util.Scanner;

/*
 * IF ELSE
 * 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 are informed of how many sales they were short.
 */
public class QuotaCalculator {

    public static void main(String args[]){

        //Initialize values we know
        int quota = 10;

        //Get unknown values
        System.out.println("Enter the number of sales you made this week:");
        Scanner scanner = new Scanner(System.in);
        int sales = scanner.nextInt();
        scanner.close();

        //Make a decision on the path to take - Output
        if(sales >= quota){
            System.out.println("Congrats! You've met your quota");
        }
        else{
            int salesShort = quota - sales;
            System.out.println("You did not make your quota. You were " + salesShort + " sales short");
        }
    }
}

How many sales did I make this week?

Let's say we met the quota, so we enter 10.



We get "Congrats, you've met your quota." Since the condition was true, it executed the code inside of the if block.

Let's do it again where we don't meet the quota. Let's say that we had 4 sales. The outcome is “You did not make your quota. You were 6 sales short”. Aha, so the condition is now false, so it executed the code inside of the else block.



There's another thing I want to show you about the if and else statements.

These curly braces are meant to encompass zero or more statements. If we only have one statement, we don't really need these curly braces. This will work just as fine.

if(sales >= quota)
    System.out.println("Congrats! You've met your quota");

So, if you see an if or else block that does not have the curly braces, you can read it all as one statement. If this condition is met, it's going to simply execute the next line.

We could not remove these curly braces in this particular else block because there are two statements that have to be executed inside of that block. You can only omit the curly braces if there’s only one statement to execute as part of the if or else.

if(sales >= quota)
    System.out.println("Congrats! You've met your quota");

else{
    int salesShort = quota - sales;
    System.out.println("You did not make your quota. You were " + salesShort + " sales short");
}


Resources