D中算術運算符

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