Expressions and Conditions

Expressions and Conditions

Expressions and conditions can appear in multiple locations in a CobolScript program. Positional string reference and array arguments can be expressions; CobolScript COMPUTE statements, which assign a value to a single variable, permit the use of mathematical expressions in the assigning value; the CobolScript DISPLAY and DISPLAYLF statements allow expressions as arguments, and the expressions are then evaluated before the result is displayed; and the IF statement and all variations of the PERFORM .. UNTIL statements evaluate conditions. Below are the CobolScript rules of syntax and evaluation for expressions and conditions. See Appendix A, Language Reference, for the exact syntax of COMPUTE, DISPLAY, IF, and PERFORM.

Expressions

In CobolScript, an expression is any mathematical formula that has a single-value solution. An expression can consist of any other expressions, numeric literals, variables, functions, or assignment statements using mathematical operators. All variables used in an expression must be properly defined with a numeric picture clause prior to the expression’s statement, because the variables’ values will be substituted in prior to evaluating the expression. Functions, which are mathematical operations such as sine, cosine, present values, and the natural log, are described fully in Appendix B, Function Reference.

CobolScript permitted operators

Symbol

Meaning

Example

Example result

+

Add

5 + 2

7

-

Unary negative sign

-4

-4

-

Subtract

5 - 2

3

*

Multiply

2 * 2

4

/

Divide

7 / 7

1

^

Raise to a power

2^4

16

\

Express in scientific notation

2\2

2 * 10^2 = 200

%

Modulus, or mod

10%4

2

=

Equals

1 = 3

0

Symbol

Meaning

Example

Example result

NOT =

Not equal to

1 NOT = 3

1

>

Greater than sign

18 > 1

1

1 > 18

0

1 > 1

0

<

Less than sign

18 < 1

0

1 < 18

1

1 < 1

0

>=

Greater than or equal to

18 >= 1

1

1 >= 18

0

1 >= 1

1

<=

Less than or equal to

18 <= 1

0

1 <= 18

1

1 <= 1

1

AND

Logical AND

2 AND 0

0

5 AND 3

1

0 AND 0

0

OR

Logical OR

1 OR 0

1

0 OR 0

0

3 OR 7

1

XOR

Logical exclusive OR

1 XOR 0

1

0 XOR 0

0

3 XOR 7

0

NOT

Logical NOT

NOT 1

0

NOT 0

1

NOT 9

0

Order of operations

Operations are not necessarily performed from left to right in an expression; instead, they are evaluated in an order that depends on the relative rank of the operation, so long as no parentheses are used. The order in which operations are performed in an expression, from first performed to last performed, is:

Order

Operation(s)

1

- (unary negative sign)

2

^ (power)

3

\ (scientific notation)

4

% (mod)

5

/, * (divide, multiply)

6

+, - (add, subtract)

7

>, <, >=, <= (greater than, less than, greater than or equals,

less than or equals)

8

=, NOT = (equals, not equals)

9

NOT (logical not)

10

AND (logical and)

11

XOR (logical exclusive or)

12

OR (logical or)

Rather than memorizing the order of operations, we recommend that you always use parentheses in your expressions. This will ensure that operations are performed in the order that you wish, and will avoid confusion for anyone else who reads or maintains your code.

Example expressions

Expression

Meaning

5

The number 5.

X

The value of the variable X.

X + Y

or

X+Y

The value of the variable X plus

the value of the variable Y.

X+Y + Z

The value of the variable X plus

the value of the variable Y plus

the value of the variable Z.

(((X+Y)/Z)%3) ^1.86 - SQRT(X)

The variable X plus the variable Y,

all divided by Z,

all mod’ed by 3,

all raised to the power of 1.86,

all minus the square root of the variable X (SQRT is a function).

3\2

3 multiplied by 10 to the power of 2,

equivalent to 3 * (10^2).

ROUNDED(X*Y*Z-Q/5/4^0.34)

The variable X multiplied by the variable Y multiplied by the variable Z,

all minus the value of:

The variable Q divided by 5 divided by the value

of:

4 to the power of 0.34.

The result is passed as an argument to the ROUNDED function, and is rounded to the nearest integer.

X + SIN(PI(0)/2)

The variable X plus the sine of p/2 radians (SIN and PI are both mathematical functions).

Expression construction rules

· Any level of nesting using parentheses is permitted.

· There is a finite length of expression permitted; generally speaking, keep your expressions small enough to be easily understandable and you will avoid this limit. If you do encounter the limit, divide your expression up into multiple assignment statements.

· There is a finite length of individual token (argument not separated by spaces) permitted. Insert spaces between expression components if you encounter this limit.

· Spaces are not required between expression components if a symbol (non-word) mathematical operator is separating the components; however, you should generally use spaces when performing subtraction operations on variables with dashes or underscores in their names. To illustrate, the expression “VAR-1 minus six” can be written two different ways, but the first method is preferred:

Þ (VAR-1 - 6)

Þ (VAR-1-6)

This is because if both VAR-1 and VAR-1-6 are defined variables, the meaning of the second example becomes unclear to anyone reading the code. In CobolScript, longer variable names are always substituted prior to shorter names, so that the second case above would always evaluate to the variable VAR-1-6. Even if both variables were defined, the first example would still evaluate to the quantity (VAR-1) minus 6, which is the desired result in this case.

· Alphanumeric variables or literals in expressions that are within COMPUTE statements are not allowed, even if the argument is in the context of a truth test. Thus, the statement:

COMPUTE total = (alnum_var = `Y`).

is illegal because it contains an alphanumeric variable (alnum_var) and an alphanumeric literal (`Y`), even though the expression would evaluate to a numeric result. To set values based on a test of alphanumerics, embed the assignment within an IF condition.

Conditions

Conditions are expression-like logic tests in IF and PERFORM .. UNTIL statements that evaluate to a numeric result. In CobolScript, conditions are less restrictive than expressions are, because conditions allow alphanumeric variables or literals to be included in tests. Like regular expressions, though, conditions must still evaluate to a single-value result. This numeric result determines whether the condition has evaluated to TRUE or FALSE; a result of exactly zero (0) is FALSE, while any other result is considered to be TRUE. Thus, the conditional statement below will evaluate to TRUE because the value of the condition is -40:

IF (4 + 6) *(-4) THEN

General condition rules

There are some general rules that govern conditions , no matter what form they take:

· As mentioned above, a condition will evaluate to FALSE only if its value is zero. Any other numeric result is TRUE.

· A condition must evaluate to a numeric result. Alphanumeric results are invalid.

· Any level of compound condition nesting using parentheses is permitted.

· There is a finite length of condition; generally speaking, keep your condition’s component expressions small enough to be easily understandable and you will avoid this limit. If you do encounter the limit, assign the value of one or some of your expressions to a variable prior to evaluating the condition. Then, your condition can include the variable in place of the lengthy expression. If you cannot do this because you are evaluating alphanumerics, break your condition up into multiple conditions instead, and nest your IF statements.

· There is a finite length of individual token (component of condition which is not separated by spaces) permitted. Insert spaces between condition components if you encounter this limit.

· There is no support for implied subjects or implied operators in CobolScript conditions. You must completely write out your conditions. (If you’re not familiar with these terms, don’t worry. They are COBOL constructs that don’t really have an equivalent in other computer languages.)

Condition syntax

CobolScript conditions come in two types: General logic tests, or Type I conditions, and tests of the type of value contained in an alphanumeric variable or literal, which are Type II conditions. This is the allowed syntax for both types of conditions, and rules specific to each condition type:

Type I conditions:

<Expression>

NOT <Expression>

<Expression> AND <Expression>

<Expression> OR <Expression>

<Expression> XOR <Expression>

<Expression> [IS] [NOT] = <Expression>

<Expression> [IS] [NOT] EQUAL [TO] <Expression>

<Expression> [IS] [NOT] > <Expression>

<Expression> [IS] [NOT] GREATER [THAN] <Expression>

<Expression> [IS] [NOT] < <Expression>

<Expression> [IS] [NOT] LESS [THAN] <Expression>

<Expression> [IS] [NOT] >= <Expression>

<Expression> [IS] [NOT] <= <Expression>

Rules specific to Type I conditions:

· All Type I conditions may have numeric literals, numeric variables, alphanumeric variables, or string literals in their component expressions.

· Alphanumeric comparisons of letters assigns a greater value to letters that come later in the English alphabet. Therefore:

`Z` > `A` evaluates to TRUE;

`A` = ` ` evaluates to FALSE.

· Comparison of alphanumeric values to numeric values is permitted, but will default to an alphanumeric to alphanumeric comparison. Thus, the following condition and others like it will evaluate to TRUE:

`9` = 9

Type II conditions:

<Alphanumeric-val> [IS] [NOT] NUMERIC

<Alphanumeric-val> [IS] [NOT] ALPHABETIC

Rules specific to Type II conditions:

· Type II conditions are tests to determine whether the characters contained within an alphanumeric variable or literal are NUMERIC or ALPHABETIC.

· A NUMERIC value is any valid number, including any negative sign and decimal point. NUMERIC values may not include spaces; a value such as `5 ` will not be considered numeric.

· An ALPHABETIC value is any value that falls within the ranges A-Z and a-z, or is a space.

· All Type II conditions may operate only on alphanumeric variables or string literals.