Arithmetic Expressions

6.1.4.1. Arithmetic Expressions

Arithmetic expressions are formed using following operators. In complex expressions composed of multiple operators, a precedence of operation applies whereby those operations having a higher precedence are computed first before operations with a lower precedence.

Precedence / Operation Discussion Figure 6-2 – Unary - Operator Syntax Precedence: 1 ST (Highest)

The unary - operator returns the arithmetic negation of its single argument, effectively

numeric-literal-1 returning as its value the product of its argument

identifier-1

and -1.

( arith-expr-1 )

Figure 6-3 – Unary + Operator Syntax Precedence: 1 ST (Highest) The unary + operator returns the value of its single numeric-literal-1

argument, effectively returning as its value the

identifier-1 product of its argument and +1.

( arith-expr-1 )

Figure 6-4 - Exponentiation Operator Syntax Precedence: 2 nd

The value of the left-hand argument raised to the power indicated by the right-hand argument is

numeric-literal-1

computed. OpenCOBOL allows the “^” symbol to identifier-1

numeric-literal-2

** identifier-2

be used in lieu of the “**” symbol.

( arith-expr-1 )

( arith-expr-2 )

Figure 6-5 - Exponentiation Operator Syntax Precedence: 3 rd

The product of the left-hand argument and the numeric-literal-1

right-hand argument is computed. identifier-1

numeric-literal-2

* ( arith-expr-2 )

identifier-2

( arith-expr-1 )

Figure 6-6 - Division Operator Syntax Precedence: 3 rd

The value of the left-hand argument divided by the numeric-literal-1

numeric-literal-2

right-hand argument is computed. identifier-1

/ identifier-2

( arith-expr-1 )

( arith-expr-2 )

Precedence / Operation Discussion Figure 6-7 - Addition Operator Syntax

Precedence: 4 th (Lowest)

The sum of the left-hand argument and the right- numeric-literal-1

numeric-literal-2

hand argument is computed. identifier-1

+ identifier-2

( arith-expr-1 )

( arith-expr-2 )

Figure 6-8 - Subtraction Operator Syntax Precedence: 4 th (Lowest)

The value of the right-hand argument subtracted numeric-literal-1

from the left-hand argument is computed. identifier-1

numeric-literal-2

– identifier-2

( arith-expr-1 )

( arith-expr-2 )

The COBOL standards require the use of at least one space before and after the exponentiation, multiplication, division, addition and subtraction operators. This is the best policy to follow when coding expressions as it ensures compatibility with other COBOL implementations and avoids the need to deal with the following special rules which define the circumstances under which leading and/or trailing spaces may be omitted:

1. OpenCOBOL does not actually require leading or trailing spaces around the exponentiation, multiplication or division operators.

2. The ADDITION operator must be followed by a space if it is followed by an unsigned numeric literal. Failure to do so (for example: “4+3”) will result in an “Invalid Expression” error because the compiler will treat the “+” as an attempt to specify a signed numeric literal, leaving the expression with no operator. In any other circumstances, the leading and trailing spaces around the ADDITION operator are optional.

3. The SUBTRACTION operator must be followed by a space if it is followed by an unsigned numeric literal.

Failure to do so (for example: “4-3”) will result in an “Invalid Expression” error because the compiler will treat the “-” as an attempt to specify a signed numeric literal, leaving the expression with no operator.

4. The SUBTRACTION operator must have a leading and/or trailing space if neither argument is a parenthesized expression. Failure to include one or the other space (“3-Arg”, “Arga-Argb”, …) will cause the compiler to look for a defined reserved word or user- defined name that (hopefully) doesn’t exist – generating a “‘identifier’ Undefined” error. If you are really unlucky, it actually WILL find such an identifier, which is almost certain to cause runtime problems!

5. If the argument of a UNARY PLUS operator is an unsigned numeric literal, the unary plus operator must be followed by a space to avoid being treated as part of the numeric literal (thus making it a signed positive numeric literal).

6. If the argument of a UNARY NEGATION operator is an unsigned numeric literal, the unary negation operator must be followed by a space to avoid being treated as part of the numeric literal (thus making it a signed negative numeric literal).

Here are some examples of arithmetic expressions (all of which involve numeric literals, to simplify the discussion). Expression

Result Notes 3*4+1

13 * has precedence over +

2 ^ 3 * 4 - 10 3 22 2 is 8, times 4 is 32, minus 10 is 22.

2 ** 3 * 4 - 10

22 Same as the above – OpenCOBOL allows either “^” or “**” to be used as the exponentiation operator.

15 Parenthesis provide for a recursive application of the arithmetic expression rules, allowing arithmetic expressions to become components within other, more complex, arithmetic expressions.

15.35 Integer and non-integer operands may be freely intermixed

– 1.15 Of course, arithmetic expression operands may be numeric data items (any USAGE except DISPLAY, POINTER or

PROGRAM POINTER) as well as numeric literals.