Arithmetic Expression

An arithmetic expression yields one or more numeric values. The expression is formed by combining data items and constants using arithmetic operators.

The arithmetic operators are listed in Arithmetic Operators Provided in DASDL.

Table 2. Arithmetic Operators Provided in DASDL

Operator

Expression

+

Add

--

Subtract

*

Multiply

/

Divide

DIV

Integer divide

MOD

Remainder divide


The following diagrams illustrate the syntax for an arithmetic expression:

<arithmetic expression>

──┬───────┬─<arithmetic primary>───────────────────────────────────────►
  ├─  +  ─┤
  └─  -  ─┘

►─┬────────────────────────────────────┬───────────────────────────────┤
  │ ┌◄───────────────────────────────┐ │
  └─┴─┬─  +  ─┬─<arithmetic primary>─┴─┘
      ├─  -  ─┤
      ├─  *  ─┤
      ├─  /  ─┤
      ├─ DIV ─┤
      └─ MOD ─┘

<arithmetic primary>

──┬─<unsigned integer>─────────────────────────────┬───────────────────┤
  ├─<unsigned number>──────────────────────────────┤
  ├─ ( <arithmetic expression> ) ──────────────────┤
  ├─<field item name>───┬─┬────────────────────────┤
  ├─<numeric item name>─┤ └─ ( <subscript list> ) ─┘
  └─<real item name>────┘

<subscript list>

  ┌◄──────── , ────────┐
──┴─<unsigned integer>─┴───────────────────────────────────────────────┤

Examples

The following examples illustrate valid arithmetic expressions:

A + 3
B(2) * (X MOD 5)

Precedence for Arithmetic Expressions

The sequence in which operations are performed is determined by the precedence of the operators involved. The order of precedence is as follows:

  1. *, /, DIV, MOD

  2. +, –

When operators have the same precedence, the operations are performed in order from left to right. Parentheses can be used in normal mathematical fashion to override the usual order of evaluation.

Restrictions for Arithmetic Expressions

When occurring data items are referenced, all subscripts must be specified. Subscripts must be unsigned integer constants. For more information on subscripts, refer to the COBOL ANSI-85 Programming Reference Manual Volume 2: Product Interfaces.

For variable-format data sets, expressions in the fixed part can refer only to items in the fixed part. Expressions in the variable part can refer to items either in the fixed part or in the same variable part, but they cannot refer to items in other variable parts.

Virtual items must not be referenced in expressions.

Negative numbers must be enclosed within parentheses when they follow operators.