Sign Clause Example


Scenario - Below example describes about the sign data type declaration and usage in COBOL programming.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SIGNDT.
       AUTHOR. MTH.
 
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VARS.
          05 WS-SIGN-NO-SEP-P   PIC S9(03) VALUE +256.
          05 WS-SIGN-LS-P       PIC S9(03) VALUE +256
             SIGN IS LEADING SEPARATE CHARACTER.
          05 WS-SIGN-TS-P       PIC S9(03) VALUE +256
             SIGN IS TRAILING SEPARATE CHARACTER.
          05 WS-SIGN-NO-SEP-N   PIC S9(03) VALUE -256.
          05 WS-SIGN-LS-N       PIC S9(03) VALUE -256
             SIGN IS LEADING SEPARATE CHARACTER.
          05 WS-SIGN-TS-N       PIC S9(03) VALUE -256
             SIGN IS TRAILING SEPARATE CHARACTER.
 
       PROCEDURE DIVISION.

           DISPLAY "Sign +ve with no separate:  " WS-SIGN-NO-SEP-P. 
           DISPLAY "Sign +ve leading separate:  " WS-SIGN-LS-P.
           DISPLAY "Sign +ve trailing separate: " WS-SIGN-TS-P.
           DISPLAY " ".

           DISPLAY "Sign -ve with no separate:  " WS-SIGN-NO-SEP-N.
           DISPLAY "Sign -ve leading separate:  " WS-SIGN-LS-N.
           DISPLAY "Sign -ve trailing separate: " WS-SIGN-TS-N.

           STOP RUN.

Output -

Sign +ve with no separate:  25F
Sign +ve leading separate:  +256
Sign +ve trailing separate: 256+

Sign -ve with no separate:  25O
Sign -ve leading separate:  -256
Sign -ve trailing separate: 256-

Explaining Example -

In the above example:

  • It declares several numeric variables with varying signs and positions of the sign separator character.
  • It initializes them with both positive and negative values.
  • Then, it displays these variables to demonstrate how the sign is displayed when it's at different positions (leading, trailing, or without separate) for both positive and negative values.