下表列出了所有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