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? 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.
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.
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.
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.
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.
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.
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 (&&).
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.
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".
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.
Subsection2.5.4Coding Challenge : Truth Tables POGIL
We encourage you to do this activity as a POGILβ1β
https://pogil.org/about-pogil/what-is-pogil
(Process Oriented Guided Inquiry Learning) group activity. POGIL groups are self-managed teams of up to 4 students where everyone has a POGIL roleβ2β
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. (Thank you to Jill Westerlund of Hoover High School and Art Lopez of Sweetwater High School for this activity suggestion).
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 variables sunny and raining. 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.
Write Java code below to test your if statement and try all the values in your truth table to see if you filled it out correctly. You will need test case for each of the 8 rows in your truth table, for example when sunny is true and false, when raining is true or false, and for a value of temperature greater than 80, for example 90, and less than 80, for example 60.
(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.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.)
int x = 10;
int y = 5;
if (x % 2 == 0 && y % 2 == 0 || x > y)
{
System.out.print("First ");
if (y * 2 == x || y > 5 && x <= 10)
{
System.out.print("Second ");
}
else
{
System.out.print("Third ");
}
}
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.