D-programming-logical-operators
提供:Dev Guides
Dプログラミング-論理演算子
次の表は、D言語でサポートされているすべての論理演算子を示しています。 変数 A が1を保持し、変数 B が0を保持すると仮定します-
Operator | Description | Example |
---|---|---|
&& | It is called Logical AND operator. If both the operands are non-zero, then condition becomes true. | (A && B) is false. |
It is called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. | (A | |
B) is true. | ! | It is 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. |
例
Dプログラミング言語で利用可能なすべての論理演算子を理解するために、次の例を試してください-
import std.stdio;
int main(string[] args) {
int a = 5;
int b = 20;
int c ;
if ( a && b ) {
writefln("Line 1 - Condition is true\n" );
}
if ( a || b ) {
writefln("Line 2 - Condition is true\n" );
}
/*lets change the value of a and b*/
a = 0;
b = 10;
if ( a && b ) {
writefln("Line 3 - Condition is true\n" );
} else {
writefln("Line 3 - Condition is not true\n" );
}
if ( !(a && b) ) {
writefln("Line 4 - Condition is true\n" );
}
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true