Summary -

In this topic, we described about the JUSTIFIED Clause with detailed example.

JUSTIFIED/JUST clause used to justify the content to the right while displaying.

By default, alphabetic and alphanumeric data types are left justified and numeric data type is right justified.

JUSTIFIED/JUST clause applies only on Alphabetic and Alphanumeric variables or data items to make them right justified if required.

JUSTIFIED|JUST clause is not applicable to numeric data items. Because, numeric data type is always right justified, so there is nothing to do with it.

JUSTIFIED clause can only specify at the elementary level.

Syntax -

JUSTIFIED Clause Syntax

As specified earlier, alphabetic or alphanumeric variables data is left justified due to the predefined left justification of data type.

When moving a data from one alphabetic variable to another alphabetic variable, the data transfer happens from left to right. i.e., the left most byte transfers first and right most byte at the end.

In this case, if the receiving variable smaller than sending variable, the data truncation happend at the right side. i.e., the data of the receiving variable length copied from left to right.

When JUSTFIED clause specified with alphabetic or alphanumeric receiving variables declaration, the assignment and truncation happens in reverse order(right to left). i.e, If the JUSTIFIED clause is specified for receiving item, the data is aligned at the rightmost character position in the receiving item.

If the sending item is larger than the receiving item, the leftmost character positions are truncated.

If the sending item is smaller than the receiving item, the unused character positions at the left are filled with spaces. The JUSTIFIED clause does not affect the values provided by the VALUE clause.

JUSTIFIED clause can't be specified for -

  • numeric, numeric-edited, alphanumeric-edited or national-edited data items.
  • index data items.
  • items described as USAGE FUNCTION-POINTER, USAGE POINTER, USAGE PROCEDURE-POINTER or USAGE OBJECT REFERENCE.
  • external floating-point or internal floating-point items.
  • level-66 (RENAMES) and level-88 (condition-name) entries.

Practical Example -

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

Code -

JUSTIFIED clause program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       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.                                                    
**************************** Bottom of Data ****************************

Output -

JUSTIFIED clause program Output

Explaining Example -

In the above example, WS-VAR, WS-SM-VAR variables are declared as alphanumeric variables without justification. WS-RJE-VAR, WS-RJL-VAR variables are declared as alphanumeric variables with JUSTIFIED right.

"MAINFRAMES" string assigned 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.