logical operators in c# with example,Logical operators allow you to combine multiple conditions. These operators involve at least two conditions and the final boolean result is determined by the operator being used. Let’s take a look at some logical operators.

Operator Pronounced as Example Effect
&& And z = (x > 2) && (y < 10) z will be assigned a value
of true if both
the conditions are true. If one of the
conditions results to  false, then z will
automatically assigned a value of false.
|| Or z = (x > 2) || (y < 10) z will be assigned a
value of true if at least one of the
conditions is true. If there’s
no condition that results to true, then z
will be assigned a value of false.
! Not z = !(x > 2) z will be assigned a
value of true if the condition
results to false, and false, if
the condition results to true.

For example, you can read z = (x > 2) && (y < 10) as “Assign z with true if the value of x is greater than 2 AND the value of y is less 10, false otherwise”. The sentence suggests that it requires to have all the          conditions to be true in order for the statement to be true. You passed the subject if you passed the final exam AND you got a passing grade. This means you cannot pass the subject if you neither pass the final exam nor have a passing grade or both.

The use of || (logical OR) is a different effect than the && (logical AND). The logical OR operator results to true if at least one of the conditions is true. If none of those conditions is true, then it will result to falsity. You can even combine both logical AND and logical OR in an expression like this:

if ( (x == 1) && ( (y > 3) || z < 10) ) )
{
	//do something here
}

The use of parentheses is important here. We use it to group the conditions. First, it will calculate the result of (y > 3) || (z < 10)because it is considered as a group thanks to the parentheses. The result will then be the right operand of the logical AND and will be compared to (x == 1) condition. The result will determine if the program should execute the code in its body.

We wrote a program on our previous lesson. Let’s make some modifications in it to show how to use a logical operator inside a program.

using System;

namespace LogicalOperatorsDemo
{
    public class Program
    {
        public static void Main()
        {
            int age;
            string gender;

            Console.Write("Enter your age: ");
            age = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter your gender (male/female): ");
            gender = Console.ReadLine();

            if (age > 12 && age < 20)
            {
                if (gender == "male")
                {
                    Console.WriteLine("You are a teenage boy.");
                }
                else
                {
                    Console.WriteLine("You are a teenage girl.");
                }
            }
            else
            {
                Console.WriteLine("You are not a teenager.");
            }
        }
    }
}

Example 1 – Logical Operators

Enter your age: 18
Enter your gender (male/female): female
You are a teenage girl.
Enter you age: 10
Enter your gender (male/female): male
You are not a teenager.

The modified program shows you the logical operator AND in action (line 16). When you reach the if statement, the program determines if your age is between 12 and 20. First, it checks if your age is greater than 12 then it checks if your age didn’t exceed 20. If both of these are true, then it will execute the code inside the if block. If one condition yields  false such as meeting the condition that you are older than 12 but not younger than 20, then it will execute the code inside the else block.

The && operator evaluates the left operand. If  it is false, then it skips evaluating the right  operand and automatically yield false as the  result. In contrast, the || operator  evaluates the left operand and if it is true, then  it skips the evaluation of the right operand and immediately yields true.

One important thing to note is that you can also use the operators & and | which are  the bitwise AND and OR operators.

if (x == 2 & y == 3)
{
   //Some code here
}

if (x == 2 | y == 3)
{
   //Some code here
}

One minor difference of this operator on the logical operators we have  discussed is that they evaluate the two operands regardless of the value of the  left operand. For example, even if the left operand is false, the right operand  is still evaluated by the bitwise AND operator (&).  If you are combining conditions, it would be better to use the logical AND (&&)  and OR (||) operators instead of the bitwise AND (&)  and OR (|) operators.

There’s another logical operator that I would like to show. The NOT Operator (!)  is used to negate or inverse the effect of an expression.Consider the following  example:

if (!(x == 2))
{
	Console.WriteLine("x is not equal to 2.");
}

If the expression x == 2 evaluates to  false, the !  operator reverses the result from false to true.