下表列出了所有D語言支持的算術(shù)運(yùn)算符。假設(shè)變量A=10和變量B=20,則:
| 運(yùn)算符 | 描述 | 實(shí)例 |
|---|---|---|
| + | 增加了兩個操作數(shù) | A + B = 30 |
| - | 從第一中減去第二個操作數(shù) | A - B = -10 |
| * | 兩個操作數(shù)相乘 | A * B = 200 |
| / | 通過取消分子分裂分子 | B / A = 2 |
| % | 模運(yùn)算符和其余整數(shù)除法 | B % A = 0 |
| ++ | 遞增運(yùn)算符相加整數(shù)值1 | A++ = 11 |
| -- | 遞減運(yùn)算符通過減少整數(shù)值1 | A-- = 9 |
試試下面的例子就明白了所有的D編程語言的算術(shù)運(yùn)算符:
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; }
當(dāng)編譯并執(zhí)行上面的程序,它會產(chǎn)生以下結(jié)果:
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