Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Arithmetic expressions

Function

Arithmetic expressions allow the user to combine arithmetic operations.

Format

An arithmetic expression may be one of the following:

  • An identifier of a numeric elementary item.

  • A numeric literal.

  • Two arithmetic expressions separated by an arithmetic operator or an arithmetic expression enclosed in parentheses.

Any arithmetic expression may be preceded by a unary plus (+) or a unary minus (-).

Arithmetic operators

The following operators may be used in arithmetic expressions:

Binary arithmetic operator

Meaning

+

Addition

-

Subtraction

*

Multiplication

/

Division

**

Exponentiation

Unary arithmetic operator

Meaning

+

The effect of multiplication by the numeric literal +1

-

The effect of multiplication by the numeric literal -1

As defined by the standard, a binary arithmetic operator must always be preceded and followed by a space.
However, the compiler allows all these operators, with the exception of the addition and subtraction operators, to be used without the enclosing spaces. The subtraction operator (-) must always be preceded and followed by a space.

The addition operator (+) must be followed by a space if it occurs before an unsigned numeric literal. Both operators may be immediately preceded and followed by a parenthesis.

A unary + must be followed by a space, if it is before an unsigned literal.
A unary - always must be followed by a space.

Rules for the formation and evaluation of expressions

  1. An arithmetic expression may begin only with a left parenthesis, a unary +, a unary -, an identifier, or a literal; and it may end only with a right parenthesis or a variable (identifier or literal).

  2. There must be a one-to-one correspondence between left and right parentheses in an arithmetic expression.

  3. Table 18 shows the permissible combinations of operators, variables and parentheses in arithmetic expressions.
     

    First symbol

    Second symbol

    identifier, literal

    arithmetic operator

    unary operator

    (

    )

    identifier, literal

    -

    P

    -

    -

    P

    arithmetic operator

    P

    -

    P

    P

    -

    unary operator

    P

    -

    -

    P

    -

    (

    P

    -

    P

    P

    -

    )

    -

    P

    -

    -

    P

    Table 18: Valid symbol combinations in arithmetic expressions

    P indicates that the two symbols may appear consecutively (permissible pair).
    - indicates that the two symbols must not appear consecutively (invalid pair).

  4. Parentheses may be used in arithmetic expressions in order to indicate the order in which the elements are to be evaluated.

  5. Expressions within parentheses are evaluated first. When nested parentheses are used, evaluation proceeds from the innermost to the outermost set of parentheses.

  6. When no parentheses are used, or parenthesized expressions are at the same level of inclusiveness, the following hierarchical order of execution is implied:

    1. Unary plus or minus (evaluated first)

    2. Exponentiation

    3. Multiplication and division

    4. Addition and subtraction (evaluated last)

  7. If consecutive operations of the same hierarchical level occur, they are evaluated from left to right.

General rules

  1. Parentheses are used either to eliminate ambiguities in logic where consecutive operations of the same hierarchical level appear, or to modify the normal hierarchical sequence of execution.

  2. Arithmetic expressions are used in arithmetic and conditional statements.

Example 8-2

Expression:

A + (B - C) * D

Evaluation:

1. B - C

(denote result by x)


2. x * D

denote result by y)


3. A + y

(final result)

Example 8-3

Expression:

A + ((B / C) + (D ** E) * F) - G

Evaluation:

1. B / C

(denote result by z)


2. D ** E

(denote result by x)


3. x * F

(denote result by y)


4. z + y

(denote result by a)


5. A + a

(denote result by b)


6. b - G

(final result)