Teach Yourself COBOL in 21 days, Second Edition

Previous chapterNext chapterContents


- E -
Summary of COBOL Syntax

This appendix presents a working summary of the syntax for various commands covered in this book. This summary is neither the whole syntax for a command nor is it all the commands supported by COBOL; however, it is enough to work with the command. For a full description of the syntax, consult the manual for your COBOL compiler.

Defining General Terms in Syntax Listings

Syntax Term Description
... (ellipsis) Indicates that the preceding item might be repeated a number of times.
[ ] (brackets) An optional clause in the syntax.
| (vertical bar) Two or more words separated by a vertical bar indicates that one of the values can be used.
alphanumeric variable An alphanumeric variable defined in WORKING-STORAGE
arithmetic expression An expression representing one or more addition, subtraction, multiplication, or division operations involving numeric values.
condition An expression representing one or more comparisons between values optionally joined by AND or OR.
constant For alphanumeric values, a set of characters enclosed in quotes, such as "ABC". For numeric values, an expression representing a number, such as 1296.54.
do something Any COBOL command.
file-field-name A COBOL variable that is a field in a record of a file described in an FD or SD (sort descriptor).
file-name The name of a file described in an FD.
file-record A COBOL variable that is the record of a file described in an FD.
index A variable defined in WORKING-STORAGE used as an index for a table or array.
mode One of the four open modes for a file: OUTPUT, EXTEND, I-O, or INPUT.
numeric value A numeric constant or a numeric variable.
numeric variable A numeric variable defined in WORKING-STORAGE.
paragraph A paragraph name in the COBOL program.
procedure A paragraph or section.
program-name A COBOL program name.
sort-file The name of a file described in an SD.
value A constant or a variable.
variable A variable defined in WORKING-STORAGE.

Command Syntax

The following syntax might not exactly match your chosen compiler. You should consult the COBOL reference manual distributed with your compiler.

ACCEPT

Syntax:

ACCEPT variable
   [WITH CONVERSION]
ACCEPT variable
   [CONVERT]

Some versions of COBOL do not use the WITH CONVERSION or CONVERT option.

Examples:

ACCEPT NUMERIC-ENTRY-FIELD
   WITH CONVERSION
ACCEPT ENTRY-FIELD

ADD

Syntax:

ADD numeric value TO
    numeric value
    GIVING variable [ROUNDED]
    [ ON SIZE ERROR
      do something ]
ADD numeric value TO
    numeric variable [ROUNDED]
    [ ON SIZE ERROR
    do something ]

Examples:

ADD 17.5 TO THE-VALUE
    GIVING THE-SUM ROUNDED
    ON SIZE ERROR
    DISPLAY "Add - overflow"

ADD 1.17 TO THE-VALUE ROUNDED
    ON SIZE ERROR
    DISPLAY "Add - overflow"

BEGIN TRANSACTION

Syntax:

BEGIN TRANSACTION
REWRITE file-record
    [ INVALID KEY
      do something ]
[ REWRITE file-record
    [ INVALID KEY
      do something ] ]

COMMIT|ROLLBACK TRANSACTION

Examples:

BEGIN TRANSACTION
REWRITE VENDOR-RECORD
    INVALID KEY
    MOVE "E" TO TRANSACTION-FLAG

WRITE VENDOR-LOG-RECORD
    INVALID KEY
    MOVE "E" TO TRANSACTION-FLAG

IF TRANSACTION-FLAG = "E"
    ROLLBACK TRANSACTION

ELSE
    COMMIT TRANSACTION

CALL

Syntax:

CALL "program-name"

CALL "program-name"

    USING variable . . .

Examples:

CALL "VNDRPT03"
CALL "MYPROG"

   USING NUMERIC-DATA, DATE-FIELD.

CLOSE

Syntax:

CLOSE file-name

Examples:

CLOSE PHONE-FILE

COMPUTE

Syntax:

COMPUTE numeric variable
    [ROUNDED] =
    arithmetic expression
    [ ON SIZE ERROR
      do something else ]

Examples:

COMPUTE THE-RESULT ROUNDED =
  (BASE-VALUE * 10) +
  (A-VALUE / 50)
  ON SIZE ERROR

  DISPLAY "Warning Size error."

COPY

Syntax:

COPY "filename.ext".

Examples:

COPY "FDVENDOR.CBL".

DELETE

Syntax:

DELETE file-name RECORD
    [ INVALID KEY
      so something ]

Examples:

DELETE VOUCHER-FILE RECORD
  INVALID KEY
   DISPLAY "ERROR DELETING"

DISPLAY

Syntax:

DISPLAY value

Examples:

DISPLAY "HELLO WORLD".
DISPLAY ERROR-MESSAGE.

DIVIDE

Syntax:

DIVIDE value
    INTO variable [ROUNDED]
    [ ON SIZE ERROR

      do something ]

DIVIDE value BY value
    GIVING variable [ROUNDED]
    [ ON SIZE ERROR

      do something ]

DIVIDE value BY value
    GIVING variable [ROUNDED]
    REMAINDER variable
    [ ON SIZE ERROR

      do something ]

DIVIDE value INTO value
    GIVING variable [ROUNDED]
    [ ON SIZE ERROR

      do something ]

DIVIDE value INTO value
    GIVING variable [ROUNDED]
    REMAINDER variable
    [ ON SIZE ERROR

      do something ]

Examples:

DIVIDE 56.2
  INTO THE-VALUE ROUNDED
  ON SIZE ERROR

  DISPLAY "Divide-error"

DIVIDE 56.2 BY THE-VALUE
  GIVING THE-QUOTIENT ROUNDED
  ON SIZE ERROR

  DISPLAY "Divide-error"

DIVIDE 15 BY 7
  GIVING THE-QUOTIENT ROUNDED
  REMAINDER THE-REMAINDER
  ON SIZE ERROR

  DISPLAY "Divide-error"

DIVIDE 56.2 INTO THE-VALUE
  GIVING THE-QUOTIENT ROUNDED
  ON SIZE ERROR

  DISPLAY "Divide-error"

DIVIDE 15 INTO THE-VALUE
  GIVING THE-QUOTIENT ROUNDED
  REMAINDER THE-REMAINDER
  ON SIZE ERROR

  DISPLAY "Divide-error"

EXIT

Syntax:

EXIT PROGRAM

Examples:

PROGRAM-EXIT.
    EXIT PROGRAM.

GO TO

Syntax:

GO TO paragraph

Examples:

GO TO PROGRAM-EXIT.

IF ELSE

Syntax:

IF condition
    do something
    do something
ELSE
    do something

Examples:

IF RECORD-FOUND = "N"
    DISPLAY "RECORD NOT FOUND"
    MOVE "ERROR" TO ERROR-CODE
ELSE
    PERFORM DISPLAY-RECORD

INSPECT

Syntax:

INSPECT variable
  CONVERTING compare list
  TO         replace list

Examples:

INSPECT ENTRY-FIELD
  CONVERTING LOWER-ALPHA
  TO         UPPER-ALPHA

MOVE

Syntax:

MOVE value TO variable
             [variable . . .]

Examples:

MOVE 19 TO THE-VALUE
           ANOTHER-VALUE
           YET-ANOTHER-VALUE

MULTIPLY

Syntax:

MULTIPLY value
    BY variable [ROUNDED]
    [ ON SIZE ERROR

    do something ]
MULTIPLY value BY value
    GIVING variable [ROUNDED]
    [ ON SIZE ERROR

      do something ]

Examples:

MULTIPLY 1.17
  BY THE-VALUE ROUNDED
  ON SIZE ERROR
  DISPLAY "Multiply-overflow"

MULTIPLY 17.5 BY THE-VALUE
  GIVING THE-PRODUCT ROUNDED
  ON SIZE ERROR

  DISPLAY "Multiply-overflow"

OPEN

Syntax:

OPEN mode file-name

Examples:

OPEN OUTPUT PHONE-FILE
OPEN EXTEND PHONE-FILE
OPEN INPUT PHONE-FILE
OPEN I-O PHONE-FILE

PERFORM

Syntax:

PERFORM paragraph
    VARYING variable
    FROM value
    BY   value
    UNTIL condition

PERFORM paragraph

PERFORM paragraph

    value TIMES

PERFORM paragraph

    UNTIL condition

Examples:

PERFORM CALCULATE-AND-DISPLAY
    VARYING THE-MULTIPLIER
    FROM INITIAL-VALUE
    BY    1

    UNTIL THE-MULTIPLIER > 12.

PERFORM CALCULATE-AND-DISPLAY

PERFORM CALCULATE-AND-DISPLAY

    12 TIMES

PERFORM CALCULATE-AND-DISPLAY

    THE-COUNT TIMES

PERFORM CALCULATE-AND-DISPLAY

    UNTIL THE-MULTIPLIER > 12

READ

Syntax:

READ file-name [NEXT] RECORD
    AT END
     do something

For a SEQUENTIAL file, READ and READ NEXT RECORD are identical:

READ file-name RECORD
    INVALID KEY
     do something

Examples:

READ PHONE-FILE NEXT RECORD
    AT END
     MOVE "Y" TO END-OF-FILE
READ VENDOR-FILE RECORD
    INVALID KEY
     MOVE "N" TO RECORD-FOUND.

REWRITE

Syntax:

REWRITE file-record
    [ INVALID KEY
      do something ]

Examples:

REWRITE VENDOR-RECORD
    INVALID KEY
    DISPLAY "REWRITE ERROR"

SEARCH

Syntax:

SEARCH table name
  [AT END
    do something ]
  WHEN condition

   do something

Examples:

SET STATE-INDEX TO 1.
SEARCH TABLE-STATE-RECORD
 AT END
  PERFORM SEARCH-FAILED
 WHEN
  VENDOR-STATE =
  TABLE-STATE-CODE(STATE-INDEX)
   PERFORM SEARCH-SUCCEEDED

SET

Syntax:

SET index
  UP|DOWN BY value
SET index variable TO value

Examples:

SET STATE-INDEX UP BY 1.

SET STATE-INDEX
  DOWN BY LAST-VALUE.
SET STATE-INDEX TO 1
SET STATE-INDEX TO LAST-VALUE.

SORT

Syntax:

SORT sort-file
  ON ASCENDING KEY sort-field
  USING input-file
  GIVING output-file

Examples:

SORT SORT-FILE
  ON ASCENDING KEY SORT-DATE
  USING VOUCHER-FILE
  GIVING WORK-FILE.

START

Syntax:

START file name
 KEY NOT < key name
  [ INVALID KEY
     do something ]
START file name
 KEY > key name
  [ INVALID KEY

     do something ]

START file name
 KEY EQUALS|NOT <|GREATER THAN
  key name
  [ INVALID KEY

     do something ]

Examples:

MOVE SPACE TO VENDOR-NAME.
START VENDOR-FILE
  KEY NOT < VENDOR-NAME
   INVALID KEY
    MOVE "Y" TO FILE-AT-END.
MOVE "AZZZZZZZZZZZZZZZZZZZ"
   TO VENDOR-NAME.
START VENDOR-FILE
  KEY > VENDOR-NAME
   INVALID KEY

    MOVE "Y" TO FILE-AT-END.
MOVE "JONES AND SONS"
   TO VENDOR-NAME.
START VENDOR-FILE
  KEY EQUALS VENDOR-NAME
   INVALID KEY

    MOVE "N" TO RECORD-FOUND.

MOVE SPACE
   TO VENDOR-NAME.
START VENDOR-FILE
  KEY NOT < VENDOR-NAME
   INVALID KEY

    MOVE "N" TO RECORD-FOUND.

SUBTRACT

Syntax:

SUBTRACT value
    FROM variable [ROUNDED]
    [ ON SIZE ERROR

    do something ]

SUBTRACT value FROM value
    GIVING variable [ROUNDED]
    [ ON SIZE ERROR
      do something ]

Examples:

SUBTRACT  1.17
  FROM THE-VALUE ROUNDED
  ON SIZE ERROR

  DISPLAY "Subtract - overflow"

SUBTRACT 17.5 FROM THE-VALUE
  GIVING THE-SUM ROUNDED
  ON SIZE ERROR

  DISPLAY "Subtract-overflow"

WRITE

Syntax:

WRITE file-record
    [FROM variable]
    [ INVALID KEY
      do something]

WRITE file-record
    AFTER|BEFORE
    ADVANCING

    PAGE|value LINES

Examples:

WRITE PHONE-RECORD
WRITE PHONE-RECORD
    FROM WORK-DATA
WRITE VENDOR-RECORD
   INVALID KEY
    MOVE "Y" TO DUPLICATE-FLAG

WRITE PRINTER-RECORD
  BEFORE ADVANCING 1 LINE
WRITE PRINTER-RECORD

  AFTER ADVANCING PAGE


Previous chapterNext chapterContents


Macmillan Computer Publishing USA

© Copyright, Macmillan Computer Publishing. All rights reserved.