Microsoft KB Archive/44763

Explanation of COBOL Level 88; Like BOOLEAN Date Type

PSS ID Number: Q44763 Article last modified on 04-20-1993

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: A COBOL level 88 data entry is called a “named condition.” It is equivalent to BOOLEAN types in other high-level languages. Named conditions are used to test for “true” and “false” conditions that may arise in a program. They enhance the readability of the program, and support COBOL’s English-like quality. A named condition is always subordinate to some elementary data item in the DATA DIVISION, but they themselves cannot have subordinate items. Named conditions have no PICTURE clause. They are called “Named conditions” because they have only a VALUE clause, and they give a name to that VALUE. The examples below should clarify how named conditions can be used.

More Information: The correct syntax for forming a named condition is: 88  VALUE literal-1 [literal-2 [THRU literal-3]…]. The THRU clause allows testing for a range of values and it is permissible to mix literals and ranges as shown in the following: WORKING-STORAGE SECTION. 01 PROMPT-USER-TO-QUIT PIC X value ‘N’. 88 USER-WANTS-TO-QUITE VALUE ‘Y’ ‘y’. 88 USER-WANTS-TO-CONTINUE VALUE ‘N’ ‘n’ ’ ’ THRU ‘X’ ‘Z’ THRU ‘x’ ‘z’ THRU ‘~’. The following code fragments show the usefulness of named conditions. The named condition is first described in the DATA DIVISION. The data item KEY-PRESSED in the example below is going to be ACCEPTed FROM ESCAPE KEY to see which key terminated an ACCEPT of a SCREEN. When the value is 0 (zero), then it was a normal terminate. If the value is one, then the ESC key was pressed; if the value was 2 through 11, then it was one of the function keys that terminated the ACCEPT. The goal is to provide the most readable way of testing for the multiple conditions possible. WORKING-STORAGE SECTION. 01 KEY-PRESSED PIC 9(2). 88 NORMAL-SCREEN-TERMINATION VALUE 0. 88 ESC-KEY-WAS-PRESSED VALUE 1. 88 A-FUNCTION-KEY-WAS-PRESSED VALUE 2 THRU 11. SCREEN SECTION. 01 GENERIC-SCREEN PIC X. PROCEDURE DIVISION. MAIN. ACCEPT GENERIC-SCREEN ACCEPT KEY-PRESSED FROM ESCAPE KEY. Now the program can determine which key terminated the ACCEPT and take the appropriate action. This can be done in two ways as demonstrated in the pairs of IF…THEN statements below. The first IF…THEN in the pair shows how we would have to write the code if there were no named conditions. The second shows the named condition makes the IF…THEN statement easier to understand, improves the readability, and can greatly shorten the code. * These two are equivalent code. IF KEY-PRESSED IS EQUAL TO 1 THEN PERFORM (ETC.) IF ESC-KEY-WAS-PRESSED THEN PERFORM (ETC.) * These two are equivalent code. IF KEY-PRESSED IS EQUAL TO 0 THEN PERFORM (ETC.) IF NORMAL-SCREEN-TERMINATION THEN PERFORM (ETC.) * These two are equivalent code. IF KEY-PRESSED IS LESS THAN OR EQUAL TO 11 AND KEY-PRESSED IS GREATER THAN OR EQUAL TO 2 THEN PERFORM (ETC.) IF A-FUNCTION-KEY-WAS-PRESSED THEN PERFORM (ETC.) A named condition is not useful unless it is descriptive and, in fact, can detract from the readability if not well written. A well-formed named conditional sounds good in an English sentence or in an IF…THEN statement, such as the ones above, and it describes what condition has occurred when its value becomes “true.” The following is a full program example. Code Example ———— $SET ANS85 DATA DIVISION. WORKING-STORAGE SECTION. 77 STUFF-TO-ACCEPT PIC X. 78 CLEAR-SCREEN VALUE X“E4”. 01 KEY-PRESSED PIC 9(2). 88 ESC-KEY-PRESSED VALUE 1. 88 FUNCTION-KEY-WAS-PRESSED VALUE 2 THRU 11. 88 F5-WAS-PRESSED VALUE 6. SCREEN SECTION. 01 ACCEPT-SCREEN. 05 LINE 10 COLUMN 20 VALUE “HIT ANY FUNCTION KEY”. 05 LINE 11 COLUMN 20 VALUE “THE ESC KEY ENDS THE PROGRAM”. 05 LINE 12 COLUMN 20 VALUE “PRESS F5 FOR A MESSAGE”. 05 PIC X USING STUFF-TO-ACCEPT. PROCEDURE DIVISION. CALL CLEAR-SCREEN. PERFORM DISPLAY-KEYS UNTIL ESC-KEY-PRESSED. STOP RUN. DISPLAY-KEYS. DISPLAY ACCEPT-SCREEN. ACCEPT ACCEPT-SCREEN ACCEPT KEY-PRESSED FROM ESCAPE KEY. IF FUNCTION-KEY-WAS-PRESSED THEN IF F5-WAS-PRESSED DISPLAY “COBOL IS FUN!!!!” AT 2020 ELSE SUBTRACT 1 FROM KEY-PRESSED DISPLAY “THAT WAS FUNCTION KEY NUMBER” AT 2020 DISPLAY KEY-PRESSED AT 2050 ADD 1 TO KEY-PRESSED END-IF ELSE DISPLAY “THAT IS NOT A FUNCTION KEY” AT 1120 END-IF.

Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.