Boolean Operators

To allow programs to make decisions, we must use the combination of Boolean operators and if statements (Future Lesson).

These operators are binary operators that require a LEFT OPERAND and a RIGHT OPERAND. The operator will produce a Boolean result of true or false.

Comparison Operators

These operators compare the left and the right operand and return true if the condition is satisfied.

let x be a variable and y be a variable that contains comparable values

Operation
Description

x == y

Checks if the values of two operands are equal, if YES: returns true.

x != y

Checks if the values of two operands are not equal to each other, if YES: returns true.

x > y

Checks if the LEFT operand is greater than the RIGHT operand, if YES: returns true.

x < y

Checks if the LEFT operand is less than the RIGHT operand, if YES: returns true.

x >= y

Checks if the LEFT operand is greater than OR equal to the RIGHT operand, if YES: returns true.

x <= y

Checks if the LEFT operand is less than OR equal to the RIGHT operand, if YES: returns true.

All the operation above return false if the condition is not met.

Logical Operators

Logical Operators are used to combine boolean expressions.

Logical AND Operator: &&

In Java, multiple boolean expressions can be combined by the && operator.

This operator returns true if both sides of the operands evaluate to true. Otherwise it will be false.

Example Use of the AND operator:

If the train is late to the station AND there are no cabs at the station, I am late to work.

Logical OR Operator: ||

In Java, multiple boolean expressions can be combined by the || operator.

This operator returns true if both sides OR either sides of the operands evaluate to true. It only returns false if both sides of the operands evaluated to false.

Example Use of the OR operator:

I only snack if I am hungry OR if I am bored.

Logical NOT Operator: !

In Java, ! is used to reverse a boolean expression's boolean result.

Operation
Result

!(true)

false

!(false)

true

Example Use of the NOT operator:

If it is NOT a weekday, I don't go to school

Code Examples on Java

SOURCES

Last updated