Csharp-logical-operators
提供:Dev Guides
C#-論理演算子
次の表は、C#でサポートされているすべての論理演算子を示しています。 変数 A がブール値trueを保持し、変数 B がブール値falseを保持すると仮定します-
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non zero then condition becomes true. | (A && B) is false. |
Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. | (A | |
B) is true. | ! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
例
次の例は、C#で使用可能なすべての論理演算子を示しています-
using System;
namespace OperatorsAppl {
class Program {
static void Main(string[] args) {
bool a = true;
bool b = true;
if (a && b) {
Console.WriteLine("Line 1 - Condition is true");
}
if (a || b) {
Console.WriteLine("Line 2 - Condition is true");
}
/*lets change the value of a and b*/
a = false;
b = true;
if (a && b) {
Console.WriteLine("Line 3 - Condition is true");
} else {
Console.WriteLine("Line 3 - Condition is not true");
}
if (!(a && b)) {
Console.WriteLine("Line 4 - Condition is true");
}
Console.ReadLine();
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true