Language Elements and Reference:xpplrm

Assignment operators Foundation

Xbase++ provides two assignment operators: the simple assignment operator = and the inline assignment operator :=. Both operators are binary and assign the value of the right operand to the left operand which must be a variable. The right operand can be an expression, a literal or another variable. The simple assignment operator can only be used within a statement, while the inline assignment operator can also appear within expressions and can assign a value to several variables.

nValue = 10                        // statement 

nValue := 10                       // expression 

dDate1 := dDate2 := Date()         // expression 

In the first two lines, the value 10 is assigned to the variable nValue and both lines are equivalent. In the third line the current date is assigned to two variables at once. This is only possible using the inline assignment operator.

When the simple assignment operator appears within an expression, it is interpreted as an equality operator and compares the values of the operands with one another instead of performing an assignment:

? ( nValue := 100 )                // results: 100 

? ( nValue = 100 )                 // result: .T. 

Using the command ?, both lines output to the screen the value of the expression in parentheses. In the first line, a value is assigned to a variable and the expression returns the value of the variable. In the second line the simple assignment operator appears within an expression and is interpreted as the equality operator. It compares the values of its operands. The result of this expression is the logical value .T. (true). The simple assignment operator is overloaded, meaning the operation which is executed by the operator is dependent on program context.

Along with these two assignment operators, there is another group of combined inline operators which first execute an operation and then perform an assignment. A list of all assignment operators is shown in the following tables:

Assignment Opererators - Simple assignment
Operator Data type Operation
= All Assign value to a variable

Assignment Opererators - Inline assignment
Operator Data type Operation
:= All Assign value to one or more variables

Assignment Opererators - Combined inline assignment
Operator Data type Operation
+= D, N Inline addition
+= C, M Inline concatenation
-= D, N Inline subtraction
-= C, M Inline concatenation
*= N Inline multiplication
/= N Inline division
%= N Inline modulus
**= N Inline exponentiation
^= N Inline exponentiation

Combined inline assignments can only be executed using specific data types as operands. The operations which the various operators execute are described as follows:

x += y   //   x := (x + y)      Inline addition, concatenation 

x -= y   //   x := (x - y)      Inline subtraction, concatenation 

x *= y   //   x := (x * y)      Inline multiplication 

x /= y   //   x := (x / y)      Inline division 

x %= y   //   x := (x % y)      Inline modulus 

x **=y   //   x := (x **y)      Inline exponentiation 

x ^= y   //   x := (x ^ y)      Inline exponentiation 

Combined assignment operators first complete the operation using the left and right operand and then assign the result to the left operand.

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.