Skip to main content

Overview

Logical operators are used to combine or invert boolean values. They are essential for creating complex conditions in your programs.

Logical Operators Table

Basic Example

Here’s a complete example demonstrating logical operators:
Output:

AND Operator (&&)

The AND operator returns true only when both conditions are true.

Truth Table for AND

Example

The AND operator uses “short-circuit” evaluation: if the left operand is false, the right operand is not evaluated because the result will always be false.

OR Operator (||)

The OR operator returns true when at least one condition is true.

Truth Table for OR

Example

The OR operator also uses “short-circuit” evaluation: if the left operand is true, the right operand is not evaluated because the result will always be true.

NOT Operator (!)

The NOT operator inverts a boolean value.

Truth Table for NOT

Example

The NOT operator is useful for checking the opposite of a condition. For example, !isEmpty is more readable than isEmpty == false.

Combining Logical Operators

You can combine multiple logical operators to create complex conditions:

Practical Examples

Access Control

Form Validation

Eligibility Check

Inverted Conditions