MQL4 Assignment Operations

Overview

Assignment operations in MQL4 are used to assign values to variables. The primary assignment operator is =. Compound assignment operators combine arithmetic or bitwise operations with assignment.

Assignment Operators

  • **=**: Basic assignment. Assigns the value of the right operand to the left operand.
  • * Example: y = x; (Assigns the value of x to y)
  • **Compound Assignment Operators**:
  • * +=: Adds right operand to left operand and assigns the result to the left operand. (y += x; is equivalent to y = y + x;) * -=: Subtracts right operand from left operand and assigns the result to the left operand. (y -= x; is equivalent to y = y - x;) * *=: Multiplies left operand by right operand and assigns the result to the left operand. (y *= x; is equivalent to y = y * x;) * /=: Divides left operand by right operand and assigns the result to the left operand. (y /= x; is equivalent to y = y / x;) * %=: Modulo operation. Assigns the remainder of the division of the left operand by the right operand to the left operand. (y %= x; is equivalent to y = y % x;) * >>=: Right bitwise shift. Shifts the binary representation of the left operand to the right by the number of bits specified by the right operand and assigns the result. (y >>= x; is equivalent to y = y >> x;) * <<=: Left bitwise shift. Shifts the binary representation of the left operand to the left by the number of bits specified by the right operand and assigns the result. (y <<= x; is equivalent to y = y << x;) * &=: Bitwise AND. Performs a bitwise AND operation between the left and right operands and assigns the result to the left operand. (y &= x; is equivalent to y = y & x;) * |=: Bitwise OR. Performs a bitwise OR operation between the left and right operands and assigns the result to the left operand. (y |= x; is equivalent to y = y | x;) * ^=: Bitwise XOR. Performs a bitwise XOR operation between the left and right operands and assigns the result to the left operand. (y ^= x; is equivalent to y = y ^ x;)

    Expression Value

    The value of an expression that includes an assignment operation is the value of the left operand after the assignment has been performed.

    Constraints and Behavior

  • **Data Types**: Bitwise operations (>>=, <<=, &=, |=, ^=) can only be applied to integer types.
  • **Bitwise Shift Count**: For logical shifts (>>= and <<=), only the 5 least significant bits of the right operand (x) are used. This means the shift is performed by a value between 0 and 31 bits, effectively dropping higher bits.
  • **Modulo Operator (%=)**: The sign of the result of the modulo operation is the same as the sign of the dividend (the left operand).
  • **Multiple Assignments**: The assignment operator can be used multiple times in a single expression. Processing occurs from left to right.
  • * Example: y = x = 3; is processed as: first x is assigned 3, then y is assigned the value of x (which is now 3).

    Related Topics

  • [Arithmetic Operations](/basis/operations/mathoperation)
  • [Operations of Relation](/basis/operations/relation)
  • [Precedence Rules](/basis/operations/rules)