Expressions

An expression is classified as a primary expression, an arithmetic expression, or a conditional expression. Arithmetic and conditional expressions are made up from a sequence of primary expressions separated by operators.
 
 primary-expression unary-expression binary-expression

literal

Can be string-literal or numeric-literal

data-item

data-name [[identifier... ]...]

Parenthesized-expression

(expression)

non-parameterized-member-access

primary-expression/type-specifier::member-name 
 type-specifier

method-invocation-expression

primary-expression/type-specifier::member-name ( positional-argument... {PARAM/PARAMETER param-name = named-argument }...) 
 type-specifier Procedure Division HeaderYou can specify the parameters, both passed to a member and items returned, in either the method signature or the procedure division header. If the parameters are specified in the method signature, the procedure division header must be omitted from the method.

Where:

positional-argument
an expression corresponding to the parameter in the same position in the method definition.
param-name
the name of a parameter as defined in the method definition, not corresponding to one of the positional-arguments.

Any parameter defined by the method that does not correspond either to one of the positional arguments, or to one of the named arguments, must be an optional parameter (i.e. a parameter with a default value).

chained-constructor

invoke self/super::new ( positional-argument... {PARAM/PARAMETER param-name = named-argument }...{PROP/PROPERTY prop/property property-name = property-initializer } ) 
 type-specifier Procedure Division HeaderYou can specify the parameters, both passed to a member and items returned, in either the method signature or the procedure division header. If the parameters are specified in the method signature, the procedure division header must be omitted from the method.

Where:

positional-argument
an expression corresponding to the parameter in the same position in the constructor definition.
param-name
the name of a parameter as defined in the constructor definition, not corresponding to one of the positional-arguments.

None of positional-argument, named-argument, or property-initializer should reference instance data or local data. Typically they reference arguments of the current constructor.

Object-creation-expression

NEW simple-type-specifier( positional-argument... {PARAM/PARAMETER param-name = named-argument }...{PROP/PROPERTY prop/property property-name = property-initializer } ) 
 Type Specifier - syntax

Where:

positional-argument
An expression corresponding to the parameter in the same position in the constructor definition.
param-name
the name of a parameter as defined in the constructor definition, not corresponding to one of the positional-arguments.
Property-name
The name of an instance property associated with the class being constructed. Following the construction of the new object, the property with that name will be initialized to the value of the property-initializer.

Any parameter defined by the constructor that does not correspond either to one of the positional arguments, or to one of the named arguments, must be an optional parameter (i.e. a parameter with a default value).

Delegate-invocation-expression

RUN primary-expression( positional-argument... {PARAM/PARAMETER param-name = named-argument }... )

Where:

primary-expression
Must resolve to an object of delegate type.
positional-argument
An expression corresponding to the parameter in the same position in the delegate definition.
param-name
The name of a parameter as defined in the delegate definition, not corresponding to one of the positional-arguments.

Any parameter defined by the delegate that does not correspond either to one of the positional arguments, or to one of the named arguments, must be an optional parameter (i.e. a parameter with a default value).

subscript-expression

 
 [ subscript ] ( subscript )

Where:

[ subscript... ]
Square brackets [ ] indicate that the subscript is zero based. For example:
     myArray[5]   *> sixth item in the array

Square brackets can also be used to pass the parameters to an indexer expression.

( subscript... )
Round parentheses ( ) indicate that the subscript is one based. For example:
     myArray(5)   *> fifth item in the array
This form of subscript expression is only available for items defined in the COBOL program using the OCCURS clause. It cannot be used to access elements of an array returned by a member access or a method invocation expression. Nor can it be used to access an indexer.

Reference-modification-expression

primary-expression [offest-expression:length-expression]

The primary expression must be of type string. The offset must be of integral type. The length expression may be omitted but, if specified, must be of integral type.

The result of a reference modification expression is a string value formed by taking a substring of the original string starting from the specified offset (0 based), for the specified length, or until the end of the original string if length expression is omitted.

If the offset expression, or the sum of the offset expression and the length expression, is greater than the original string length an exception is thrown.

Concatenation-expression

primary-expression & primary-expression

where the right-hand primary-expression is not itself a concatenation expression. The result of a concatenation expression is a string value formed by concatenating all the operands. If any operand is not itself of type string, the ToString (.NET) or toString(JVM) method is applied to the operand before it participates in the concatenation.

Cast-expression

primary-expression AS [IF] type-specifier

The cast expression is used to specify an explicit conversion, for example, reference type conversion or explicit user conversion. If you do not specify the optional IF and the cast fails, an exception is thrown (InvalidCastException for .NET or ClassCastException for JVM); if you specify IF and the cast fails, no exception is thrown and the target object is set to null. See the Explicit reference conversions section for more information.

Type-of-expression

TYPE OF simple-type-specifier

Anonymous-method-expression

DELEGATE
procedure-division
END-DELEGATE

The procedure division may start with a procedure division header, which can specify zero or more parameters and an optional returning item.

Method-group-expression

METHOD non-parameterized-member-access

The expression represents any of the methods with the given name belonging to the specified type. For instance methods, the method group expression also encapsulates an object instance.

Method group expressions are implicitly converted to a delegate type by selecting the member of the method group which is compatible with the delegate type.

Example:

01 del type MyDelegate.
01 o type MyType.
set del to method o::Meth1

The compiler will set del to point to the override of Meth1 in MyType, which has the same signature as MyDelegate.

Arithmetic-expression

An arithmetic expression may be either a unary expression or a binary expression. In both cases, the expressions which are subject to operations may be:

  • Numeric expressions.
  • Expressions of a type for which a suitable operator overload has been defined.
  • In the case of binary +, the left expression may be an enum type and the right expression an integral type, or vice versa. The result is of the same enum type.
  • In the case of binary -, both expressions may be of the same enum type, or the left expression may be an enum type and the right expression an integral type. The result in the first case is a value whose type is that of the underlying type of the enum. In the second case, the result is of the same enum type.
  • In the case of binary b-and, b-or and b-xor, both expressions may be of the same enum type. The result is of the same enum type.
  • In the case of binary + and binary -, both expressions may be of the same delegate type or one expression may be a delegate type and the other a compatible anonymous method or method group. The result is of the same delegate type.

Size-of-expression

SIZE OF expression
where expression can be one of the following:
  • An item of type string
  • A .NET single-dimensional array of any type
  • An item of a collection type
Note: The word OF in the SIZE OF phrase is normally optional, but is required in the case of a DISPLAY statement, where the expression is not the first DISPLAY operand.

Table-of-expression

Helps create an array inline in .NET COBOL.

For example:

set myArray to table of ( "abc", "bcd" )

This is equivalent to:

set content of myArray to ( "abc", "bcd" )

An example of creating a multi-dimensional array:

declare myArray as string occurs any any = table of string (
               ("abc", "def")
               ("ghi", "jkl")
           )
Note: Multi-dimensional arrays do not exist in JVM environments.

unary-expression

primary expression, unary-operator unary-expression 
 unary-operator

Where unary-operator is one of '+', '-', or 'b-not'.

binary-expression

unary expression or unary expression {binary-operator unary-expression}... 
 binary-operator

Where binary-operator is one of '+', '-', '*', '/', '**', 'b-and', 'b-or', 'b-xor', 'b-left', or 'b-right'.

When the binary-expression contains more than one binary operator, the order in which the operations are performed is determined by operator precedence as follows:
  1. Shift right and shift left (b-right and b-left)
  2. binary or (b-or)
  3. binary xor (b-xor)
  4. binary and (b-and)
  5. Exponentiation (**)
  6. Multiplication and division( * and /)
  7. Addition and subtraction (* and -)

The order of evaluation can be modified by making use of parenthesized expressions.

Conditional-expression

A conditional expression may be a primary conditional expression or a combined conditional expression.

A primary conditional expression may be:

  • An arithmetic expression evaluating to a boolean type
  • A relational condition, consisting of two arithmetic expressions separated by a relational operator
  • A negated primary conditional expression
  • A conditional expression enclosed in parentheses
  • An INSTANCE OF expression
  • A CONTAINS expression

A relational operator is one of the following, where each line includes variants for the same operator.

=   EQUALS
<> NOT= NOT EQUALS
>   GREATER [THAN]
<   LESS [THAN]
>= NOT< GREATER [THAN] OR EQUAL

NOT LESS [THAN]

<= NOT> LESS [THAN] OR EQUAL

NOT GREATER [THAN]

An INSTANCE OF expression is of the form:

expression-1 IS [NOT] INSTANCE-OF type-specifier

A CONTAINS expression is of the form:

expression-1 is [not] contains expression-2

where expression-1 evaluates to either:

  • An object of type DICTIONARY and expression-2 evaluates to an object whose type is the key type of the dictionary, or
  • an object of type LIST and expression-2 evaluates to an object whose type is the element type of the list.

Await-expression

Note: Only applies to .NET COBOL.
AWAIT primary-expression

The type of primary-expression must implement the method GetAwaiter(). If the type returned by this GetAwaiter method has a GetResult method, then the type of the await-expression is the same as the type returned by this GetResult method. Otherwise, the type of the await-expression is void.

A void await-expression can be specified by using an INVOKE statement:

INVOKE AWAIT my-expression

A non-void await-expression can be used wherever an expression of its resolved type can be used.

In most cases, the type of the primary-expression will be one of System.Threading.Tasks.Task or System.Threading.Tasks.Task[T], which satisfy the above conditions. In the first case, the type of the await-expression is void, and in the second case the type of the await-expression is T.