发布于 2015-08-16 14:45:11 | 119 次阅读 | 评论: 0 | 来源: 网络整理
下表列出了所有D语言支持的逻辑运算符。假设变量A=1和变量B=0,则:
运算符 | 描述 | 实例 |
---|---|---|
&& | 所谓逻辑AND运算符。如果这两个操作数都为非零,则条件变为真。 | (A && B) is false. |
|| | 所谓逻辑或运算符。如果任何两个操作数为非零,则条件变为真。 | (A || B) is true. |
! | 所谓逻辑非运算符。使用反转其操作数的逻辑状态。如果条件为真,则逻辑NOT运算符将为false。 | !(A && B) is true. |
试试下面的例子就明白了所有的D编程语言的逻辑运算符:
import std.stdio;
int main(string[] args)
{
int a = 5;
int b = 20;
int c ;
if ( a && b )
{
writefln("Line 1 - Condition is truen" );
}
if ( a || b )
{
writefln("Line 2 - Condition is truen" );
}
/* lets change the value of a and b */
a = 0;
b = 10;
if ( a && b )
{
writefln("Line 3 - Condition is truen" );
}
else
{
writefln("Line 3 - Condition is not truen" );
}
if ( !(a && b) )
{
writefln("Line 4 - Condition is truen" );
}
return 0;
}
当编译并执行上面的程序它会产生以下结果:
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true