Friday 17 August 2018

C#: Logical Operators

C# provides three logical operators, These are &&(AND), || (OR), and !(NOT)

Logical AND is also called Conditional AND

Logical OR is also called Conditional OR

Logical AND, OR are called Short circuit operators, will see why these are called short circuit soon.

Logical AND Operator
Operand1
Operand2
Evaluates To
TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
TRUE
FALSE
FALSE
FALSE
FALSE

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        bool operand1 = true;
        bool operand2 = true;

        Console.WriteLine("operand1 = {0}", operand1);
        Console.WriteLine("operand2 = {0}", operand2);
        Console.WriteLine("operand1 && operand2 = {0}", (operand1 && operand2));

        operand1 = true;
        operand2 = false;
        Console.WriteLine("\noperand1 = {0}", operand1);
        Console.WriteLine("operand2 = {0}", operand2);
        Console.WriteLine("operand1 && operand2 = {0}",(operand1 && operand2));

        operand1 = false;
        operand2 = true;
        Console.WriteLine("\noperand1 = {0}", operand1);
        Console.WriteLine("operand2 = {0}", operand2);
        Console.WriteLine("operand1 && operand2 = {0}", (operand1 && operand2));

        operand1 = false;
        operand2 = false;
        Console.WriteLine("\noperand1 = {0}", operand1);
        Console.WriteLine("operand2 = {0}", operand2);
        Console.WriteLine("operand1 && operand2 = {0}", (operand1 && operand2));
    }
}

Output
operand1 = True
operand2 = True
operand1 && operand2 = True

operand1 = True
operand2 = False
operand1 && operand2 = False

operand1 = False
operand2 = True
operand1 && operand2 = False

operand1 = False
operand2 = False
operand1 && operand2 = False






Why Logical AND is called short circuit operator?
Since if the first statement in the expression evaluates to false, then java won't evaluate the entire expression. So Logical AND is called short circuit AND.


Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int a = 10, b = 21;

        if ((a > b) && (a++ > b))
        {
            Console.WriteLine("This statement not evaluated");
        }
        Console.WriteLine("a is not incremented " + a);

        if ((a < b) && (a++ < b))
        {
            Console.WriteLine("This statement is evaluated");
        }
        Console.WriteLine("a is incremented " + a);
    }
}


Output
a is not incremented 10
This statement is evaluated
a is incremented 11

Observation
In the expression (a>b) && (a++ > b), a>b is false, so && operator won't evaluate second statement in the expression, so a is not incremented.

Logical OR Operator
Operand1
Operand2
Evaluates To
TRUE
TRUE
TRUE
TRUE
FALSE
TRUE
FALSE
TRUE
TRUE
FALSE
FALSE
FALSE

As shown in the above table, || operator returns true if any of the operand evaluates to true, otherwise returns false.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        bool operand1 = true;
        bool operand2 = true;

        Console.WriteLine("operand1 = {0}", operand1);
        Console.WriteLine("operand2 = {0}", operand2);
        Console.WriteLine("operand1 || operand2 = {0}", (operand1 || operand2));

        operand1 = true;
        operand2 = false;
        Console.WriteLine("\noperand1 = {0}", operand1);
        Console.WriteLine("operand2 = {0}", operand2);
        Console.WriteLine("operand1 || operand2 = {0}", (operand1 || operand2));

        operand1 = false;
        operand2 = true;
        Console.WriteLine("\noperand1 = {0}", operand1);
        Console.WriteLine("operand2 = {0}", operand2);
        Console.WriteLine("operand1 || operand2 = {0}", (operand1 || operand2));

        operand1 = false;
        operand2 = false;
        Console.WriteLine("\noperand1 = {0}", operand1);
        Console.WriteLine("operand2 = {0}", operand2);
        Console.WriteLine("operand1 || operand2 = {0}", (operand1 || operand2));

    }
}


Output
operand1 = True
operand2 = True
operand1 || operand2 = True

operand1 = True
operand2 = False
operand1 || operand2 = True

operand1 = False
operand2 = True
operand1 || operand2 = True

operand1 = False
operand2 = False
operand1 || operand2 = False

Why Logical OR is called short circuit operator?
Since if the first statement evaluates to true, then java won't evaluates the entire expression. So Logial OR is called short circuit OR.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int a = 10, b = 21;

        if ((a < b) || (a++ < b))
        {
            Console.WriteLine("This statement evaluated");
        }
        Console.WriteLine("a is not incremented " + a);

        if ((a > b) || (a++ > b))
        {
            Console.WriteLine("This statement is evaluated");
        }
        Console.WriteLine("a is incremented " + a);

    }
}


Output
This statement evaluated
a is not incremented 10
a is incremented 11

Logical (!)NOT operator
Operand
Evaluates To
FALSE
TRUE
TRUE
FALSE
        
If the operand is FALSE, ! Operator evaluates it to TRUE
If the operand is TRUE, ! Operator evaluates it to FALSE.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        bool a = true;
        Console.WriteLine("a = {0}, !a = {1}", a, !a);

        a = false;
        Console.WriteLine("a = {0}, !a = {1}", a, !a);
    }
}

Output
operand1 = True
operand2 = True
operand1 || operand2 = True

operand1 = True
operand2 = False
operand1 || operand2 = True

operand1 = False
operand2 = True
operand1 || operand2 = True

operand1 = False
operand2 = False
operand1 || operand2 = False




Previous                                                 Next                                                 Home

No comments:

Post a Comment