Addition Assignment Operator (+=)

Adds the value of an expression to the value of a variable and assigns the result to the variable.

result += expression 

Arguments

  • result
    Any variable.

  • expression
    Any expression.

Remarks

Using this operator is almost the same as specifying result = result + expression, except that result is only evaluated once.

The type of the expressions determines the behavior of the += operator.

Result

expression

Then

char

char

Error

char

Numeric

Add

char

String

Error

Numeric

char

Add

Numeric

Numeric

Add

Numeric

String

Concatenate

String

char

Concatenate

String

Numeric

Concatenate

String

String

Concatenate

For concatenation, numbers are coerced into a string representation of the numeric value, and chars are considered to be strings of length 1. For addition of a char and a number, the char is coerced into a numeric value, and the two numbers are added. Some combinations of types give errors because the resulting type of the addition cannot be coerced into the required output type.

Example

The following example illustrates how the addition assignment operator processes expressions of different types.

var str : String = "42";
var n : int = 20;
var c : char = "A";  // The numeric value of "A" is 65.
var result;
c += n;         // The result is the char "U".
n += c;         // The result is the number 105.
n += n;         // The result is the number 210.
n += str;       // The result is the number 21042.
str += c;       // The result is the string "42U".
str += n;       // The result is the string "42U21042".
str += str;     // The result is the string "42U2104242U21042".
c += c;         // This returns a runtime error.
c += str;       // This returns a runtime error.
n += "string";  // This returns a runtime error.

Requirements

Version 1

See Also

Reference

Addition Operator (+)

Assignment Operator (=)

Concepts

Operator Precedence

Operator Summary