Link Search Menu Expand Document

Short Circuiting

Learning Objectives Covered

By the end of this lesson, you will be able to use short-circuiting to combine and simplify compound conditional expressions containing AND and OR logical operators.

Learning Materials

Time Commitment

Approximately 35-40 minutes


Watch (Approximately 3 minutes)

Duke Logical And / Or - Start at 5:30 and watch until the end of the video.

NOTE: This video is part of a Coursera course you probably already took. However, if you did not, you will be required to enroll. Make sure you choose to audit the course so you don’t have to pay for it.


Read (Approximately 20-25 minutes)

Short Circuit Evaluation Reading


Try (Approximately 10 minutes)

Instructions: For each question below, answer with the number of boolean expressions evaluated. For example, if expressions on both sides of an && would be evaluated, the correct answer would be 2.


Question 1

int[] guests = new int[10];
int roomCount = 10;

if (roomCount < 10 && guests[roomNum] != 0) {
    roomCount++;
} 
Show Answer

Evaluation Count: 1

Because roomCount < 10 evaluates to false, the && check short-circuits and only evaluates the first boolean expression. Here you can see short circuit evaluation protecting against an ArrayIndexOutOfBounds being thrown by the second boolean expression.



Question 2

boolean isFriday = true;
boolean isRaining = false;
if (isFriday && isRaining) {
    System.out.println("board games at my place!");
}
Show Answer

Evaluation Count: 2

Since isFriday evaluates to true, the second expression needs to be evaluated. Even through this second expression causes the overall expression to evaluate to false.



Question 3

How many boolean expressions are evaluated in the belcherNameLength method? We are going to call this method with the name “Teddy” as in belcherNameLength("Teddy");

private String[] belchers = {"Bob", "Tina", "Louise", "Linda", "Gene"};

/**
* Finds the index of name appears in the belchers array,
* otherwise returns -1 if the name is not in the array.
*/
public int find(String name) {
    for (int i = 0; i < belchers.length; i++) {
        if (belchers[i].equals(name)) {
            return i;
        }
    }
    return -1;
}

public void belcherNameLength(String name) {
    int index = find(name);
    if (index >= 0 && belchers[index].length > 8) {
        System.out.println("That's a Belcher with a long name!");
    }
}
Show Answer

Evaluation Count: 1

Because teddy >= 0 evaluates to false. Again you can see short circuit evaluation preventing an ArrayIndexOutOfBoundsException trying to get the -1 index of the array.



Question 4

boolean isTired = false;
boolean isNight = true;
if (isTired || isNight) {
    System.out.println("Take a nap.");
}
Show Answer

Evaluation Count: 2

Since here we are evaluating an or statement. This requires only one side of the || to be true and since isTired is false, the second expression must also be evaluated.



Question 5

public class Store {
    public boolean isClosed {
        return false;
    }
}
Store ballardPizzaCompany = new Store();
if (ballardPizzaCompany == null || ballardPizzaCompany.isClosed()) {
    System.out.println("Sorry, you can't have pizza.");
}
Show Answer

Evaluation Count: 2

Since ballardPizzaCompany is not null the first expression evaluates to false. This means the second expression must also be evaluated. Since it also evaluated to false, the overall expression evaluates to false. Here you can see another way we are preventing a NullPointerException.



Question 6

int value1 = 1;
int value2 = 2;
if((value1 == 1) || (value2 == 1)) {
    System.out.println("value1 is 1 OR value2 is 1");
}
Show Answer

Evaluation Count: 1

Since value1 == 1 evaluates to true. We already know at least one expression evaluates to true, so the expression can short-circuit and evaluate the overall || expression to true.