Operator = (compound assignment) Foundation
Compound assignment (binary): performs an operation and assigns the resulting value to a variable.
<VarName> += <nValue> // addition
<VarName> += <cString> // string concatenation
<VarName> -= <cString> // string concatenation
<VarName> -= <nValue> // subtraction
<VarName> -= <dDate> // subtraction
<VarName> *= <nValue> // multiplication
<VarName> /= <nValue> // division
<VarName> %= <nValue> // modulus
<VarName> **= <nValue> // exponential
<VarName> ^= <nValue> // exponential
A compound assignment operator first executes the specified operation with the value of the variable <VarName> and the value of the expression and then assigns the result of the operation to the variable. The various operators are defined as follows:
Operator | Operation | Description |
---|---|---|
x += y | x := (x + y) | Inline addition, inline concatenation |
x -= y | x := (x - y) | Inline subtraction, inline 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 |
Like the inline assignment operator (:=), the compound assignment operator can be used in expressions. The expression on the right side of the operator is always evaluated first, then the specified operation is performed with the value of the variable. Finally, the resulting value is assigned to the variable on the left side of the operator. Compound assignments follow the same rules as inline assignments.
// This example shows various results of the operator +=
PROCEDURE Main
LOCAL nValue, dDate, cString
nValue := 4 // addition
? nValue += 6 // result: 10
? Sqrt( nValue += 90 ) // result: 10
? nValue // result: 100
dDate := CtoD("12/31/94") // addition
? dDate += 6 // result: 01/06/95
cString := "James " // string concatenation
? cString += "Bond" // result: James Bond
RETURN
PROCEDURE Main
LOCAL nValue, dDate, cString
nValue := 25 // subtraction
? nValue -= 15 // result: 10
? Sqrt( nValue -= 6 ) // result: 2
? nValue // result: 4
dDate := CtoD("12/31/94") // subtraction
? dDate -= 7 // result: 12/24/94
cString := "James " // string concatenation
? cString -= "Bond" // result: JamesBond
RETURN
// This example shows various results of the operator *=.
PROCEDURE Main
LOCAL nValue := 100
? (nValue *= 50) - 500 // result: 4500
? nValue // result: 5000
nValue *= 2
? nValue // result: 10000
RETURN
// This example shows various results of the operator /=.
PROCEDURE Main
LOCAL nValue := 100
? (nValue /= 50) + 50 // result: 52
? nValue // result: 2
nValue /= 0.5
? nValue // result: 4
RETURN
// This example shows various results of the operator %=.
PROCEDURE Main
LOCAL nValue := 100
? (nValue %= 30) + 50 // result: 60
? nValue // result: 10
nValue %= 4
? nValue // result: 2
RETURN
// This example shows results of an inline exponentiation operator.
PROCEDURE Main
LOCAL nValue := 2
? (nValue **= 3) - 6 // result: 2
? nValue // result: 8
nValue ^= 4
? nValue // result: 4096
RETURN
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.