π3.5: Compound Booleans
Table of Contents
π This page is a condensed version of CSAwesome Topic 3.5
π Take notes in a Codespace during class, coding along with the instructor.
- Go to GitHub and click on your picture in the TOP RIGHT corner
- Select
Your repositories
- Open
CS2-Unit-3-Notes
- Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
(unless you already have one listed there), wait for the environment to load, then youβre ready to code!
Compound Boolean Expressions
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 and the body of the condition will only be executed only if both are true.
What if you want to go out and your parents say you can go out if you clean your room and do your homework? Test 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"
.
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");
}
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 and the body of the condition will be executed if one or both are true.
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"
.
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");
}
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
whenP = false
andQ = 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
whenP = false
andQ = true
?
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 istrue
, then the second expression wonβt be executed, since only one needs to betrue
for the result to betrue
. - If two boolean values/expressions are combined with a logical and (
&&
) and the first expression isfalse
, then the second expression wonβt be executed. If the first expression isfalse
, the result will befalse
, since both sides of the&&
need to betrue
for the result to betrue
.
- 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"); }
- 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"); }
Venn Diagram Activity
π Explore the following problems in a group of 4:
-
Draw or print a Venn diagram of 4 intersecting circles. Put the names of the 4 people in your group one in each circle. Write down the age of each person in your group in the circles. If two or more people are the same age, put the age in the intersecting parts of their circles. Write a Boolean expression that compares the age of each person in the group using
==
,<
,>
, and&&
, for example Adaβs age>
Alanβs age&&
Alanβs age==
Graceβs age. 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. 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. Think of 1 more comparison and write it in the circles and as a Boolean expression. Share the Boolean expressions with the class. -
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
temperature
and boolean variablessunny
andraining
. 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. -
Complete a truth table for the if statement that you wrote in #2 with columns for sunny, temperature > 80, raining, and go to the beach.
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. See how high a score you can get!
βοΈ Summary
-
Logical operators
!
(not),&&
(and), and||
(or) are used with Boolean values. -
A && B
istrue
if bothA
andB
aretrue
. -
A || B
istrue
if eitherA
orB
(or both) aretrue
. -
!A
istrue
ifA
isfalse
. -
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.
π When class ends, donβt forget to SAVE YOUR WORK!
- Navigate to the
Source Control
menu on the LEFT sidebar - Type a brief commit message in the box, for example:
updated Main.java
- Click the button on the LEFT menu
- Click the button on the LEFT menu
- Finally you can close your Codespace!
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.