C# Logical operators



Conditional AND (&&) Operator

Suppose that we wish to ensure at some point in an app that two conditions are both true before we choose a certain path of execution. In this case, we can use the && (conditional AND) operator.

The following table shows all four possible combinations of false and true values when using the && operator.

expression1 expression2 expression1 && expression2
false false false
false true false
true false false
true true true

The following code example shows how to use the && operator:


    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;   
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Please enter a number:");
                int score = Convert.ToInt32(Console.ReadLine());
                if (score > 85 && score < 100)
                {
                    Console.WriteLine("You are ready to pass the EXAM!!!");
                }
                else
                {
                    Console.WriteLine("You need more study!!");
                }

                Console.ReadLine();
            }
        }
    }

    Program output:
    91
    You are ready to pass the EXAM!!!

Conditional OR ( || ) Operator

Suppose we wish to ensure that either or both of two conditions are true before we choose a certain path of execution. In this case, we use the || (conditional OR) operator.

The following table shows all four possible combinations of false and true values when using the || (conditional OR) operator.

expression1 expression2 expression1 && expression2
false false false
false true true
true false true
true true true

The following code example shows how to use the || (conditional OR) operator:


    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;   
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                /* using the || (conditional OR) operator*/
                Console.WriteLine("Please Score Semester Average:");
                int scoresemesterAverage = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Please enter Final Exam Score:");
                int finalExam = Convert.ToInt32(Console.ReadLine());

                    if ((scoresemesterAverage >= 90) || (finalExam >= 90))
                        Console.WriteLine("Student grade is A");
                    else
                        Console.WriteLine("Student grade is not A");

                Console.ReadLine();
            }
        }
    }

    Output:
    Please Score Semester Average: 80
    Please enter Final Exam Score: 90
    Student grade is A

Boolean Logical Exclusive OR (^) Operator

A complex condition containing the boolean logical exclusive OR (^) operator (also called the logical XOR operator) is true if and only if one of its operands is true and the other is false.

If both operands are true or both are false, the entire condition is false.

The following table shows all possible combinations of false and true values when using the boolean logical exclusive (^) OR operator .

expression1 expression2 expression1 && expression2
false false false
false true true
true false true
true true false

Logical Negation (!) Operator

The ! (logical negation or not) operator enables you to “reverse” the meaning of a condition.

Unlike the logical operators &&, ||, &, | and ^, which are binary operators that combine two conditions, the logical negation operator is a unary operator that has only a single condition as an operand.

The following table shows all possible combinations of false and true values when using the Logical Negation (!) operator .

expression ! expression
true false
false true

The logical negation operator is placed before a condition to choose a path of execution if the original condition (without the logical negation operator) is false.

The following code example shows how to use the Logical Negation (!) operator:


    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;   
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                /* using the Logical Negation (!) operator*/
                Console.WriteLine("Please Score Semester Average:");
                int scoresemesterAverage = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Please enter Final Exam Score:");
                int finalExam = Convert.ToInt32(Console.ReadLine());

                    if ((scoresemesterAverage != 90) && (finalExam > 90))
                        Console.WriteLine("Student grade is A");
                    else
                        Console.WriteLine("Student grade is not A");
                Console.ReadLine();
            }
        }
    }

    Output:
    Please Score Semester Average: 70
    Please enter Final Exam Score: 95
    Student grade is A

Ads Right