πŸ““2.5: Compound Boolean Expressions

Table of Contents


πŸ“– This page is a condensed version of CSAwesome Topic 2.5


Compound Boolean Expressions

VENN DIAGRAM ACTIVITY

  1. In the printed Venn Diagram, label each circle with the names of the 3 people in your group.
  2. πŸ“ Write down the age of each person in your group in the circles. If two or more people are the same age, put that age in the intersecting parts of their circles as well.
  3. Then, ask each person in your group their favorite movie. If two or more people have the same favorite movie, put the movie in the intersecting parts of their circles.
  4. Think of a few more pieces of information to share, finding as much common ground as possible!
  • We can write a Compound Boolean Expression that compares the age of every person in the group using ==, <, >, and &&, for example Ada’s age > Alan’s age && Alan’s age == Grace’s age.
  • Write a Boolean expression that compares the favorite movies in the group using ==, !=, and &&, for example Ada’s movie == Alan’s movie && Alan’s movie != Grace’s movie.

And (&&) Operator

What if you want two things to be true before the body of the conditional is executed? Use && as a logical and to join two Boolean expressions. The body of the condition will be executed only if both are true.

Let’s imagine you have fun plans on a Friday night, but your super strict parents say you only can go out if you clean your room AND do your homework?

boolean cleanedRoom = true;
boolean didHomework = false;

if (cleanedRoom && didHomework) {
  System.out.println("You can go out");
}
else {
  System.out.println("No, you can't go out, and furthermore, you're grounded.");
}

Test the code and try different values for cleanedRoom and didHomework and see what they have to be for it to print "You can go out".

Or (||) Operator

What if it is okay if only one of two things is true? Use || as a logical or to join two Boolean expressions. The body of the condition will be executed if at least one part is true.

Your parents might say you can go out if you can walk to the event, OR if they don’t need the car.

boolean walking = false;
boolean carIsAvailable = false;

if (walking || carIsAvailable) {
  System.out.println("You can go out");
}
else {
  System.out.println("No, you can't go out");
}

Try different values for walking and carIsAvailable and see what the values have to be to print "You can go out".

Not (!) Operator

The not (!) operator can be used to negate a boolean value. We’ve seen ! before in != (not equal).

The code below says if homework is not done, you can’t go out. Try different values for homeworkDone.

boolean homeworkDone = false;
if (!homeworkDone) {
  System.out.println("Sorry, you can't go out!");
}

If you use ! in expressions with && and ||, be careful because the results are often the opposite of what you think it will be at first. We’ll see examples of this in the next lesson.

In Java, ! will be executed before &&, and && will be executed before ||, unless there are parentheses. Anything inside parentheses is executed first.

Truth Tables

AND (&&) TRUTH TABLE

The following table (also called a truth table) shows the result for P && Q when P and Q are both expressions that can be true or false. An expression involving logical operators like P && Q evaluates to a boolean value, true or false. As you can see below the result of P && Q is only true if both P and Q are true.

P Q P && Q
true true true
true false false
false true ???
false false false

πŸ’¬: The truth table above is missing one result. What is the result of P && Q when P = false and Q = true?

OR (||) TRUTH TABLE

The following table shows the result for P || Q when P and Q are both expressions that can be true or false. As you can see below the result of P || Q is true if either P or Q is true. It is also true when both of them are true.

P Q P || Q
true true true
true false true
false true ???
false false false

πŸ’¬: The truth table above is missing one result. What is the result of P || Q when P = false and Q = true?

Truth Table Practice

  1. Write the sentence β€œIf it’s sunny, OR if the temperature is greater than 80 and it’s not raining, I will go to the beach.” as a Java if statement using an int variable temp and boolean variables isSunny and isRaining. If the conditional is true, print out β€œGo to the beach!”.

    So, you will go to the beach on days that it is sunny (in any temperature), OR you will go to the beach on days when the temperature is over 80 degrees AND it’s not raining.

  2. Complete a truth table for the if statement that you wrote with columns for the conditions isSunny, temp > 80, isRaining, and outcome (go to the beach).

Short Circuit Evaluation

image-small

Both && and || use short circuit evaluation. That means that the second expression (on the right of the operator) isn’t necessarily checked, if the result from the first expression is enough to tell if the compound boolean expression is true or false:

  • If two boolean values/expressions are combined with a logical or (||) and the first expression is true, then the second expression won’t be executed, since only one needs to be true for the result to be true.
  • If two boolean values/expressions are combined with a logical and (&&) and the first expression is false, then the second expression won’t be executed. If the first expression is false, the result will be false, since both sides of the && need to be true for the result to be true.
  1. What is printed when the following code executes and x has been set to 0 and y to 3?
    if (x > 0 && (y / x) == 3) {
      System.out.println("first case");
    }
    else {
      System.out.println("second case");
    }
    
  2. What is printed when the following code executes and x has been set to zero and y is set to 3?
    if (x == 0 || (y / x) == 3) {
      System.out.println("first case");
    }
    else {
      System.out.println("second case");
    }
    

Boolean Game

🎲 Try the game below written to practice Boolean Expressions. Click on Booleans, look at the color and number in the block, and evaluate the boolean expression to select true or false. Then, check on Compound for an added challenge. See how high a score you can get!


Summary

  • (AP 2.5.A.1) Logical operators ! (not), && (and), and || (or) are used with Boolean values.

    • A && B is true if both A and B are true.

    • A || B is true if either A or B (or both) are true.

    • !A is true if A is false.

  • (AP 2.5.A.1) ! has precedence (is executed before) && which has precedence over ||. (Parentheses can be used to force the order of execution in a different way.)

  • (AP 2.5.A.1) An expression involving logical operators evaluates to a Boolean value.

  • (AP 2.5.A.2) Short-circuit evaluation occurs when the result of a logical operation using && or || can be determined by evaluating only the first Boolean expression. In this case, the second Boolean expression is not evaluated. (If the first expression is true in an || operation, the second expression is not evaluated since the result is true. If the first expression is false in an && operation, the second expression is not evaluated since the result is false.)


Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.