The DISPLAY statement in COBOL is used to output data to the standard output device, typically the console or terminal. It is one of the primary methods for providing output, user messages, debugging information, and program status in COBOL programs. Unlike the WRITE statement which is used for file output, DISPLAY is designed for console/terminal output and is essential for interactive programs, debugging, and providing feedback to users.
The DISPLAY statement outputs data to the standard output device. It can display:
DISPLAY is simple to use, doesn't require file definitions, and is perfect for debugging, user messages, and program status output.
The basic syntax of the DISPLAY statement is:
123DISPLAY identifier-1 [identifier-2 ...] [UPON mnemonic-name] [WITH NO ADVANCING]
Components:
Literal strings are text enclosed in quotes. You can use single or double quotes, but they must match:
123456PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Hello, World!" DISPLAY 'Welcome to COBOL' DISPLAY "Customer Processing System" STOP RUN.
Output:
123Hello, World! Welcome to COBOL Customer Processing System
To display variable values, simply use the variable name:
1234567891011WORKING-STORAGE SECTION. 01 CUSTOMER-NAME PIC X(30) VALUE 'JOHN SMITH'. 01 ACCOUNT-BALANCE PIC 9(8)V99 VALUE 12345.67. 01 CUSTOMER-ID PIC 9(5) VALUE 12345. PROCEDURE DIVISION. MAIN-PARA. DISPLAY CUSTOMER-NAME DISPLAY ACCOUNT-BALANCE DISPLAY CUSTOMER-ID STOP RUN.
Output:
123JOHN SMITH 00012345.67 12345
You can combine literals and variables in a single DISPLAY statement:
1234567891011WORKING-STORAGE SECTION. 01 CUSTOMER-NAME PIC X(30) VALUE 'JOHN SMITH'. 01 CUSTOMER-ID PIC 9(5) VALUE 12345. 01 ACCOUNT-BALANCE PIC 9(8)V99 VALUE 12345.67. PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Customer ID: " CUSTOMER-ID DISPLAY "Customer Name: " CUSTOMER-NAME DISPLAY "Account Balance: $" ACCOUNT-BALANCE STOP RUN.
Output:
123Customer ID: 12345 Customer Name: JOHN SMITH Account Balance: $00012345.67
You can display multiple items in one DISPLAY statement by separating them with spaces:
12DISPLAY CUSTOMER-ID CUSTOMER-NAME ACCOUNT-BALANCE DISPLAY "ID:" CUSTOMER-ID "Name:" CUSTOMER-NAME "Balance:" ACCOUNT-BALANCE
Items are displayed with spaces between them automatically.
The WITH NO ADVANCING clause prevents the cursor from moving to the next line, allowing you to build output on the same line:
1234567PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Customer: " WITH NO ADVANCING DISPLAY CUSTOMER-NAME DISPLAY "ID: " WITH NO ADVANCING DISPLAY CUSTOMER-ID STOP RUN.
Output (on same lines):
12Customer: JOHN SMITH ID: 12345
You can display figurative constants like SPACES, ZEROS, etc.:
1234567PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Line 1" DISPLAY SPACES DISPLAY "Line 2" DISPLAY "Value: " ZEROS STOP RUN.
While DISPLAY itself has limited formatting, you can format output by preparing formatted strings:
123456789101112131415WORKING-STORAGE SECTION. 01 CUSTOMER-ID PIC 9(5) VALUE 12345. 01 CUSTOMER-NAME PIC X(30) VALUE 'JOHN SMITH'. 01 FORMATTED-MESSAGE PIC X(60). PROCEDURE DIVISION. MAIN-PARA. STRING "Customer ID: " DELIMITED BY SIZE CUSTOMER-ID DELIMITED BY SIZE " | Name: " DELIMITED BY SIZE CUSTOMER-NAME DELIMITED BY SIZE INTO FORMATTED-MESSAGE END-STRING DISPLAY FORMATTED-MESSAGE STOP RUN.
Use editing characters in PICTURE clauses to format numeric output:
123456789WORKING-STORAGE SECTION. 01 BALANCE-RAW PIC 9(8)V99 VALUE 12345.67. 01 BALANCE-FORMATTED PIC $,$$$,$$$.99. PROCEDURE DIVISION. MAIN-PARA. MOVE BALANCE-RAW TO BALANCE-FORMATTED DISPLAY "Balance: " BALANCE-FORMATTED STOP RUN.
Display formatted dates and times using function calls:
123456789101112131415WORKING-STORAGE SECTION. 01 CURRENT-DATE PIC 9(8). 01 CURRENT-TIME PIC 9(8). 01 DATE-FORMATTED PIC X(10). 01 TIME-FORMATTED PIC X(8). PROCEDURE DIVISION. MAIN-PARA. ACCEPT CURRENT-DATE FROM DATE YYYYMMDD ACCEPT CURRENT-TIME FROM TIME MOVE CURRENT-DATE TO DATE-FORMATTED MOVE CURRENT-TIME TO TIME-FORMATTED DISPLAY "Current Date: " DATE-FORMATTED DISPLAY "Current Time: " TIME-FORMATTED STOP RUN.
123DISPLAY "DEBUG: Entering procedure PROCESS-CUSTOMER" DISPLAY "DEBUG: Customer ID = " CUSTOMER-ID DISPLAY "DEBUG: Processing record " RECORD-COUNT
1234DISPLAY "Welcome to Customer Management System" DISPLAY "Please enter customer ID: " WITH NO ADVANCING ACCEPT CUSTOMER-ID DISPLAY "Processing customer " CUSTOMER-ID "..."
1234DISPLAY "File opened successfully" DISPLAY "Records processed: " RECORD-COUNT DISPLAY "Errors encountered: " ERROR-COUNT DISPLAY "Processing complete"
123456DISPLAY "========================================" DISPLAY "CUSTOMER REPORT" DISPLAY "========================================" DISPLAY SPACES DISPLAY "ID Name Balance" DISPLAY "----------------------------------------"
Follow these best practices when using DISPLAY:
Understanding when to use DISPLAY vs WRITE:
| Aspect | DISPLAY | WRITE |
|---|---|---|
| Purpose | Console/terminal output | File output |
| File definition required | No | Yes (FILE-CONTROL, FD) |
| File must be opened | No | Yes (OPEN statement) |
| Use for | User messages, debugging, status | Writing records to files |
| Format | Simple, direct output | Record-based output |
12345PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Hello, World!" DISPLAY "This is a COBOL program" STOP RUN.
123456789WORKING-STORAGE SECTION. 01 COUNTER PIC 9(3) VALUE 1. PROCEDURE DIVISION. MAIN-PARA. PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > 5 DISPLAY "Count: " COUNTER END-PERFORM STOP RUN.
1234567891011121314WORKING-STORAGE SECTION. 01 CUSTOMER-ID PIC 9(5) VALUE 12345. 01 CUSTOMER-NAME PIC X(30) VALUE 'JOHN SMITH'. 01 OUTPUT-LINE PIC X(50). PROCEDURE DIVISION. MAIN-PARA. STRING "ID: " CUSTOMER-ID " | Name: " CUSTOMER-NAME DELIMITED BY SIZE INTO OUTPUT-LINE END-STRING DISPLAY OUTPUT-LINE STOP RUN.
Think of DISPLAY like talking out loud:
So DISPLAY is just a way for your COBOL program to "speak" and show information to the user!
Complete these exercises to reinforce your understanding:
Create a program that displays your name, age, and a welcome message using DISPLAY statements.
Create a program that calculates the sum of two numbers and displays the calculation: "5 + 3 = 8" using variables and DISPLAY.
Create a program that displays a formatted customer record with labels: "ID: 12345, Name: John Smith, Balance: $1000.00".
Create a program that uses WITH NO ADVANCING to build a single line of output from multiple DISPLAY statements.
Create a program that displays a formatted report header with a title, date, and column headers using multiple DISPLAY statements.
1. What is the primary purpose of the DISPLAY statement?
2. How do you display a literal string in COBOL?
3. What does WITH NO ADVANCING do?
4. Can you display multiple items in one DISPLAY statement?
5. What is the difference between DISPLAY and WRITE?
6. How do you create a blank line with DISPLAY?