Operator = (compound assignment) Foundation

Compound assignment (binary): performs an operation and assigns the resulting value to a variable.

Syntax
<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
Parameters
<VarName>
<VarName> designates any variable type including a field variable, member variable, or array element. The value of the variable must be of data type character, date, numeric or memo. When <VarName>designates a field variable, either the alias operator must be used or <VarName> must be declared in a FIELD statement.
<cString>
<cString> is a character expression whose value is used in a compound assignment.
<nValue>
<nValue> is a numerical expression whose value is used in a compound assignment.
<dDate>
<dDate> is a date expression whose value is used in a compound assignment.
Description

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:

Compound operators (inline operators)
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.

Examples
The inline addition and inline concatenation operator (+=)

// 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 

The inline subtraction and inline concatenation operator (-=)


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 
The inline multiplication operator (*=)

// 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 
The inline division operator (/=)

// 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 
The inline modulus operator (%=)

// 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 
The inline exponentiation operator (**=)

// 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 
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.