IF, ELSE, ELSE IF statements in Java

The if statement is the most basic and perhaps the most useful "control flow statements." By control flow statements we mean statements that help our program jump from one place to another instead of sequentially executing each line in our code.

The if statement essentially says: IF something is true then do what follows in the IF block and when that something is NOT true (i.e. false) then just go to the end of the IF block and skip what is within the IF block. The following syntax will make it clear.

Listing 1: IF statement syntax
if (this expression is true) {

//do whatever is here

}
/* when the above expression is false continue from here and skip what is
within the curly braces for the above if...*/

Notice that the above if statement does not take care of what happens when the expression evaluates to false. The program just continues to execute right after the ending curly brace of the if block. To ensure that we do something specific when the if condition is false, we can use the else statement. Syntax listing 2 below will make it clearer.

Listing 2: IF and ELSE statement syntax
if (this expression is true) {

//do whatever is here

} else {

//do whatever is here (i.e. when the expression evalutates to false)

}
/* continue from here...*/

Finally, there is a possibilty that we wish to further check for another expression right after we have an IF and ELSE combination. In other words the new if condition is right along with an else statement pertaining to a previous if statement. See listing 3 below for syntax.

Listing 3: IF and ELSE IF statements syntax
if (this expression is true) {

//do whatever is here

} else if (check whether this expression is true) {

//do whatever is here and do not go further down

} else {
// if niether if condition is true, do this as a catch all
}
/* continue from here...*/

Overall, the syntax in listing 3 says that check for the second if condition only if the first one is false. It also says that when none of the if conditions appearing together are true then do what is in the final else block. You can use this format to check for multiple expressions. The following example should help make it clear.

The program checks a score and assigns a grade based on the score.

Example 1: IF and ELSE IF Demo
class SimpleIfElseDemo {
    public static void main(String[] args) {

        /* declare a variable called score which is an integer
        and initialize it to 65. Of course you can also use a Scanner 
        to obtain the score from a user*/
        
        int score = 85;
       
        char grade;

        if (score >= 90) {
            grade = 'A';
        } else if (score >= 80) {
            grade = 'B';
        } else if (score >= 70) {
            grade = 'C';
        } else if (score >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }
        System.out.println("Grade is: " + grade);
    }
}

Can you tell what will be the output of this program.

Also, try removing the else parts from all the else if statements. Leave in the final else block (related to grade F). Run the program. What is the output now and why?

Links: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

Future links