ILE COBOL Programmer's Guide

Arithmetic Expressions

In the examples of COMPUTE shown above, everything to the right of the equal sign represents an arithmetic expression. Arithmetic expressions can consist of a single numeric literal, a single numeric data item, or a single Intrinsic Function reference. They can also consist of several of these items connected by arithmetic operators. These operators are evaluated in a hierarchic order.

Table 7. Operator Evaluation

Operator Meaning Order of Evaluation
Unary + or - Algebraic sign First
** Exponentiation Second
/ or * Division or multiplication Third
Binary + or - Addition or subtraction Last

Operators at the same level are evaluated from left to right; however, you can use parentheses with these operators to change the order in which they are evaluated. Expressions in parentheses are evaluated before any of the individual operators are evaluated. Parentheses, necessary or not, make your program easier to read.

In addition to using arithmetic expressions in COMPUTE statements, you can also use them in other places where numeric data items are allowed. For example, you can use arithmetic expressions as comparands in relation conditions:

IF (A + B) > (C - D + 5) THEN...


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