Skip to content

Logical operators

A logical operator is an operator that combines multiple boolean values (tests that evaluate to true or false) into a single boolean value.

The key thing is that they work with boolean values (true or false) and produce another boolean value as a result.

We can logically think of this as:

  • If true AND true the the result is true
  • If true OR false the the result is false
  • If NOT true the result is false
  • etc.
OperatorNameReturnsExampleResult
&&Logical ANDtrue if both are truetrue && falsefalse
||Logical ORtrue if at least one is truetrue || falsetrue
!Logical NOTopposite of the value!truefalse

The logical AND operator returns:

  • true if both values are true
  • false if at least one value is false

We can write this as a truth table:

ABA && B
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

An example of using the logical AND operator in C# is:

bool isLoggedIn = true;
bool isAdmin = false;
bool canAccess = isLoggedIn && isAdmin; // false

Or, for an example which returns true:

int age = 20;
bool hasLicence = true;
bool hasCar = true;
bool isOldEnough = age >= 17;
bool canDrive = hasLicence && hasCar && isOldEnough; // true

This also shows an example of chainingg multiple logical operators together.

The logical OR operator returns:

  • true if at least one value is true
  • false if both values are false

We can write this as a truth table:

ABA || B
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

In other words, the only time it returns false is when both values are false.

An example of using the logical OR operator in C#:

bool isWeekend = false;
bool isHoliday = true;
bool canRelax = isWeekend || isHoliday; // true

The logical NOT operator returns the opposite of the boolean value.

  • If the value is true, it returns false
  • If the value is false, it returns true

We can write this as a truth table:

A!A
truefalse
falsetrue

In other words, it inverts the boolean value.

Here’s an example:

bool isRaining = true;
bool goodTimeForAPicnic = !isRaining; // false

Or, another example:

bool isWeekend = false;
bool isWeekday = !isWeekend; // true

Logical operators can be combined to create more complex boolean expressions.

For example:

bool isWeekend = true;
bool isHoliday = false;
bool isSunny = true;
bool canGoToBeach = (isWeekend || isHoliday) && isSunny; // true

Or, using the logical NOT operator:

bool isRaining = false;
bool isWindy = true;
bool isBadWeather = !(isRaining || isWindy);

In the last example, you might wonder why the parentheses were necessary.

That’s because the logical NOT operator (!) has a higher precedence than the logical AND (&&) and logical OR (||) operators, and so, without the brackets, we would just have been negating the value of isRaining only.

Here’s the order of precedence for logical operators (from highest to lowest):

  • Logical NOT (!)
  • Logical AND (&&)
  • Logical OR (||)

The ones higher on the list will be evaluated first.