JUSTIFIED Clause


By default, alphabetic and alphanumeric data is left aligned, and numeric data is right aligned in the COBOL variables.

JUSTIFIED | JUST clause overrides the default alignment of data types and aligns the content to the right while displaying them. It applies only to alphabetic and alphanumeric variables but not numeric ones. Because the numeric data type is always right aligned, there is nothing to do with it.

Syntax -

01 ws-var  PIC X(var-length) [JUSTIFIED|JUST] [RIGHT].
Note! All statements coded in [ ] are optional.

For example -

01 WS-VAR      PIC X(11) JUSTIFIED RIGHT.
  • ws-variable - Specifies the variable name. From example - WS-VAR.
  • var-length - Specifies the length of the string to store in the variable. The maximum length is 256 characters. From example - 11.

Process -

  1. As defined, alphabetic or alphanumeric variables data is left aligned by default.
  2. If the alphabetic or alphanumeric variables are declared without a JUSTIFIED clause, the data transfer starts from left to right. i.e., the leftmost byte transfers first and the rightmost byte at the end. If the receiving variable is smaller than the sending data, the data truncation happens on the right side.
           01 WS-SM-VAR     PIC X(07).
    
           PROCEDURE DIVISION. 
               MOVE "MAINFRAMES"    TO WS-SM-VAR.
    The result value in WS-SM-VAR is "MAINFRA".
  3. If the alphabetic or alphanumeric variables are declared with a JUSTIFIED clause, the data movement and truncation happen in reverse order (right to left). i.e., the rightmost byte transfers first and the leftmost byte at the end. If the receiving variable is smaller than the sending data, the data truncation happens on the left side.
           01 WS-RJL-VAR    PIC X(07) JUSTIFIED RIGHT.
    
           PROCEDURE DIVISION. 
               MOVE "MAINFRAMES"    TO WS-RJL-VAR. 
    The result value in WS-SM-VAR is "NFRAMES".
  4. If the sending item is smaller than the receiving item, the unused character positions are filled with spaces.

Rules -

JUSTIFIED clause can't be used for -

  • Numeric, numeric-edited, alphanumeric-edited or national-edited variables.
  • Index variables.
  • External floating-point or internal floating-point variables.
  • Level-66 (RENAMES) and level-88 (condition-name) variables.
  • The JUSTIFIED clause can code only at the elementary level.

Practical Example -


Scenario - Below example describes how the JUSTIFIED clause used in COBOL programming.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. RJUSTIFY.
       AUTHOR. MTH. 

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01 WS-VAR        PIC X(15).
       01 WS-SM-VAR     PIC X(07). 
       01 WS-RJE-VAR    PIC X(15) JUSTIFIED RIGHT. 
       01 WS-RJL-VAR    PIC X(07) JUSTIFIED RIGHT.

       PROCEDURE DIVISION.

           MOVE "MAINFRAMES"    TO WS-VAR
                                   WS-SM-VAR 
                                   WS-RJE-VAR 
                                   WS-RJL-VAR.
 
           DISPLAY "WS-VAR     :"  WS-VAR ":". 
           DISPLAY "WS-SM-VAR  :"  WS-SM-VAR ":".
           DISPLAY "WS-RJE-VAR :"  WS-RJE-VAR ":".
           DISPLAY "WS-RJL-VAR :"  WS-RJL-VAR ":". 

           STOP RUN. 

Output -

WS-VAR     :MAINFRAMES     :
WS-SM-VAR  :MAINFRA:
WS-RJE-VAR :     MAINFRAMES:
WS-RJL-VAR :NFRAMES:

Explaining Example -

  • "MAINFRAMES" string moved to all the variables. However, WS-SM-VAR, WS-RJL-VAR are smaller than the string assigned. So, WS-SM-VAR having value "MAINFRA" due to the default left justification and the truncation happens right side.
  • WS-RJL-VAR having "NFRAMES" due to the right justification and truncation happens at the left side.
Note! In the above example, ":" used in the output to showcase the variable length including spaces.