Using Boolean Operators

A Boolean operator is an operator whose result is either true or false. C# has several very useful Boolean operators, the simplest of which is the NOT operator, which is represented by the exclamation point symbol (!). The ! operator negates a Boolean value, yielding the opposite of that value. In the previous example, if the value of the variable areYouReady is true, the value of the expression !areYouReady is false.

Understanding Equality and Relational Operators

Two much more commonly used Boolean operators are the equality (==) and inequality (!=) operators. You use these binary operators to find out whether a value is the same as another value of the same type. The following table summarizes how these operators work, using an int variable called age as an example.

Operator |
Meaning |
Example |
Outcome if age is 42 |

-- |
Equal to |
age -- 100 |
false |

`!=` |
Not equal to |
age != 0 |
true |

Closely related to these two operators are the relational operators. You use these operators to find out whether a value is less than or greater than another value of the same type. The following table shows how to use these operators.

Operator |
Meaning |
Example |
Outcome if age is 42 |

< |
Less than |
age < 21 |
false |

<= |
Less than or equal to |
age <= 18 |
false |

> |
Greater than |
age > 16 |
true |

>= |
Greater than or equal to |
age >= 30 |
true |

Don't confuse the equality operator == with the assignment operator =. Code such as x==y compares x to y and has the value true if the values are the same. Code such as x=y assigns the value of y to x.

Understanding Conditional Logical Operators

C# also provides two other Boolean operators: the logical AND operator, which is represented by the && symbol, and the logical OR operator, which is represented by the || symbol. Collectively, these are known as the conditional logical operators. Their purpose is to combine Boolean expressions together into bigger expressions. These binary operators are similar to the equality and relational operators in that their outcome is either true or false, but they differ in that the values they operate on must themselves be either true or false.

The outcome of the && operator is true if and only if both of the Boolean expressions it operates on are true. For example, the following statement assigns the value true to validPercentage if and only if the value of percent is greater than or equal to zero and the value of percent is less than or equal to 100:

bool validPercentage; validPercentage = (percent >= 0) && (percent <= 100);

A common beginner's error is to try to combine the two tests by naming the percent variable only once, like this:

percent >= 0 && <= 100 // this statement will not compile.

validPercentage = percent >= 0 && percent <= 100 validPercentage = (percent >= 0) && (percent <= 100)

The outcome of the || operator is true if either of the Boolean expressions it operates on is true. You use the || operator to determine whether any one of a combination of Boolean expressions is true. For example, the following statement assigns the value true to invalidPercentage if the value of percent is less than zero, or the value of percent is greater than 100:

bool invalidPercentage; invalidPercentage = (percent < 0) || (percent > 100);

Summarizing Operator Precedence and Associativity

The following table summarizes the precedence and associativity of all the operators you have learned about so far. Operators in the same category have the same precedence. Operators in a higher category take precedence over operators in a lower category.

Category |
Operators |
Description |
Associativity |

Primary |
( ) |
Precedence override |
Left |

Unary |
! |
Logical NOT |
Left |

Multiplicative |
* |
Multiply |
Left |

Additive |
+ |
Addition |
Left |

Relational |
< |
Less than |
Left |

Equality |
== |
Equal to |
Left |

Conditional AND |
&& |
Logical AND |
Left |

Conditional OR |
|| |
Logical OR |
Left |

Assignment |
= |
Right |