evaluate compound Boolean expressions in program code
AND (&&)
What if you want two things to be true before the body of the conditional is executed?
You can use && as a logical AND to join two Boolean expressions and the body of the condition will only be executed if both are true.
For example, the following code will only print "You can go out" if cleanedRoom and didHomework are true:
OR (||)
What if you want code to run if only one of the two conditions 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.
Note: The vertical bar character | can be used by hitting shift + \ on the keyboard.
For example, in the following code your parents will say "You can go out" if you decide to walk OR they don't need the car:
Note: In English, we often use an exclusive-or as 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 both conditions are true.
With numerical values, the or (||) operator is often to 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 a range:
NOT (!)
The not (!) operator can be used to negate a boolean value.
We have seen ! before in the != (not equal) operator.
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.
Note: The not operator (!) will be executed before &&, and && will be executed before ||, unless there are parentheses. Anything inside parentheses will be executed first.
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.
The result of P || Q is true if either P or Q is true. It is also true when they are both true.
SHORT CIRCUIT EVALUATION
Both && and || use short circuit evaluation.
This 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:
For example,
1) 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.
2) 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.
SUMMARY
Logical operators ! (not), && (and), and || (or) are used with Boolean values.
( A && B ) is only 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.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) I challenge you to the Boolean game! Make sure to click "Boolean", and check the boxes for "Timer" AND "Compound" (you can check the box for "Sound" too if you would like). Send a screenshot to coombs_d@milfordschools.org of your high score! The MINIMUM score is 50.