Arithmetic Expressions
Arithmetic expressions consist of numeric values and arithmetic operators arranged together to perform an arithmetic operation. The numeric values can be literals, symbols, variables or intrinsic functions.
The simplest arithmetic expression is a single numeric literal, symbol, variable or intrinsic function. If the arithmetic operator is omitted, the unary + operator is implied. A simple arithmetic expression can combine two operands using an arithmetic operator. More complicated arithmetic expressions are formed by combining simple expressions using arithmetic operators.
Expressions are evaluated by performing the operations specified by each of the arithmetic operators on the adjacent elements.
Precedence
When more than one operator appears in an expression, operations are performed in order of precedence. If two or more operators have the same precedence, the leftmost operation is performed first (refer to the table below). Operations of equal precedence are performed from left to right.
Operator |
Operation |
Precedence |
---|---|---|
( ) |
Expression within parentheses |
1 |
^ |
Exponent |
2 |
+ - |
Unary plus and minus |
3 |
* / |
Multiplication and division |
4 |
+ - |
Addition and subtraction |
5 |
|
6 |
|
: |
7 |
|
|
8 |
|
AND, OR |
9 |
Parentheses
Any subexpression can be enclosed in parentheses. Within the parentheses, the rules of precedence apply. However, the overall expression within parentheses has the highest precedence and is evaluated first. For example:
(10+2)*(3-1) = 12*2 = 24
Parentheses can be used anywhere in order to clarify the order of evaluation, even if they do not change the order.
Strings in Arithmetic Expressions
If a string value containing only numeric characters is included in an arithmetic expression, it is treated as a decimal number. A null string value is treated as zero. For example:
123 + "456" = 579
If a string containing non-numeric characters is included in an arithmetic expression, the following message is displayed when the program is run.
[B16] Non-numeric data when numeric required; zero used.
Examples
The following are examples of arithmetic expressions
6+8/2+5 evaluates to 15
(6+8)/(2+5) evaluates to 2
12/2*3 evaluates to 18
12/(2*3) evaluates to 2
A+75/25 evaluates to A + 3
-5+2 evaluates to -3
-(5+2) evaluates to -7
8*(-2) evaluates to -16
5*"3" evaluates to 15
3:2*2 evaluates to 34
3*3+4*4 evaluates to 25
(3*3)+(4*4) evaluates to 25
(3*3+4)*4 evaluates to 52