< Prev
Next >

# C# Logical Operators

The C# language gives us some logical operators which are categorized into two parts -
• Short-circuit logical operator (&&, ||)
• Non-short-circuit logical operator(&,|, !)

## Short-circuit logical operators

• && (AND operator)
• ||(OR operator)

## && operator

This operator is also known as AND operator because its functioning corresponds to the AND gate.
``<boolean-expression> && <boolean-expression>``

## Rules of && operator

• If the result of both boolean expression around && operator is true, only then && operator results in true boolean value.
• If either of the result of both boolean expression is false, && operator results in false boolean value.

## Why && is named short-circuit logical operator?

&& operator first evaluates the boolean expression on its left and then on its right. Both boolean expression should return true for && operator to return true. Hence, if the result of left boolean expression of && is false then && operator doesn't waste time in evaluating the boolean expression on its right, making it a short-circuit logical operator.

## Example with && operator

``````//C# Example of Logical operators

using System;

class A
{
public static void Main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;

//Printing the values
Console.WriteLine("a = "+ a);
Console.WriteLine("b = "+ b);
Console.WriteLine("ch1 = "+ ch1);
Console.WriteLine("ch2 = "+ ch2);
Console.WriteLine("d = "+ d);

if(a<15 && a>10)	//a is less than 15 and a is greater than 10
Console.WriteLine("a<15 && a>10, is true");

if(b<25 && b>19)	//b is less than 25 and a is greater than 19
Console.WriteLine("b<25 && b>19, is true");

if(ch1=='a' && ch2=='b') 	//ch1 has 'a' and ch2 has 'b'
Console.WriteLine("ch1=='a' && ch1=='b', is true");

if(d>2 && ch2=='b')	//d is greater than 2 and value in ch2 has 'b', is true
Console.WriteLine("d>2 && ch2==b, is true");

if(a<=10 && d<=2.5)	//a is less than or equal to 10 and d is less than or equal to 2.5
Console.WriteLine("a<=10 && d<=2.5, is true");

if(a<10 && ch1=='a')	//a is less than 10 and ch1 has 'a'
Console.WriteLine("a<=10 and ch =='a', is true");
}
}``````

## Output is

``````a = 10
b = 20
ch1 = a
ch2 = b
d = 2.5
b<25 && b>19, is true
ch1=='a' && ch1=='b', is true
d>2 && ch2==b, is true
a<=10 && d<=2.5, is true``````

## || operator

This operator is also known as or operator because its functioning corresponds to the OR gate.
``<boolean-expression> || <boolean-expression>``

## Rules of || operator

• If either of the result of boolean expression is true, || operator will still result in a true boolean value.
• If result of both boolean expression is true, || operator results in true boolean value.
• If result of both boolean expression is false, only then || operator results in false boolean value.

## Why || is named short-circuit logical operator?

|| operator first evaluates the boolean expression on its left and then evaluates the one on its right. Either of the boolean expression may return true for && operator to return true. Hence, if the result of left boolean expression of || is true then || operator doesn't waste time in evaluating the boolean expression on its right.

## Example with || operator

``````//C# Example of || operator

using System;

class A
{
public static void Main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;

//Printing the values
Console.WriteLine("a = "+ a);
Console.WriteLine("b = "+ b);
Console.WriteLine("ch1 = "+ ch1);
Console.WriteLine("ch2 = "+ ch2);
Console.WriteLine("d = "+ d);

if(a<15 || a>10)	//a is less than 15 or a is greater than 10
Console.WriteLine("a<15 || a>10, is true");

if(ch1=='a' || ch2=='b') //ch1 has 'a' or ch2 has 'c'
Console.WriteLine("ch1=='a' || ch1=='b', is true");

if(b<25 || b>19)	//b is less than 25 or a is greater than 19
Console.WriteLine("b<25 || b>19, is true");

if(d>2 || ch2=='b')	//d is greater than 2 or value in ch2 has 'b', is true
Console.WriteLine("d>2 || ch2==b, is true");

if(a<=10 || d<=2.5)	//a is less than or equal to 10 or d is less than or equal to 2.5
Console.WriteLine("a<=10 || d<=2.5, is true");

if(a<10 || ch1=='a')	//a is less than 10 or ch1 has 'a'
Console.WriteLine("a<10 || ch1=='a', is true");
}
}``````

## Output is

``````a = 10
b = 20
ch1 = a
ch2 = b
d = 2.5
a<15 || a>10, is true
ch1=='a' || ch1=='b', is true
b<25 || b>19, is true
d>2 || ch2==b, is true
a<=10 || d<=2.5, is true
a<10 || ch1=='a', is true``````

## Non short-circuit logical operators are -

• &(AND operator)
• |(OR operator)
• !(NOT operator)

## & operator

The functioning of & operator also corresponds to the AND gate.
``<boolean-expression> && <boolean-expression>``

## Rules of & operator

• If result of both boolean expression is true, only then & operator results in true boolean value.
• If either of the result of both boolean expression is false, && operator results in false boolean value.

## Why & is named non-short-circuit logical operator?

& operator first evaluates the boolean expression on its left and then evaluates the one its right. Both boolean expression should return true for & operator to return true. But, even if the result of left boolean expression of & is false, & operator still unnecessarily continues to evaluate the boolean expression on its right, making it a non-short-circuit logical operator.

## Example with & operator

``````//C# Example of non-shortcut & operator

using System;

class A
{
public static void Main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;

//Printing the values
Console.WriteLine("a = "+ a);
Console.WriteLine("b = "+ b);
Console.WriteLine("ch1 = "+ ch1);
Console.WriteLine("ch2 = "+ ch2);
Console.WriteLine("d = "+ d);

if(a<15 & a>10)	//a is less than 15 and a is greater than 10
Console.WriteLine("a<15 & a>10, is true");

if(b<25 & b>19)	//b is less than 25 and a is greater than 19
Console.WriteLine("b<25 & b>19, is true");

if(ch1=='a' & ch2=='b') 	//ch1 has 'a' and ch2 has 'c'
Console.WriteLine("ch1=='a' & ch1=='b', is true");

if(d>2 & ch2=='b')	//d is greater than 2 and value in ch2 has 'b', is true
Console.WriteLine("d>2 & ch2=='b', is true");

if(a<=10 & d>=2.5)	//a is less than or equal to 10 and d is greater than or equal to 2.5
Console.WriteLine("a<=10 & d>=2.5, is true");

if(a<10 & ch1=='a')	//a is less than 10 and ch1 has 'a'
Console.WriteLine("a<10 & ch1=='a', is true");
}
}``````

## Output is

``````a = 10
b = 20
ch1 = a
ch2 = b
d = 2.5
b<5 & b>19, is true
ch1=='a' & ch1=='b', is true
d>2 & ch2=='b', is true
a<=10 & d>=2.5, is true``````
Note: There should never be any space between > and = of >= operator, or a compile error is thrown.

## | operator

The functioning of | operator also corresponds to the OR gate.
``<boolean-expression> | <boolean-expression>``

## Rules of | operator

• If either of the result of boolean expression is true while the other boolean expression is false, | operator will still result in a true boolean value.
• If result of both boolean expression is true, then | operator results in true boolean value.
• If result of both boolean expression is false, then | operator results in false boolean value.

## Why | is named non-short-circuit logical operator?

| operator first evaluates the boolean expression on its left and then evaluates the one its right. Both of either boolean expression may return true for | operator to return true. Hence, even if the result of left boolean expression of & is true, | operator still unnecessarily continues to evaluate the boolean expression on its right, making it a non-short-circuit logical operator.

## Example with | operator

``````//C# Example of non-shortcut | operator

using System;

class A
{
public static void Main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;

//Printing the values
Console.WriteLine("a = "+ a);
Console.WriteLine("b = "+ b);
Console.WriteLine("ch1 = "+ ch1);
Console.WriteLine("ch2 = "+ ch2);
Console.WriteLine("d = "+ d);

if(a<15 | a>10)	//a is less than 15 and a is greater than 10
Console.WriteLine("a<15 | a>10, is true");

if(b<25 | b>19)	//b is less than 25 and a is greater than 19
Console.WriteLine("b<25 | b>19, is true");

if(ch1=='a' | ch2=='b') 	//ch1 has 'a' and ch2 has 'b'
Console.WriteLine("ch1=='a' | ch1=='b, is true");

if(d>2 | ch2=='b')	//d is greater than 2 and value in ch2 has 'b', is true
Console.WriteLine("d>2 | ch2=='b', is true");

if(a<=10 | d>=2.5)	//a is less than or equal to 10 and d is greater than or equal to 2.5
Console.WriteLine("a<=10 | d<=2.5, is true");

if(a<10 | ch1=='a')	//a is less than 10 and ch1 is equal to 'a'
Console.WriteLine("a<10 | ch1=='a', is true");
}
}``````
Output -
``````a = 10
b = 20
ch1 = a
ch2 = b
d = 2.5
a<15 | a>10, is true
b<25 | b>19, is true
ch1=='a' | ch1=='b, is true
d>2 | ch2=='b', is true
a<=10 | d<=2.5, is true
a<10 | ch1=='a', is true``````
Note: There should never be any space between = and = of == operator, or a compile error is thrown.

## ! operator

The functioning of ! operator corresponds to the NOT gate. It inverts the value of a boolean expression.
``!<boolean-expression>``

## Rules of ! operator

• ! operator applied to a boolean expression that resulted in true, will invert its boolean value to false.
• ! operator applied to a boolean expression that resulted in false, will invert its boolean value to true.

## Example with ! (NOT operator)

``````//C# Example of ! operator

using System;

class A
{
public static void Main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
bool b1 = true, b2 = false;

//Printing the values
Console.WriteLine("a = "+ a);
Console.WriteLine("b = "+ b);
Console.WriteLine("ch1 = "+ ch1);
Console.WriteLine("ch2 = "+ ch2);
Console.WriteLine("d = "+ d);
Console.WriteLine("b1 = "+ b1);
Console.WriteLine("b2 = "+ b2);

if(!(10>15))
Console.WriteLine("!(10>15), is true ");

if( !(b<25 || b>19) )	//Invert of (b is less than 25 and a is greater than 19)
Console.WriteLine("!(b<25 | b>19), is false");

if( !(ch1=='a' && ch2=='b') ) 	//Inverting the boolean value of (ch1=='a' && ch2=='b')
Console.WriteLine("!(ch1=='a' && ch2=='b'), is false");

if(d>2  &  !(ch2=='b'))		//Inverting the boolean value of an expression by, !(ch2=='b')
Console.WriteLine("d>2  &  !(ch2=='b'), is true");

if( !(a<=10 | d>=2.5) )	///Inverting the boolean value of (a<=10 | d>=2.5)
Console.WriteLine("a<=10 | d<=2.5, is true");

if(!(a<10))	//Inverting the boolean value of (a<10)
Console.WriteLine("!(a<10) is true");

if(!b1)	//Inverting boolean value true to false
Console.WriteLine("this won't be printed");

if(!b2)	//Inverting boolean value false to true
Console.WriteLine("false is inverted to true");
}
}``````

## Output -

``````a = 10
b = 20
ch1 = a
ch2 = b
d = 2.5
b1 = True
b2 = False
!(10>15), is true
!(a<10) is true
false is inverted to true``````

Please subscribe to our social media channels for daily updates.   