Conditional Expressions

6.1.4.2. Conditional Expressions

Conditional expressions are expressions which identify the conditions under which a program may make a decision about processing to be performed. As such, conditional expressions produce a value of TRUE or FALSE.

There are seven types of conditional expressions, as follows, in increasing order of complexity.

6.1.4.2.1. Condition Names (Level-88 Items)

These are the simplest of all conditions. Observe the following code:

05 SHIRT-SIZE PIC 99V9.

88 LILLIPUTIAN VALUE 0 THRU 12.5

88 XS VALUE 13 THRU 13.5.

88 S VALUE 14, 14.5.

88 M VALUE 15, 15.5.

88 L VALUE 16, 16.5.

88 XL VALUE 17, 17.5.

88 XXL VALUE 18, 18.5.

88 HUMUNGOUS VALUE 19 THRU 99.9. The condition names “LILLIPUTIAN”, “XS”, “S”, “M”, “L”, “XL”, “XXL” and “HUMONGOUS” will have TRUE or FALSE

values based upon the values within their parent data item (SHIRT-SIZE). So, a program wanting to test whether or not the current SHIRT-SIZE value can be classified as “XL” could have that decision coded as a combined condition (the most complex type of conditional expression), as follows:

IF SHIRT-SIZE = 17 OR SHIRT-SIZE = 17.5 Or it could utilize the condition name XL as follows:

IF XL

6.1.4.2.2. Class Conditions

Figure 6-9 - Class Condition Syntax Class conditions evaluate the type of data that is

NUMERIC currently stored in a data item. ALPHABETIC

ALPHABETIC-LOWER

identifier-1 IS [ NOT ] ALPHABETIC-UPPER

OMITTED

class-name-1

1. The NUMERIC class test considers only the characters “0”, “1”, … , “9” to be numeric; only a data item containing nothing but digits will pass a IS NUMERIC class test. Spaces, decimal points, commas, currency signs, plus signs, minus signs and any other characters except the digit characters will all fail “IS NUMERIC” class tests.

2. The ALPHABETIC class test considers only upper-case letters, lower-case letters and SPACES to be alphabetic in nature.

3. The ALPHABETIC-LOWER and ALPHABETIC-UPPER class conditions consider only spaces and the respective type of letters to be acceptable in order to pass such a class test.

4. Only data items whose USAGE is either explicitly or implicitly defined as DISPLAY may be used in NUMERIC or any of the ALPHABETIC class conditions.

5. Some COBOL implementations disallow the use of group items or PIC A items with NUMERIC class conditions and the use of PIC 9 items with ALPHABETIC class conditions. OpenCOBOL has no such restrictions.

6. The OMITTED class condition is used when it is necessary for a subroutine to determine whether or not a particular argument was passed to the subroutine. In such class conditions, identifier-1 must be a LINKAGE SECTION item

defined on the USING clause of the subprograms “PROCEDURE DIVISION” header. See section 6.7 for the method to use when omitting arguments from a CALL to a subprogram.

7. The class-name-1 option allows you to test for a user-defined class. Here’s an example. First, assume the following SPECIAL-NAMES definition of the user- defined class “Hexadecimal”:

SPECIAL-NAMES. CLASS Hexadecimal IS „0‟ THRU „9‟, „A‟ THRU „F‟, „a‟ THRU „f‟.

Now observe the following code, which will execute the 150-Process-Hex-Value procedure if Entered-Value contains nothing but valid hexadecimal digits:

IF Entered-Value IS Hexadecimal PERFORM 150-Process-Hex-Value END-IF

6.1.4.2.3. Sign Conditions

Figure 6-10 - Sign Condition Syntax Sign conditions evaluate the numeric state of a PIC 9 data

item.

POSITIVE

identifier-1 IS [ NOT ] NEGATIVE

ZERO

1. Only data items defined with some sort of numeric USAGE/PICTURE can be used for this type of class condition.

2. A POSTIVE or NEGATIVE class condition will be TRUE only if the value of identifier-1 is strictly greater than or less than zero, respectively. A ZERO class condition can be passed only if the value of identifier-1 is exactly zero.

6.1.4.2.4. Switch-Status Conditions

Figure 6-11 - Using Switch Conditions In the SPECIAL-

NAMES paragraph

Relevant sections of ‘testprog’…

(see section 4.1.4 ), an external switch name

can be associated

with one or more

condition names.

ENVIRONMENT DIVISION.

These condition

names may then be

used to test the

Setting the switch and ON/OFF status of the

running the program… external switch.

SPECIAL-NAMES.

SWITCH-1 IS External-Stat-1

An example is shown

$ COB_SWITCH_1=ON

ON STATUS IS OK-To-Display

to the left.

$ export COB_SWITCH_1

$ testprog

Switch 1 Set PROCEDURE DIVISION. . . .

IF OK-To-Display DISPLAY ‘Switch 1 Set’ END-DISPLAY

END-IF . . .

6.1.4.2.5. Relation Conditions

Figure 6-12 - Relation Condition Syntax These conditions

evaluate how two

IS EQUAL TO

different values

IS =

“relate” to each

EQUALS

other.

IS NOT EQUAL TO IS NOT = IS GREATER THAN

identifier-1

IS >

identifier-2

literal-1

literal-2

arith-expr-1

IS LESS THAN

arith-expr2

index-name-1

IS <

index-name-2

IS GREATER THAN OR EQUAL TO IS >= IS NOT LESS THAN IS LESS THAN OR EQUAL TO IS <= IS NOT GREATER THAN

1. When comparing one numeric value to another, the USAGE and number of significant digits in either value are irrelevant as the comparison is performed using the actual algebraic values.

2. When comparing strings, the comparison is made based upon the program’s collating sequence (see section 4.1.2 ).

When the two string arguments are of unequal length, the shorter is assumed to be padded (on the right) with a sufficient number of SPACES as to make the two strings of equal length. String comparisons take place on a corresponding character-by-character basis until an unequal pair of characters is found. At that point, the relative position of where each character in the pair falls in the collating sequence will determine which is greater (or less) than the other.

6.1.4.2.6. Combined Conditions

Figure 6-13 - Combined Condition Syntax

A combined condition is one that computes a TRUE/FALSE value from the TRUE/FALSE values of two

AND

condition-1

condition-2

other conditions (which could – themselves – be

OR

combined conditions).

1. If either condition has a value of TRUE, the result of ORing the two together will result in a value of TRUE. Only when ORing two FALSE conditions will a result of FALSE occur.

2. In order for AND to yield a value of TRUE, both conditions must have a value of TRUE. In all other circumstances, AND produces a FALSE value.

3. When chaining multiple, similar conditions together with the same operator (OR/AND), and left or right arguments having common operators and subjects, it is possible to abbreviate the program code. For example:

IF ACCOUNT-STATUS = 1 OR ACCOUNT-STATUS = 2 OR ACCOUNT-STATUS = 7 Could be abbreviated as: IF ACCOUNT-STATUS = 1 OR 2 OR 7

4. Just as multiplication takes precedence over addition in arithmetic expressions, so does AND take precedence over OR in combined conditions. Use parenthesis to change this precedence, if necessary. For example:

FALSE OR TRUE AND TRUE

evaluates to TRUE

(FALSE OR FALSE) AND TRUE evaluates to FALSE FALSE OR (FALSE AND TRUE) evaluates to TRUE

6.1.4.2.7. Negated Conditions

Figure 6-14 - Negated Condition Syntax

A condition may be negated by prefixing it with the NOT NOT condition

operator.

1. The NOT operator has the highest precedence of all logical operators, just as a unary minus sign (which negates a numeric value) is the highest precedence arithmetic operator.

2. Parenthesis must be used to explicitly signify the sequence in which conditions are evaluated and processed if the default precedence isn’t desired. For example:

NOT TRUE AND FALSE AND NOT FALSE evaluates to FALSE AND FALSE AND TRUE which evaluates to FALSE NOT (TRUE AND FALSE AND NOT FALSE) evaluates to NOT (FALSE) which evaluates to TRUE NOT TRUE AND (FALSE AND NOT FALSE) evaluates to FALSE AND (FALSE AND TRUE) which evaluates to

FALSE