跳至内容

Arithmetic Operations

算术运算

算术运算包括加法和乘法运算:

Sum of variables                        i = j + 2;
Difference of variables                 i = j - 3;
Changing the sign                       x = - x;
Product of variables                    z = 3 * x;
Division quotient                       i = j / 5;
Remainder of division                   minutes = time % 60;
Adding 1 to the variable value          i++;
Adding 1 to the variable value          ++i;
Subtracting 1 from the variable value   k--;
Subtracting 1 from the variable value   --k;

递增和递减运算仅适用于变量,不适用于常量。前缀“increment”(++i)和“decrement”(–k)在变量在表达式中被使用之前应用于该变量。

后递增(i++)和后递减(–k)在变量在表达式中被使用后立即应用于该变量。

重要提示

int i=5;
int k = i++ + ++i;

将上述表达式从一个编程环境迁移到另一个环境时(例如,从Borland C++到MQL4),可能会出现计算问题。通常,计算的顺序取决于编译器的实现方式。实际上,有两种实现后递减(后递增)的方法:

  1. 后递减(后递增)在计算完整个表达式后应用于该变量。
  2. 后递减(后递增)在运算时立即应用于该变量。

目前,MQL4中实现了第一种后递减(后递增)的计算方法。但即使了解这一特性,也不建议尝试使用它。

示例:

int a=3;
a++;            // valid expression
int b=(a++)*3;  // invalid expression

另请参阅

优先级规则

最后更新于