COBOL DISPLAY Statement
DISPLAY statement prints the variable contents to the output device, from left to right. It is mainly used to -
- Debug the program when no tools are available.
- To see the values of the variable at runtime.
Syntax -
DISPLAY [variable-name|literal] 
     [UPON output-device-env] 
	 [WITH NO ADVANCING]- variable-name - refers the variable name.
- literal - refers to the literal coded with double or single quotes.
- UPON output-device-env - Optional. UPON clause specifies where the displayed information should appear. Common options include UPON CONSOLE or UPON DISPLAY. If the UPON clause is omitted, the compiler chooses the display device.
- WITH NO ADVANCING - Optional. If coded, the cursor does not move to a new line after displaying the content.
Practical Example -
Scenario1 - Basic DISPLAY
Code -
 ...
WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-VAR1      PIC X(05) VALUE 'HELLO'.
    05 WS-VAR2      PIC X(05) VALUE 'WORLD'.
 ...
 PROCEDURE DIVISION.
* Simple DISPLAY
     DISPLAY "MESSAGE1: " WS-VAR1.
     DISPLAY "MESSAGE2: " WS-VAR2.Output -
MESSAGE1: HELLO MESSAGE2: WORLD
Scenario2 - DISPLAY with multiple items
Code -
 ...
 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-VAR1      PIC X(05) VALUE 'HELLO'.
    05 WS-VAR2      PIC X(05) VALUE 'WORLD'.
 ...
 PROCEDURE DIVISION.
* Simple DISPLAY
     DISPLAY "MESSAGE: " WS-VAR1 " " WS-VAR2.Output -
MESSAGE: HELLO WORLD
Scenario3 - DISPLAY with no advancing
Code -
 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-VAR1      PIC X(05) VALUE 'HELLO'.
    05 WS-VAR2      PIC X(05) VALUE 'WORLD'.
 PROCEDURE DIVISION.
* DISPLAY WITH NO ADVANCING             
     DISPLAY WS-VAR1 WITH NO ADVANCING. 
     DISPLAY " " WS-VAR2.Output -
HELLO WORLD
