Logic operators

In this chapter you will learn:

  1. List of logic operators
  2. Boolean logical AND operator
  3. Boolean logical OR operator
  4. Boolean logical NOT operator
  5. Logical operators with an if statement

List of logic operators

C# has the following conditional operators:

Operator Meaning
& AND
| OR
^XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! NOT

True table,

p Q p & qp | q p ^ q!p
FalseFalse False False False True
TrueFalse FalseTrue True False
False True False True True True
True TrueTrue True False False
using System; //ja v a2 s  . c  o  m
 
class Example {    
  public static void Main() {    
     bool b1, b2; 
 
    b1 = true; 
    b2 = false; 
    if(b1 & b2) 
        Console.WriteLine("(b1 & b2) is true"); 
    if(!(b1 & b2)) 
        Console.WriteLine("!(b1 & b2) is true"); 
    if(b1 | b2) 
        Console.WriteLine("b1 | b2 is true"); 
    if(b1 ^ b2) 
        Console.WriteLine("b1 ^ b2 is true"); 
  }    
}

The following demo shows how to use them:

using System;/*  j  a  v a 2  s .co m*/

class Program
{
    static void Main(string[] args)
    {
        int i = 5;
        int j = 6;
        if (i == j)
        {
            Console.WriteLine("identical");
        }
        else {
            Console.WriteLine("not identical");
        }
    }
}

The output:

false expressions for operators: ==, > and <

class MainClass//from jav  a 2  s  .c  o  m
{
  public static void Main()
  {
    bool result;

    
    result = 10 == 1;
    System.Console.WriteLine("10 == 1 is " + result);
    result = 10 < 1;
    System.Console.WriteLine("10 < 1 is " + result);
    result = 10 <= 1;
    System.Console.WriteLine("10 <= 1 is " + result);

  }

}

The code above generates the following result.

true expressions for operators: !=, >, <

class MainClass// j a v  a 2 s.co m
{
  public static void Main()
  {
    bool result;
    
    result = 10 != 1;
    System.Console.WriteLine("10 != 1 is " + result);
    result = 10 > 1;
    System.Console.WriteLine("10 > 1 is " + result);
    result = 10 >= 1;
    System.Console.WriteLine("10 >= 1 is " + result);
    int intValue1 = 10;
    int intValue2 = 1;
    result = intValue1 != intValue2;
    System.Console.WriteLine("intValue1 != intValue2 is " + result);
  }
}

The code above generates the following result.

Boolean logical AND operator

class MainClass/*from  jav a  2  s . co  m*/
{

  public static void Main()
  {
    bool result;
    
    result = (1 == 1) && (2 > 1);
    System.Console.WriteLine("(1 == 1) && (2 > 1) is " + result);
    result = (1 == 1) && (2 < 1);
    System.Console.WriteLine("(1 == 1) && (2 < 1) is " + result);

  }

}

The code above generates the following result.

Boolean logical OR operator

class MainClass/*from j a  v  a  2s  .  co m*/
{

  public static void Main()
  {
    bool result;
    
    
    result = (1 == 1) || (1 == 0);
    System.Console.WriteLine("(1 == 1) || (1 == 0) is " + result);
    result = (1 == 0) || (1 == 0);
    System.Console.WriteLine("(1 == 0) || (1 == 0) is " + result);
  }

}

The code above generates the following result.

Boolean logical NOT operator

class MainClass// j  a va 2 s.  co  m
{

  public static void Main()
  {
    bool result;
    
    
    result = !(1 == 0);
    System.Console.WriteLine("!(1 == 0) is " + result);
    result = !(1 == 1);
    System.Console.WriteLine("!(1 == 1) is " + result);
  }

}

The code above generates the following result.

Logical operators with an if statement

class MainClass/*  ja v  a2  s . c o  m*/
{

  public static void Main()
  {

    int intValue = 1500;
    string stringValue = "closed";

    if ((intValue > 1000) && (stringValue == "closed"))
    {
      System.Console.WriteLine("and");
    }
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. When to use Logic operator shortcut
  2. && vs &
  3. Side-effects of short-circuit operators
Home » C# Tutorial » 
C# operators
Arithmetic Operators
Assignment Operator
Remainder operator
Increment operator and decrement operators
Logic operators
Logic operator shortcut
Relational operators
sizeof
Ternary operator(The ? Operator)
typeof
Bitwise Operators
bitwise AND
bitwise OR
bitwise NOT
bitwise exclusive OR
Shift Operators
Precedence of the C# Operators