Logic operators
In this chapter you will learn:
- List of logic operators
- Boolean logical AND operator
- Boolean logical OR operator
- Boolean logical NOT operator
- 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 & q | p | q | p ^ q | !p |
---|---|---|---|---|---|
False | False | False | False | False | True |
True | False | False | True | True | False |
False | True | False | True | True | True |
True | True | True | 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:
Home » C# Tutorial »