COBOL/400 Language Help


Arithmetic Expressions

Arithmetic expressions are used as operands of certain conditional and arithmetic statements.

An arithmetic expression may consist of any of the following:

  1. An identifier described as a numeric elementary item
  2. A numeric literal
  3. The figurative constant ZERO
  4. Identifiers and literals, as defined in items 1, 2, and 3, separated by arithmetic operators
  5. Two arithmetic expressions, as defined in items 1, 2, 3, and/or 4, separated by an arithmetic operator
  6. An arithmetic expression, as defined in items 1, 2, 3, 4, and/or 5, enclosed in parentheses.

Any arithmetic expression may be preceded by a unary operator.

Identifiers and literals appearing in arithmetic expressions must represent either numeric elementary items or numeric literals on which arithmetic may be performed.

If an exponential expression is evaluated as both a positive and a negative number, the result will always be the positive number. The square root of 4, for example (4 ** 0.5) is evaluated as +2 and -2. The compiler always returns +2.

If the value of an expression to be raised to a power is zero, the exponent must have a value greater than zero. Otherwise, the size error condition exists. In any case, where no real number exists as a result of the evaluation, the size error condition exists.

Arithmetic Operators

Five binary arithmetic operators and two unary arithmetic operators may be used in arithmetic expressions. They must be preceded and followed by a space, and they are interpreted as follows:

Binary Operator
Operation Performed
 
 
+
Addition
-
Subtraction
*
Multiplication
/
Division
**
Exponentiation

Unary Operator
Operation Performed
 
 
+
Multiplication by +1
-
Multiplication by -1

You can use parentheses in arithmetic expressions to specify the order in which elements are to be evaluated. Expressions within parentheses are evaluated first. When expressions are contained within a nest of parentheses, evaluation proceeds from the innermost to the outermost set.

When parentheses are not used, or when parenthesized expressions are at the same level of inclusiveness, the following hierarchical order is implied:

  1. Unary operator
  2. Exponentiation
  3. Multiplication and division
  4. Addition and subtraction

Parentheses either eliminate ambiguities in logic where consecutive operations appear at the same hierarchic level, or modify the normal hierarchic sequence of execution when this is necessary. When the order of consecutive operations at the same hierarchic level is not completely specified by parentheses, the order is from left to right.

An arithmetic expression may begin only with a left parenthesis, a unary operator, or an operand (that is, an identifier or a literal). It may end only with a right parenthesis or an operand. An arithmetic expression must contain at least one reference to an identifier or a literal.

There must be a one-to-one correspondence between left and right parentheses in an arithmetic expression, with each left parenthesis placed to the left of its corresponding right parenthesis.

If the first operator in an arithmetic expression is a unary operator, it must be immediately preceded by a left parenthesis if that arithmetic expression immediately follows an identifier or another arithmetic expression.


[ Top of Page | Previous Page | Next Page | Table of Contents ]