**Relational and Logical Operators**

**Relational operators** are operators that compare quantities. They only return True or False depending upon the context.

- Greater than (>)
- Greater than or Equal to (>=)
- Less than (<)
- Less than or Equal to (<=)
- Equal to (==)
- Not Equal to (!=)

Examples of relational statements:

- 5 > 2 (True)
- 3 != 3 (False)

**Boolean Operators** are operators that define the relationship between relational statements and allow multiple relational statements to be joined.

- NOT (!) – Performs the negation of a statement (True changed to false, false changes to true)
- AND (&&) – Performs the conjunction of two statements.
- OR (||) – Performs the disjunction of two statements.

These operators can almost be used as you would in plain english.

- If you are NOT ready, then you can NOT go.
- If I have at least $10 AND you have at least $8, then we can go to the movie

Examples:

- !(5 < 2) (True) – 5 is not less than 2, so the inside returns False, however the negation operator switches that to True.
- 9 > 4 && 6 <= 3 (False) – 9 is greater than 4, but 6 is not less than or equal to 3, thus entire statement is False since the && operator requires both operands to be true.
- 3 == 2 || 4 > -44 (True) – While 3 is not equal to 2, 4 is greater than -44, thus the entire statement is True since the || requries only one operand to be true.

These operators can be chained together to form rather large and complex statements. Operator precedence applies here. Parenthesis are evaluated first. Equal and Not Equal second, then AND and OR.

- !(5 4 && 6 <= 3) && !(3 == 2 || 4 < -44) – This is a TRUE statement

Explaination

- !(5 < 2) is TRUE
- (9 > 4 && 6 <= 3) is FALSE
- !(3 == 2 || 4 < -44) is TRUE
- The statement can be reduced to (TRUE || FALSE && TRUE)
- Because the && operation gets performed first, we then end up with (TRUE || FALSE), which is TRUE.

Boolean and other data types can also be used in these logical statements in Java

Example:

boolean isReady = True;

int temperature = 85;

(isReady && temperature < 90) – This statement returns TRUE since isReady is True AND temperature is less than 90.