D語言邏輯運算符
下表列出了所有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 true
" ); } if ( a || b ) { writefln("Line 2 - Condition is true
" ); } /* lets change the value of a and b */ a = 0; b = 10; if ( a && b ) { writefln("Line 3 - Condition is true
" ); } else { writefln("Line 3 - Condition is not true
" ); } if ( !(a && b) ) { writefln("Line 4 - Condition is true
" ); } return 0; }
當編譯並執行上面的程序它會產生以下結果:
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true