+ Operator

Purpose

Used to add/concatenate two expressions.

Syntax

r = x + y

Description

The + operator is the arithmetic addition operator when:

- Both expressions are numeric data types.
- One expression is numeric and the other is a Variant (except Null).
- Both Variant expressions are numeric.
- One Variant expression is numeric and the other is a string.

The + operator is a concatenation operator when:

- Both expressions are String data types.
- One expression is a String and the other is a Variant (except Null).
- Both Variant expressions are strings.

If either expression is Null, the result is Null.

For simple arithmetic addition involving only expressions of numeric data types, the data type of result is usually the same as that of the most precise expression. The order of precision, from least to most precise, is Byte, Integer, Long, Single, Double, Currency, and Large. The following are exceptions to this order

- A Single added to a Long added results in a Double.
- A Date added to any data type results in a Date.

For Variants these exceptions apply:

- When the data type of result is a Long, Single, or Date variant that overflows its legal range, result is converted to a Double variant.

- When the data type of result is a Byte variant that overflows its legal range, result is converted to an Integer variant.

- When the data type of result is a Short variant that overflows its legal range, result is converted to a Long variant.

Example

OpenW 1

Global x% = 30, y# = 17 , a%

Print x + y

KeyGet a%

CloseW 1

Remarks

When used with integers the compiler will optimize for integer math.

When you use the + operator, you may not be able to determine whether addition or string concatenation will occur. Use the & or $ operator for concatenation to eliminate ambiguity and provide self-documenting code.

See Also

$, & and +, -, ^, *, /, \, %, Dec, Inc, Add, Sub, Mul, Div, ++, --, +=, -=, /= , *=, Operator Hierarchy

{Created by Sjouke Hamstra; Last updated: 24/06/2017 by James Gaite}