Please enable JavaScript to use CodeHS

Chapter 3

Boolean Expressions and if Statements

3.5 Compound Boolean Expressions

In the previous sections, you looked at how boolean expressions can be used in an if-else if-else statement. In this section, you are going to take a step back and look at the boolean expressions used in those conditional statements.

What Are Logical Operators?

Boolean variables can only take on one of two possible values, true or false. Logical operators allow you to combine and connect booleans in useful ways. When you use logical operators, the resulting expression will result in a single boolean value.

There are three fundamental logical operators:

  • NOT
  • OR
  • AND

These should look quite familiar; in fact, you use them in speech every day! The NOT operator is used on a single boolean, whereas AND and OR are used across multiple boolean values.

Logical operators can be used to help avoid nested if statements. A nested if statement consists of an if statement within an if statement. For example:

if (temp > 70) {
    if (sunny) {
        System.out.println("Time to get outside!");
   }
}
Plain text

Using logical operators, you can condense a nested if statement into a single statement, for example:

if (temp > 70 && sunny) {
    System.out.println("Time to get outside!");
}
Plain text

The AND Operator

AND is represented in Java as: &&. An expression using AND is true only when all its component parts are true. If any of the boolean values are false, the whole expression evaluates to false.

For example, if it is 6:30am AND it is a school day, you should wake up. You can test the expression of “6:30am AND a school day.” If both are true, then the whole expression evaluates to true. If either or both are false, then the whole expression is false, and you should stay in bed.

boolean sixThirty = true;
boolean schoolDay = false;
System.out.println(sixThirty && schoolDay);  // because both values aren't true, it prints "false"
Plain text

For variables p and q that can be true or false:

p q p AND q
true true true
true false false
false true false
false false false

The OR Operator

OR is represented in Java as ||. An expression using OR is true when all or any of its parts are true. It is only false when all of the boolean values are false.

Say that you are trying to decide whether to wear a coat today. You’ll wear your coat if it is raining right now or if it is cold outside. You can evaluate this expression based on the answers to those two boolean values. If it’s raining, cold, or raining and cold, then you will wear your coat. The only case in which you would not wear your coat is if it’s neither raining nor cold.

boolean isRaining = true;
boolean isCold = false;
System.out.println(isRaining || isCold); // it's not cold, but it is raining, so it prints true
Plain text

For variables p and q that can be true or false:

p q p OR q
true true true
true false true
false true true
false false false

Short Circuit Evaluation

Since AND expressions evaluate to false if any condition is false, and OR expressions will evaluate to true if any condition is true, there is a shortcut that Java uses to make your programs run more efficiently. If the first value of the boolean expression is false in an AND operation, or the first value is true in an OR operation, the remaining conditions are irrelevant!

Because these expressions will always evaluate to false and true respectively, regardless of what the other expression evaluates to, Java will not evaluate the second expression. This process of skipping the second condition in a boolean expression is called short circuit evaluation.

Expression Result
first && second If first is false, then the whole expression must be false. Don’t bother evaluating second.
first || second If first is true, then the whole expression must be true. Don’t bother evaluating second.

The NOT Operator

NOT is represented in Java as !. When placed in front of a boolean value, NOT causes the boolean to take on its opposite value. For example “NOT true” gives the answer “false.” This is fairly intuitive – if something is not true, then it must be false. Similarly, if something is not false, then it must be true.

The example below sets up a variable hungry as being true. Then it prints out NOT hungry.

boolean hungry = true;
System.out.println(!hungry);  // prints "false"
Plain text

For a variable p that can be true or false:

p !p
true false
false true

Order of Operations

Just like mathematical expressions have an order of operations, boolean operators also follow an order of operations. In the case of Booleans, the ! operator takes precedence over &&, and && takes precedence over ||. Here is an example:

boolean a = true;
boolean b = false;
boolean c = a && b || b
Plain text

In this example, c evaluates to false. The first part of the expression to be evaluated would be the a && b since AND takes precedence over OR. Since that part evaluates to false, the expression short circuits and returns false.

Light Switch

Number in Range

Pizza Slices

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    Which of the following will result in a logical short circuit assuming a is true and b is false?

Exercise: Compound Roller Coaster

To ride the rollercoaster, you must be at least 48 inches tall. You must also be at least 12 years old.

Ask the user how tall and how old they are. Use ONE if-else statement to determine if the user can ride the rollercoaster.

If they can ride, print "Welcome aboard!"
If they cannot, print "Sorry, you are not eligible to ride"