By 22/07/2015 7 Comments

C# Logical Operators

  • Logical operators compare two or more conditions.
  • More than one value is compared with each other by relational or comparison operators are called a condition (Conditions are described above in relational operators section with great detail).
  • Logical operators are very useful when we want to compare more than one condition.
  • Logical operators must be applied on bool type operands or variables.
  • They produce result in boolean which is true or false.


Suppose:

int v1 = 1, v2 = 1;

OPERATOR DESCRIPTION
& Logical AND, compare two conditions such as (v1 == 10) & (v2 == v1) these are two conditions of type bool one is (v1 == 10) and second is (v2 == v1) these two conditions are also called a statement or expressions. NOTICE the result of these conditions is produced in a bool data type which is true or false. In this example, one condition is compared with second condition by using (&) Logical AND operator, here the result of condition by using (&) Logical AND operator, here the result of these two conditions is FALSE because a logical AND (&) operator is being used here. NOTE: logical AND (&) operator must needs each and every condition true for give us result true, otherwise false. Consider above conditions (v1 == 10) & (v2 == v1) this gives us FALSE result because first condition (v1 == 10) is a false condition because in above v1 = 1 at the other hand (v2 == v1) is true because v1 = 1 and also v2 = 1. So one condition is false and second is true, when we compare these two conditions using logical (AND) it gives us false because it needs both must be true. REMEMBER this operator is called normal logical AND (&), because In an AND operation, if the first condition is false, then the outcome is false and it shall not evaluate the second condition.
&& Short-circuit AND, this operator working same as above AND (&) but REMEMBER: short-circuit AND (&&) versions will evaluate both conditions only when necessary.
| Logical OR, compare two conditions such as (v1 == 10) | (v2 == v1) these are two conditions of type bool one is (v1 == 10) and second is (v2 == v1) these two conditions are also called a statement or expressions. NOTICE the result of these conditions is produced in a bool data type which is true or false. In this example, one condition is compared with second condition by using (|) Logical OR operator, here the result of these two conditions is TRUE because a logical OR (|) operator is being used here. NOTE: logical OR (|) operator needs any one condition to be true for give us result true, otherwise false. Consider above conditions (v1 == 10) | (v2 == v1) this gives us TRUE result because first condition (v1 == 10) is a false condition because in above v1 = 1 at the other hand (v2 == v1) is true because v1 = 1 and also v2 = 1. So one condition is false and second is true, when we compare these two conditions using logical (OR) it gives us true because it needs only one condition to be true.
|| Short-circuit OR, this operator working same as above OR (|) but REMEMBER: short-circuit OR (||) versions will evaluate both conditions only when necessary.
! Logical NOT, that operator uses the reverse process on one or multiple conditions’ state, for instance if a condition is false then logical NOT (!) operator makes it true and vice versa. e.g. according to above suppose values: !((v1 == 1) || (v2 == 1)) = false, (v1 ==1) || (v2 == 1) was true but when we use NOT (!) then these conditions become false.

Example:

using System;
namespace csharpBasic
{
  // Start class definition / declaration.
    class Program
    {
      // Static main method void type declaration.
        static void Main(string[] args)
        {
            // Declare and initialize v1 and v2 variables, assign 1,1 values to them.
            int v1 = 1, v2 = 1;

            // Declare a bool type result.
            bool result;

            // Perform AND operation assign result to a variable result.
            result = (v1 != 1) & (v2 == 1);

            // Print the result variable.
            Console.WriteLine("Logical AND (&): {0} ", result);

            // Perform short-circuit AND operation.
            result = (v1 == 1) && (v2 != 1);

            Console.WriteLine("Logical Short-circuit AND (&&): {0} ", result);

            // Perform OR operation.
            result = (v1 == 1) | (v2 == 10); 

            Console.WriteLine("Logical OR (|): {0} ", result);

            // Perform short-circuit OR operation.
            result = (v2 == 10) || (v2 == 1); 
            
            Console.WriteLine("Logical Short-circuit OR (||): {0} ", result);

            // Perform logical NOT operation.
            result = !((v2 == 10) || (v2 == 1));

            Console.WriteLine("Logical NOT (!): {0} ", result);
            Console.ReadKey();

        } // End of main method definition.

    } // End of class.
}

Remember:

  • Logical NOT (!) operator can be used with all logical operators such as logical OR (||) and logical AND (&&).





Next Tutorial →



Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .