COBOL - Condition-Name Condition
A condition-name condition tests a conditional variable to determine whether its value is equal to any values that are associated with the condition-name.
Format:
If condition-name has been associated with a range of values (or with several ranges of values), the conditional variable is tested to determine whether its value
falls within the ranges, including the end values.
The result of the test is true if one
of the values that corresponds to the condition-name equals the value of its
associated conditional variable.
Condition-names are allowed for alphanumeric, DBCS, national, and floating-point
data items, as well as others, as defined for the condition-name format of the
VALUE clause.
The following example illustrates the use of conditional variables and
condition-names:
01 AGE-GROUP PIC 99.
88 INFANT VALUE 0.
88 BABY VALUE 1, 2.
88 CHILD VALUE 3 THRU 12.
88 TEENAGER VALUE 13 THRU 19.
|
AGE-GROUP is the conditional variable; INFANT, BABY, CHILD, and TEENAGER
are condition-names. For individual records in the file, only one of the values
specified in the condition-name entries can be present.
The following IF statements can be added to the above example to determine the age group of a specific record:
IF INFANT... (Tests for value 0)
IF BABY... (Tests for values 1, 2)
IF CHILD... (Tests for values 3 through 12)
IF TEENAGER... (Tests for values 13 through 19)
|
Condition-name conditions and windowed date field comparisons:
If the conditional variable is a windowed date field, the values associated with its
condition-names are treated like values of the windowed date field. That is, they
are treated as if they were converted to expanded date format, as described under
For example, given YEARWINDOW(1945), a century window of 1945-2044, and the following definition:
05 DATE-FIELD PIC 9(6) DATE FORMAT YYXXXX.
88 DATE-TARGET VALUE 051220.
|
a value of 051220 in DATE-FIELD would cause the following condition to be true:
because the value associated with DATE-TARGET and the value of DATE-FIELD would both be treated as if they were prefixed by '20' before comparison.
However, the following condition would be false:
IF DATE-FIELD = 051220... |
because in a comparison with a windowed date field, literals are treated as if they were prefixed by '19' regardless of the century window. So the above condition effectively becomes:
IF 20051220 = 19051220... |
Let's see another simple example for IF condition statement:
IDENTIFICATION DIVISION.
PROGRAM-ID. COBCONDN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHECK-GENDER.
05 GENDER PIC X(1).
88 MALE VALUE 'M'.
88 FEMALE VALUE 'F'.
PROCEDURE DIVISION.
MAIN-PARA.
SET FEMALE TO TRUE.
IF MALE
DISPLAY "GENDER - MALE"
ELSE
DISPLAY "GENDER - FEMALE"
END-IF.
STOP RUN.
|
When you compile and execute the above program, it produces the following result −
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!