算术运算符用于执行不同的算术运算。
这些算术运算主要是 -
添加
减法
乘法
分配
模数等。
让我们看我们有两个名为 x 和 y 的 int 变量,其中 x 存储值 10,y 存储值 20。
在下表中,您可以看到所有算术运算符,包括它们的符号、名称、它们产生的输出等。
考虑下表 -
运算符 | 名称 | 描述 | 输出 |
---|---|---|---|
+ | 添加 | Addition of two or more operands | x + y = 30 |
- | 减法 | Subtraction of the second operand from the first | x - y = -10 |
* | 乘法 | Multiplication of two or more operands | x * y = 200 |
/ | 分配 | Returns quotient after division | x / y = 0.5 |
% | 模数 | Returns remainder after division | x % y = 10 |
-expr | 一元减 | Reverse the sign of the expression | -(x - y) = 10 |
让我们在 dart 程序中使用上述所有运算符。
考虑下面显示的例子 -
void main(){ var x = 10, y = 20; print("x + y is: ${x + y}"); print("x - y is: ${x - y}"); print("x * y is: ${x * y}"); print("x / y is: ${x / y}"); print("x % y is: ${x % y}"); print("- (x - y) is: ${-(x - y)}"); }输出结果
x + y is: 30 x - y is: -10 x * y is: 200 x / y is: 0.5 x % y is: 10 - (x - y) is: 10