COBOL DISPLAY
DISPLAY writes data to the screen or console (standard output). It is the simplest way to see what a program is
doing, and in batch jobs its output goes to the job log (SYSOUT).
DISPLAY item-1 item-2 ... [UPON device] [WITH NO ADVANCING]Each operand can be a literal, a data item, a figurative constant or a function call. They are printed one after another with no separator, then a newline.
How different items are displayed
How a value appears depends on its picture. Knowing these rules saves a lot of confusion:
| Item | Displayed as |
|---|---|
PIC X(n) |
All n characters, including trailing spaces |
PIC 9(n) |
All n digits with leading zeros |
PIC S9(n) |
A sign (+ or -) followed by all digits |
PIC 9(n)V9(m) |
Digits with a . at the implied point (GnuCOBOL) |
Edited PIC ZZ9.99 |
Exactly as edited (that's what edited pictures are for) |
IDENTIFICATION DIVISION.
PROGRAM-ID. DISPLAY-RULES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TEXT PIC X(8) VALUE "COBOL".
01 WS-COUNT PIC 9(5) VALUE 42.
01 WS-CHANGE PIC S9(3) VALUE 42.
01 WS-DEBT PIC S9(3)V99 VALUE -7.5.
01 WS-PRETTY PIC ZZ,ZZ9.99 VALUE 1234.5.
PROCEDURE DIVISION.
DISPLAY "[" WS-TEXT "]"
DISPLAY "[" WS-COUNT "]"
DISPLAY "[" WS-CHANGE "]"
DISPLAY "[" WS-DEBT "]"
DISPLAY "[" WS-PRETTY "]"
STOP RUN.[COBOL ]
[00042]
[+042]
[-007.50]
[ 1,234.50]
WITH NO ADVANCING
Normally each DISPLAY ends the line. WITH NO ADVANCING keeps the cursor on the same line, so the next
DISPLAY continues there:
IDENTIFICATION DIVISION.
PROGRAM-ID. NO-ADVANCE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-I PIC 9 VALUE 0.
PROCEDURE DIVISION.
DISPLAY "Counting: " WITH NO ADVANCING
PERFORM 5 TIMES
ADD 1 TO WS-I
DISPLAY WS-I " " WITH NO ADVANCING
END-PERFORM
DISPLAY "done"
STOP RUN.Counting: 1 2 3 4 5 done
Displaying computed values
DISPLAY accepts intrinsic functions directly. FUNCTION TRIM removes leading and trailing spaces, which is
the usual cure for padded text fields:
IDENTIFICATION DIVISION.
PROGRAM-ID. DISPLAY-FUNCS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-FIRST PIC X(10) VALUE "Ada".
01 WS-LAST PIC X(10) VALUE "Lovelace".
PROCEDURE DIVISION.
DISPLAY "Padded: " WS-FIRST WS-LAST "!"
DISPLAY "Trimmed: " FUNCTION TRIM(WS-FIRST) " "
FUNCTION TRIM(WS-LAST) "!"
DISPLAY "Upper: " FUNCTION UPPER-CASE(WS-LAST)
STOP RUN.Padded: Ada Lovelace !
Trimmed: Ada Lovelace!
Upper: LOVELACE
UPON: choosing the device
UPON sends the output somewhere other than standard output. Common choices in GnuCOBOL are SYSOUT (standard
output, the default), SYSERR (standard error) and CONSOLE. On a mainframe, UPON CONSOLE writes to the operator
console, so don't use it casually there.
DISPLAY "Report finished" UPON SYSOUT
DISPLAY "Warning: 3 rows skipped" UPON SYSERRNote: The opposite of
DISPLAYisACCEPT, which reads a line from the keyboard (ACCEPT WS-NAME) or fetches system values (ACCEPT WS-TODAY FROM DATE YYYYMMDD). The playground has no keyboard input, so this course sets values in the program instead. On a terminal,ACCEPTwaits for you to type a line.
01 WS-NAME PIC X(20).
01 WS-TODAY PIC 9(8).
*> ...
DISPLAY "Your name? " WITH NO ADVANCING
ACCEPT WS-NAME
ACCEPT WS-TODAY FROM DATE YYYYMMDD
DISPLAY "Hello " FUNCTION TRIM(WS-NAME)
", today is " WS-TODAYDebugging with DISPLAY
Most COBOL debugging still happens by displaying values. A helpful habit is to label each value and wrap text in brackets so trailing spaces become visible:
IDENTIFICATION DIVISION.
PROGRAM-ID. DEBUG-STYLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-ACCOUNT PIC X(10) VALUE "AC-77".
01 WS-AMOUNT PIC S9(5)V99 VALUE 120.
PROCEDURE DIVISION.
DISPLAY "DEBUG account=[" WS-ACCOUNT "] amount=" WS-AMOUNT
STOP RUN.DEBUG account=[AC-77 ] amount=+00120.00
Test yourself
What does DISPLAY print for PIC S9(3) VALUE 42?
- 42
- 042
- +042
- 42
Which phrase keeps the next output on the same line?
- NO NEWLINE
- WITH NO ADVANCING
- UPON LINE
- CONTINUE
How do you print a PIC X(10) name without its trailing spaces?
- DISPLAY WS-NAME TRIMMED
- DISPLAY FUNCTION TRIM(WS-NAME)
- DISPLAY WS-NAME(1:)
- You cannot