下表列出了所有D語言支援的算術運算子。假設變數A=10和變數B=20,則:
運算子 | 描述 | 範例 |
---|---|---|
+ | 增加了兩個運算元 | A + B = 30 |
- | 從第一中減去第二個運算元 | A - B = -10 |
* | 兩個運算元相乘 | A * B = 200 |
/ | 通過取消分子分裂分子 | B / A = 2 |
% | 模運算子和其餘整數除法 | B % A = 0 |
++ | 遞增運算子相加整數值1 | A++ = 11 |
-- | 遞減運算子通過減少整數值1 | A-- = 9 |
試試下面的例子就明白了所有的D程式設計語言的算術運算子:
import std.stdio; int main(string[] args) { int a = 21; int b = 10; int c ; c = a + b; writefln("Line 1 - Value of c is %d ", c ); c = a - b; writefln("Line 2 - Value of c is %d ", c ); c = a * b; writefln("Line 3 - Value of c is %d ", c ); c = a / b; writefln("Line 4 - Value of c is %d ", c ); c = a % b; writefln("Line 5 - Value of c is %d ", c ); c = a++; writefln("Line 6 - Value of c is %d ", c ); c = a--; writefln("Line 7 - Value of c is %d ", c ); char[] buf; stdin.readln(buf); return 0; }
當編譯並執行上面的程式,它會產生以下結果:
Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 21 Line 7 - Value of c is 22