Programming Java

An Introduction to Computer Science



11. Compound Boolean Expressions

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

Figure 11A – Logical Complement Operator

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!PRelationalNegation
TrueFalse==!=
FalseTrue!===
<>=
><=
<=>
>=<
Table 11A – Negation Table
!true 	        becomes false
!(x == 0)	becomes x != 0
!(x > 0)	becomes x <= 0
!(x >= 0)	becomes x < 0

Logical-And Operator

Figure 11B – Logical-And Operator

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.

PQP && Q
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
Table 11B – P && Q

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.

Figure 11C – Short-Circuit Behavior

Logical-Or Operator

Figure 11D – Logical-Or Operator

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.

PQP || Q
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
Table 11C – P || Q

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.

Figure 11E – Short-Circuit Behavior

Precedence Order

When more than one logical operator is present in a Boolean expression, the operators are evaluated in the following order:

  1. logical complement
  2. logical-And
  3. 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.

Figure 11F – Conditional Operator

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.

Figure 11G – Conditional Op Example

This is output when the program runs.

Do you want to quit? (Y/N)
Your response: N
Welcome back

2022 Copyright


By:


Design a site like this with WordPress.com
Get started