Skip to main content

Section 2.11 Compound Boolean Expressions

Subsection 2.11.1 And (&&), Or (||), and Not (!)

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 and the body of the condition will only be executed only if both are true.
Coding Exercise

Checkpoint 2.11.1.

What if you want to go out and your parents say you can go out if you clean your room and do your homework? Run the code below and try different values for cleanedRoom and didHomework and see what they have to be for it to print You can go out.
What if it is okay if only one of two things is true? Use || as a logical or to join two Boolean expressions and the body of the condition will be executed if one or both are true.
Coding Exercise

Checkpoint 2.11.2.

For example, your parents might say you can go out if you can walk or they don’t need the car. Try different values for walking and carIsAvailable and see what the values have to be to print You can go out.

Note 2.11.3.

In English, we often use an exclusive-or like in the sentence “do you want to be player 1 or player 2?” where you can’t be both player 1 and player 2. In programming, the or-operator is an inclusive-or which means that the whole expression is true if either one or the other or both conditions are true.
With numerical values, the or (||) operator is often used to check for error conditions on different ends of the number line, while the and (&&) operator is often used to see if a number is in an range.
Coding Exercise

Checkpoint 2.11.4.

Explore how && and || are used with numbers below. Try different values for score like -10 and 110 in the code below.
The not (!) operator can be used to negate a boolean value. We’ve seen ! before in != (not equal). 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.
Coding Exercise

Checkpoint 2.11.5.

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

Note 2.11.6.

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

Subsection 2.11.2 Truth Tables

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.
Table 2.11.7.
P Q P && Q
true true true
false true false
true false ?
false false false
Check your understanding

Checkpoint 2.11.8.

3-5-5: The truth table above is missing one result. What is the result of P && Q when P=true and Q=false?
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.
Table 2.11.9.
P Q P || Q
true true true
false true ?
true false true
false false false
Check your understanding

Checkpoint 2.11.10.

3-5-6: The truth table above is missing one result. What is the result of P || Q when P=false and Q=true?
Check your understanding

Checkpoint 2.11.11.

3-5-7: What is printed when the following code executes and x has been set to 3 and y has been set to 9?
if (x > 0 && (y / x) == 3)
{
   System.out.println("first case");
}
else
{
   System.out.println("second case");
}
  • first case
  • first case will print if both of the conditions are true and they are.
  • second case
  • second case will print either of the conditions are false.

Checkpoint 2.11.12.

3-5-8: What is printed when the following code executes and x has been set to 3 and y has been set to 6?
if (x > 0 && (y / x) == 3)
{
   System.out.println("first case");
}
else
{
   System.out.println("second case");
}
  • first case
  • first case will print if both of the conditions are true, but the second is not.
  • second case
  • second case will print if either of the conditions are false and the second one is (6 / 3 == 2).

Checkpoint 2.11.13.

3-5-9: What is printed when the following code executes and x has been set to 3 and y has been set to 6? Notice that it is now an or (||) instead of and (&&).
if (x > 0 || (y / x) == 3)
{
   System.out.println("first case");
}
else
{
   System.out.println("second case");
}
  • first case
  • first case will print if either of the two conditions are true. The first condition is true, even though the second one isn’t.
  • second case
  • second case will print if both of the conditions are false, but the first condition is true.

Subsection 2.11.3 Short Circuit Evaluation

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.
Check your understanding

Checkpoint 2.11.14.

3-5-10: 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");
}
  • first case
  • first case will only print if x is greater than 0 and it is not.
  • second case
  • second case will print if x is less than or equal to zero or if y divided by x is not equal to 3.
  • You will get a error because you can’t divide by zero.
  • Since the first condition is false when x is equal to zero the second condition won’t execute. Execution moves to the else.

Checkpoint 2.11.15.

3-5-11: 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");
}
  • first case
  • Since x is equal to zero the first expression in the complex conditional will be true and the (y / x) == 3 won’t be evaluated, so it won’t cause a divide by zero error. It will print "first case".
  • second case
  • Since x is equal to zero the first part of the complex conditional is true so it will print first case.
  • You will get a error because you can’t divide by zero.
  • You won’t get an error because of short circuit evaluation. The (y / x) == 3 won’t be evaluated since the first expression is true and an or is used.

Subsection 2.11.4 Summary

  • 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.
  • In Java, ! has precedence (is executed before) && which has precedence over ||. Parentheses can be used to force the order of execution in a different way.
  • When the result of a logical expression using && or || can be determined by evaluating only the first Boolean operand, the second is not evaluated. This is known as short-circuit evaluation.

Subsection 2.11.5 Boolean Game

Try the game below written to practice Booleans. Click on Booleans, look at the color and number in the block and evaluate the boolean expression to choose true or false. Then, check on Compound for an added challenge. We encourage you to work in pairs and see how high a score you can get.
window.scrollTo(0, 0);
You have attempted of activities on this page.