Logical Operators
Logical operators typically involve boolean values and yields boolean results. There are often use to create multiple or complex conditions. We have learned that a boolean value can either be true or false. We can use the different logical operators available in C#. Suppose that var2 and var3 are boolean values.
Operator | Name | Category | Example |
---|---|---|---|
&& | Logical AND | Binary | var1 = var2 && var3; |
|| | Logical OR | Binary | var1 = var2 || var3; |
! | Logical NOT | Unary | var1 = !var1; |
Logical AND Operator (&&)
An expression using the logical AND operator evaluates to true if both operands are true. If either or both of the operands are false, the expression evaluates to false. The following shows the truth table for the logical AND operator.
X | Y | X && Y |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
To better understand the effect of logical AND, remember that it can only yields true if both operands are true. All other combinations yield false. When using logical AND operator, the moment that the program sees that the first operand is false, then the result is automatically false.
You can use the logical AND operator on boolean expressions such as those using comparison operators. For example, the following expression assigns true to the result if age is greater than 18 and salary are less than 1000.
result = (age > 18) && (salary < 1000);
The logical AND operator is also useful when specifying a range of values. For example, the mathematical notation 10 <= x <= 100means the x can have a value of range 10 to 100. To test if a number falls at a certain range, we can simply use the logical AND operator.
inRange = (number <= 10) && (number >= 100);
You will see more useful examples of the logical AND in a later lesson.
Logical OR Operator (||)
The logical OR operator evaluates to true if either or both of the operators are true. It is the opposite of logical AND which evaluates to false if either or both of the operands are false. The following shows the truth table for the logical OR operator.
X | Y | X || Y |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
As you can see, an expression with a logical OR operator only evaluates to false if both of the operands are false. As an example, the following code assigns true to the result if the student’s final grade is greater than or equal to 75 or his final exam’s grade is equal to 100.
isPassed = (finalGrade >= 75) || (finalExam == 100);
We will see some practical examples of using the logical OR operator in later lessons.
Logical NOT Operator (!)
Unlike the other two logical operators, the logical NOT operator is a unary operator and only requires one operand. The logical NOT operator negates a boolean expression or value. If the expression or value is true, it is turned to false, and if false, it is turned to true. The following shows the truth table for the logical NOT operator.
X | !X |
---|---|
true | false |
false | true |
This code assigns true to result if age is not greater than or equal 18, or in other words if age is less than 18.
isMinor = !(age >= 18);
We will later see a more practical use of the ! operator in a later lesson.