Loading

Java: If, Else If, Else

If, Else If, Else

This Java video tutorial examines the uses of the conditional if statement in Java. The if statement allows us to designate a block of code that is to be executed only under certain circumstances. Namely, the if statement uses a boolean value, and executes its block of code if that value is true.

The If Statement

The If Statement

Program 1

package test;

public class Test {
    public static void main(String[] args) {
        boolean bIsOpen = true;
        if (bIsOpen) {
            System.out.println("The door is open.");
        }
    }
}

Output

Program Output

In the program above, we begin by declaring a boolean variable named bIsOpen; this usage of a verb in the name follows the general convention for boolean variables. After that declaration and assignment, there is an if statement that evaluates whether the boolean value is true or not. If it is true, the code inside the braces is executed. Otherwise, it is not. So, when we run this code as it is shown above, we see the message "The door is open." If we set the boolean to false and execute it, the program will display nothing.

if (boolean) {
    // code for true
} else {
    // code for false
}

We often want have two branches, one that is executed when a boolean is true and one that is executed when it is false. To do this, we can add an else branch.

The If-Else Statement

The If-Else Statement

Program 2

package test;

public class Test {
    public static void main(String[] args) {
        boolean bIsOpen = false;
        if (bIsOpen) {
            System.out.println("The door is open.");
        } else {
            System.out.println("The door is closed.");
        }
    }
}

Output

Program Output

The second program is very similar to the first, except that we have added an else branch. The else branch is a executed when the boolean value is false, as shown above.

We can string together many conditions in a chain, using else if branches. Each else if branch uses a boolean to determine whether or not it should be executed. Each branch is evaluated in order, and only the first branch with a true value is executed.

The If-Else If-Else Statement

The If-Else If-Else Statement

Program 3

package test;

public class Test {
    public static void main(String[] args) {
        boolean bIsBlue = false;
        boolean bIsRed = true;
        boolean bIsGreen = true;
        if (bIsBlue) {
            System.out.println("The ball is blue.");
        } else if (bIsRed) {
            System.out.println("The ball is red.");
        } else if (bIsGreen) {
            System.out.println("The ball is green.");
        } else {
            System.out.println("The ball is not blue, red, or green.");
        }
    }
}

Output

Program Output

In our final program, we have three boolean variables. As we stated before, each else if is evaluated in order and the first only the first branch that is true is executed. This is why the bIsRed branch is executed and not the bIsGreen, even though it is also true.

As we mentioned in the video, we can always leave out the else branch; in that case, nothing will happen when all of the boolean values are false. We can also nest if statements to make code that is evaluated for multiple conditions.