Your First COBOL Program

Save as hello.cob (note: each line starts with 7 spaces; statements with 11), then:

4.1 Hello, COBOL!

Save as hello.cob (note: each line starts with 7 spaces; statements with 11), then:

IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.
       PROCEDURE DIVISION.
           DISPLAY "Hello, COBOL!".
           STOP RUN.
cobc -x hello.cob
./hello
# Hello, COBOL!

4.2 A program with data and input

DATA DIVISION / WORKING-STORAGE SECTION declares variables (data items).

PIC X(30) = 30 alphanumeric characters; PIC 9(3) = 3-digit unsigned number; PIC ZZ9 = an edited number with leading zeros suppressed.

MAIN-PARA. is a paragraph name; the statements below it form the paragraph.

Lines with * in column 7 are comments.

IDENTIFICATION DIVISION.
       PROGRAM-ID. GREET.
       AUTHOR. STUDENT.
      *----------------------------------------------------------
      * Ask for a name and an age, then answer politely.
      *----------------------------------------------------------
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-NAME              PIC X(30).
       01  WS-AGE               PIC 9(3).
       01  WS-NEXT-AGE          PIC 9(3).
       01  WS-NEXT-AGE-OUT      PIC ZZ9.

       PROCEDURE DIVISION.
       MAIN-PARA.
           DISPLAY "What is your name? " WITH NO ADVANCING
           ACCEPT WS-NAME
           DISPLAY "How old are you? " WITH NO ADVANCING
           ACCEPT WS-AGE
           ADD 1 TO WS-AGE GIVING WS-NEXT-AGE
           MOVE WS-NEXT-AGE TO WS-NEXT-AGE-OUT
           DISPLAY "Hello, " FUNCTION TRIM(WS-NAME)
                   "! Next year you will be "
                   FUNCTION TRIM(WS-NEXT-AGE-OUT) "."
           STOP RUN.

4.3 The same program on IBM z/OS

On the mainframe, you store the source as a member of a partitioned dataset (e.g., USER01.COBOL.SOURCE(GREET)), then compile, link, and run it with JCL using the IBM-supplied IGYWCLG procedure (Chapter 35). There, ACCEPT reads from the SYSIN DD statement and DISPLAY writes to SYSOUT.