Real-world scenarios require decision-making under multiple conditions. The logical operators are used to create complex conditions called “compound Boolean Expressions.”
Logical Complement Operator
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/c10-2.png?w=600)
The logical complement operator has one operand which is either a Boolean value or an expression that evaluates to a Boolean value.
The logical complement operator negates the Boolean value represented by its operand. The logical complement operator is also called the “logical-Not operator”.
Given
boolean found = true;
int x = 1;
int y = 2;
!found // not true = false
!(x > y) // not false = true
!x // error: x is not boolean type
The logical complement operator also negates the relational operators.
P | !P | Relational | Negation |
---|---|---|---|
True | False | == | != |
False | True | != | == |
< | >= | ||
> | <= | ||
<= | > | ||
>= | < |
!true becomes false
!(x == 0) becomes x != 0
!(x > 0) becomes x <= 0
!(x >= 0) becomes x < 0
Logical-And Operator
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/c10-3.png?w=600)
Each operand must be a Boolean value or an expression that evaluates to a Boolean value.
The logical-And operator evaluates two operands and resolves to a Boolean value.
Given
boolean found = true;
int x = 1;
int y = 2;
found && (x < y) // valid
found && x // error: x is not boolean type
The logical-And operator resolves to true
, if and only if both operands evaluate to true
. Otherwise, it resolves to false
.
P | Q | P && Q |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Therefore, the logical-And operator evaluates the right-hand operand only if the left-hand operand evaluates to true
. This is called short-circuiting behavior.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/c10-4.png?w=300)
Logical-Or Operator
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/c10-5.png?w=600)
Each operand must be a Boolean value or an expression that evaluates to a Boolean value.
The logical-Or operator evaluates two operands and resolves to a Boolean value.
Given
boolean found = true;
int x = 1;
int y = 2;
found || (x < y) // valid
found || x // error: x is not boolean type
The logical-OR operator resolves to false
, if and only if both operands evaluate to false
. Otherwise, it resolves to true
.
P | Q | P || Q |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Therefore, the logical-Or operator evaluates the right-hand operand only if the left-hand operand evaluates to false
. This is the short-circuiting behavior.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/c10-6.png?w=300)
Precedence Order
When more than one logical operator is present in a Boolean expression, the operators are evaluated in the following order:
- logical complement
- logical-And
- logical-Or
Example
Here is a compound Boolean expression.
B || !(A || !B) && (!C && A)
Assume A, B, and C evaluate to true
. Substitute the truth values.
true || !(true || !true) && (!true && true)
The expressions inside the parentheses are evaluated first.
First, apply the logical complement operator in both parentheses.
Next, evaluate the left parenthesis and then the right parenthesis
true || !(true || false) && (false && true)
true || !(true) && (false && true)
true || !true && false
Once all the parentheses are eliminated, evaluate the simplified expression.
First, apply the logical complement operator.
Next, apply the logical-And operator followed by the logical-Or operator.
true || false && false
true || false
true
Conditional Operator ?:
The conditional operator uses the Boolean value of a condition to decide which of the two following expressions to evaluate.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/c10-7.png?w=600)
The conditional_expression must evaluate to a Boolean value.
Expression_1 is evaluated if conditional_expression is true
.
Expression_2 is evaluated if conditional_expression is false
.
The result of the expression that gets evaluated becomes the result of the conditional operator expression. Therefore, each expression must result in a value.
System.out.println("Do you want to quit? (Y/N)");
String playerResponse = "N";
System.out.println("Your response: " + playerResponse);
String message = (playerResponse == "Y") ? "Good bye" : "Welcome back";
System.out.println(message);
Line 3 – The variable playerResponse
is String data type which represents text values. The variable is assigned the value "N"
.
Line 6 – The variable message
is also String data type. It is assigned the value that results from the conditional operator expression.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/c10-8.png?w=600)
This is output when the program runs.
Do you want to quit? (Y/N)
Your response: N
Welcome back
2022 Copyright