COMP-2 in COBOL
Last Updated :
08 Aug, 2022
In COMP Usage data will get stored in the complete binary form. Based on the usage the data will get stored either in HALF-WORD or FULL-WORD. HALF-WORD which is nothing but 2-bytes data and FULL-WORD is nothing but 4 bytes of data.
When comes COMP-2, is similar to COMP-1. In COMP-1 data will get stored in one word in the floating point form whereas in COMP-2 data will get stored or represented in two words of storage.
Features:
- We need not specifically use the PICTURE Clause for COMP-2 variables similar to COMP-1, Because PIC will help to create a relation between data name and data type but in COMP-2 data is numeric and the data type is already pre-defined as two words.
- COMP-2 is comparatively more precise than COMP-1 because this will make the data more significant
Example:
Cobol
Program for COMP - 2 in COBOL
IDENTIFICATION DIVISION .
PROGRAM - ID . Comp2-Code.
ENVIRONMENT DIVISION .
DATA DIVISION .
WORKING-STORAGE SECTION .
77 WS-VAL1 PIC 9 ( 2 ) USAGE IS COMP .
77 WS-VAL2 PIC 9 ( 3 ) USAGE IS COMP .
77 WS-VAL3 PIC 9 ( 6 ) USAGE IS COMP .
77 WS-VAL5 USAGE IS COMP - 2 .
PROCEDURE DIVISION .
DISPLAY 'COMP USAGE DISPLAY' .
MOVE 99 TO WS-VAL1.
DISPLAY 'VALUE OF WS-VAL1 IS: ' WS-VAL1.
DISPLAY 'LENGTH OF WS-VAL1: ' LENGTH OF WS-VAL1.
MOVE 99 TO WS-VAL2.
DISPLAY 'VALUE OF WS-VAL2 IS: ' WS-VAL2.
DISPLAY 'LENGTH OF WS-VAL2: ' LENGTH OF WS-VAL2.
MOVE 99 TO WS-VAL3.
DISPLAY 'VALUE OF WS-VAL3 IS: ' WS-VAL3.
DISPLAY 'LENGTH OF WS-VAL3: ' LENGTH OF WS-VAL3.
DISPLAY ' '
DISPLAY 'COMP-2 USAGE DISPLAY' .
MOVE 999 TO WS-VAL5.
DISPLAY 'VALUE OF WS-VAL5 IS: ' WS-VAL5.
DISPLAY 'LENGTH OF WS-VAL5: ' LENGTH OF WS-VAL5.
STOP RUN .
|
Output:
Explanation:
In this example, we are displaying the comparison example for COMP and COMP-2. So we can see that the length of the COMP variables are varying based on the input data but for the COMP-2 variable length is 8 bytes and also we are not specifying the PICTURE Clause.
Share your thoughts in the comments
Please Login to comment...