Csharp/CSharp Tutorial/Operator/Logical Relational Operators
Содержание
Boolean logical AND operator
class MainClass
{
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);
}
}
(1 == 1) && (2 > 1) is True (1 == 1) && (2 < 1) is False
Boolean logical NOT operator
class MainClass
{
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);
}
}
!(1 == 0) is True !(1 == 1) is False
Boolean logical OR operator
class MainClass
{
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);
}
}
(1 == 1) || (1 == 0) is True (1 == 0) || (1 == 0) is False
false expressions for operators: ==, > and
class MainClass
{
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);
}
}
10 == 1 is False 10 < 1 is False 10 <= 1 is False
List of Boolean Operators
Operator Symbol
Equals ==
Not Equal !=
Less Than <
Greater Than >
And (Short Circuiting) &&
Or (Short Circuiting) ||
And &
Or |
Less Than or Equal <=
Greater Than or Equal >=
Logical XOR ^
Logical operators
Operator Meaning & AND | OR ^ XOR (exclusive OR) || Short-circuit OR && Short-circuit AND ! NOT 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
Logical operators with an if statement
class MainClass
{
public static void Main()
{
int intValue = 1500;
string stringValue = "closed";
if ((intValue > 1000) && (stringValue == "closed"))
{
System.Console.WriteLine("and");
}
}
}
and
Relational Operators
The relational operators are as follows:
Operator Meaning == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to
using System;
class MainClass
{
static void Main(string[] args)
{
int a = 10, b = 20, c = 30;
if (a < 15 && b < 20)
c = 10;
Console.WriteLine(c);
if (a < 15 || b < 20)
c = 15;
Console.WriteLine(c);
if (!(a == 15))
c = 25;
Console.WriteLine(c);
}
}
true expressions for operators: !=, >,
class MainClass
{
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);
}
}
10 != 1 is True 10 > 1 is True 10 >= 1 is True intValue1 != intValue2 is True