MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL DISPLAY Statement

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.

What is the DISPLAY Statement?

The DISPLAY statement outputs data to the standard output device. It can display:

  • Literal strings: Text enclosed in quotes
  • Variable values: Current values of data items
  • Combinations: Mix of literals and variables
  • Figurative constants: SPACES, ZEROS, etc.

DISPLAY is simple to use, doesn't require file definitions, and is perfect for debugging, user messages, and program status output.

Basic DISPLAY Syntax

The basic syntax of the DISPLAY statement is:

cobol
1
2
3
DISPLAY identifier-1 [identifier-2 ...] [UPON mnemonic-name] [WITH NO ADVANCING]

Components:

  • identifier-1, identifier-2, etc.: Data items (variables) or literals to display
  • UPON mnemonic-name: Optional. Specifies an output device (if omitted, uses standard output)
  • WITH NO ADVANCING: Optional. Prevents cursor from advancing to next line

Displaying Literal Strings

Literal strings are text enclosed in quotes. You can use single or double quotes, but they must match:

cobol
1
2
3
4
5
6
PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Hello, World!" DISPLAY 'Welcome to COBOL' DISPLAY "Customer Processing System" STOP RUN.

Output:

text
1
2
3
Hello, World! Welcome to COBOL Customer Processing System

Displaying Variables

To display variable values, simply use the variable name:

cobol
1
2
3
4
5
6
7
8
9
10
11
WORKING-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:

text
1
2
3
JOHN SMITH 00012345.67 12345

Combining Literals and Variables

You can combine literals and variables in a single DISPLAY statement:

cobol
1
2
3
4
5
6
7
8
9
10
11
WORKING-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:

text
1
2
3
Customer ID: 12345 Customer Name: JOHN SMITH Account Balance: $00012345.67

Displaying Multiple Items

You can display multiple items in one DISPLAY statement by separating them with spaces:

cobol
1
2
DISPLAY CUSTOMER-ID CUSTOMER-NAME ACCOUNT-BALANCE DISPLAY "ID:" CUSTOMER-ID "Name:" CUSTOMER-NAME "Balance:" ACCOUNT-BALANCE

Items are displayed with spaces between them automatically.

WITH NO ADVANCING Clause

The WITH NO ADVANCING clause prevents the cursor from moving to the next line, allowing you to build output on the same line:

cobol
1
2
3
4
5
6
7
PROCEDURE 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):

text
1
2
Customer: JOHN SMITH ID: 12345

Displaying Figurative Constants

You can display figurative constants like SPACES, ZEROS, etc.:

cobol
1
2
3
4
5
6
7
PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Line 1" DISPLAY SPACES DISPLAY "Line 2" DISPLAY "Value: " ZEROS STOP RUN.

Formatting Output

While DISPLAY itself has limited formatting, you can format output by preparing formatted strings:

Using STRING to Format

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
WORKING-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.

Using Editing Characters

Use editing characters in PICTURE clauses to format numeric output:

cobol
1
2
3
4
5
6
7
8
9
WORKING-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.

Displaying Dates and Times

Display formatted dates and times using function calls:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
WORKING-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.

Common DISPLAY Patterns

Pattern 1: Debugging Output

cobol
1
2
3
DISPLAY "DEBUG: Entering procedure PROCESS-CUSTOMER" DISPLAY "DEBUG: Customer ID = " CUSTOMER-ID DISPLAY "DEBUG: Processing record " RECORD-COUNT

Pattern 2: User Messages

cobol
1
2
3
4
DISPLAY "Welcome to Customer Management System" DISPLAY "Please enter customer ID: " WITH NO ADVANCING ACCEPT CUSTOMER-ID DISPLAY "Processing customer " CUSTOMER-ID "..."

Pattern 3: Status Messages

cobol
1
2
3
4
DISPLAY "File opened successfully" DISPLAY "Records processed: " RECORD-COUNT DISPLAY "Errors encountered: " ERROR-COUNT DISPLAY "Processing complete"

Pattern 4: Formatted Reports

cobol
1
2
3
4
5
6
DISPLAY "========================================" DISPLAY "CUSTOMER REPORT" DISPLAY "========================================" DISPLAY SPACES DISPLAY "ID Name Balance" DISPLAY "----------------------------------------"

Best Practices for DISPLAY

Follow these best practices when using DISPLAY:

  • Use meaningful messages: Display clear, descriptive messages that help users understand what's happening
  • Format for readability: Use spacing, separators, and formatting to make output readable
  • Remove debug DISPLAYs in production: Remove or conditionally compile debug DISPLAY statements in production code
  • Use consistent formatting: Maintain consistent formatting patterns throughout your program
  • Handle long output: Break long output into multiple DISPLAY statements for readability
  • Use WITH NO ADVANCING carefully: Use WITH NO ADVANCING when building single-line output, but be aware it affects subsequent output
  • Consider performance: Excessive DISPLAY statements can impact performance; use judiciously
  • Format numbers appropriately: Use editing characters or formatting before displaying numeric values

DISPLAY vs WRITE

Understanding when to use DISPLAY vs WRITE:

DISPLAY vs WRITE Comparison
AspectDISPLAYWRITE
PurposeConsole/terminal outputFile output
File definition requiredNoYes (FILE-CONTROL, FD)
File must be openedNoYes (OPEN statement)
Use forUser messages, debugging, statusWriting records to files
FormatSimple, direct outputRecord-based output

Common DISPLAY Examples

Example 1: Simple Output

cobol
1
2
3
4
5
PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Hello, World!" DISPLAY "This is a COBOL program" STOP RUN.

Example 2: Variable Output

cobol
1
2
3
4
5
6
7
8
9
WORKING-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.

Example 3: Formatted Output

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
WORKING-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.

Explain Like I'm 5: DISPLAY

Think of DISPLAY like talking out loud:

  • DISPLAY "Hello" is like saying "Hello" out loud so everyone can hear
  • DISPLAY CUSTOMER-NAME is like saying the customer's name out loud
  • DISPLAY "Name: " CUSTOMER-NAME is like saying "Name: John" - you're saying both the label and the value
  • WITH NO ADVANCING is like not taking a breath between sentences - you keep talking on the same line

So DISPLAY is just a way for your COBOL program to "speak" and show information to the user!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Basic DISPLAY

Create a program that displays your name, age, and a welcome message using DISPLAY statements.

Exercise 2: Variable Display

Create a program that calculates the sum of two numbers and displays the calculation: "5 + 3 = 8" using variables and DISPLAY.

Exercise 3: Formatted Output

Create a program that displays a formatted customer record with labels: "ID: 12345, Name: John Smith, Balance: $1000.00".

Exercise 4: WITH NO ADVANCING

Create a program that uses WITH NO ADVANCING to build a single line of output from multiple DISPLAY statements.

Exercise 5: Report Header

Create a program that displays a formatted report header with a title, date, and column headers using multiple DISPLAY statements.

Test Your Knowledge

1. What is the primary purpose of the DISPLAY statement?

  • To write data to files
  • To output data to the console or terminal
  • To read data from files
  • To perform calculations

2. How do you display a literal string in COBOL?

  • DISPLAY Hello World
  • DISPLAY "Hello World"
  • DISPLAY (Hello World)
  • DISPLAY [Hello World]

3. What does WITH NO ADVANCING do?

  • Prevents output from being displayed
  • Prevents the cursor from advancing to the next line
  • Displays output in uppercase
  • Displays output without formatting

4. Can you display multiple items in one DISPLAY statement?

  • No, only one item per statement
  • Yes, by separating them with spaces or commas
  • Only if they are the same data type
  • Only literals, not variables

5. What is the difference between DISPLAY and WRITE?

  • There is no difference
  • DISPLAY is for console output, WRITE is for file output
  • DISPLAY is for file output, WRITE is for console output
  • DISPLAY is faster than WRITE

6. How do you create a blank line with DISPLAY?

  • DISPLAY BLANK
  • DISPLAY SPACES or DISPLAY ""
  • DISPLAY NULL
  • You cannot create blank lines

Related Concepts

Related Pages