codegurukool.com · Free coding tutorials & online compiler

Home › COBOL Tutorial › All commands

All commands

Every command of this course: name, detailed description, syntax and example code.

Every command used in this course, with its name, a detailed description, the syntax and an example.

IDENTIFICATION DIVISION

Definition: The first division. It names the program with PROGRAM-ID.

Syntax:

IDENTIFICATION DIVISION.
PROGRAM-ID. name.

Example:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

PROCEDURE DIVISION

Definition: The division that holds the instructions the program runs.

Syntax:

PROCEDURE DIVISION.
    statements.

Example:

PROCEDURE DIVISION.
    DISPLAY "Hi".

WORKING-STORAGE SECTION

Definition: The part of the data division where you declare ordinary variables.

Syntax:

WORKING-STORAGE SECTION.
01 name PIC type.

Example:

WORKING-STORAGE SECTION.
01 WS-AGE PIC 99.

PIC

Definition: The picture clause gives a data item its type and size: X for text, 9 for digits, V for an implied decimal point.

Syntax:

01 name PIC X(n).
01 name PIC 9(n)V99.

Example:

01 WS-NAME PIC X(10).
01 WS-PRICE PIC 9(3)V99.

VALUE

Definition: Gives a data item its starting value.

Syntax:

01 name PIC type VALUE literal.

Example:

01 WS-AGE PIC 99 VALUE 36.

DISPLAY

Definition: Prints literals and variables to the screen.

Syntax:

DISPLAY item item ...

Example:

DISPLAY "Total: " WS-TOTAL.

ACCEPT

Definition: Reads a value typed by the user into a variable.

Syntax:

ACCEPT variable.

Example:

ACCEPT WS-NAME.

MOVE

Definition: Copies a value into one or more variables.

Syntax:

MOVE source TO target.

Example:

MOVE 10 TO WS-AGE.

ADD

Definition: Adds numbers. GIVING stores the result in a different field.

Syntax:

ADD a TO b.
ADD a TO b GIVING c.

Example:

ADD 5 TO WS-TOTAL.

COMPUTE

Definition: Calculates a formula with + - * / and **. Add ROUNDED to round the result.

Syntax:

COMPUTE result = formula.

Example:

COMPUTE R = (X + Y) * 2.

IF

Definition: Runs statements only when a condition is true. ELSE covers the other case and END-IF closes it.

Syntax:

IF condition
    ...
ELSE
    ...
END-IF.

Example:

IF SCORE >= 60
    DISPLAY "pass"
END-IF.

EVALUATE

Definition: Chooses between many WHEN cases, like a switch statement.

Syntax:

EVALUATE value
    WHEN x ...
    WHEN OTHER ...
END-EVALUATE.

Example:

EVALUATE DAY-NUM
    WHEN 1 DISPLAY "Mon"
    WHEN OTHER DISPLAY "Other"
END-EVALUATE.

PERFORM

Definition: Runs a paragraph or a block of statements, once or repeatedly (TIMES, UNTIL, VARYING).

Syntax:

PERFORM n TIMES ... END-PERFORM.
PERFORM paragraph-name.

Example:

PERFORM 3 TIMES
    DISPLAY "Hi"
END-PERFORM.

OCCURS

Definition: Repeats a data item to make a table. Subscripts start at 1.

Syntax:

05 name PIC type OCCURS n TIMES.

Example:

05 NUM PIC 99 OCCURS 3 TIMES.

STRING

Definition: Joins several items into one field. DELIMITED BY says where each piece stops.

Syntax:

STRING a DELIMITED BY SPACE
       b DELIMITED BY SIZE
       INTO target.

Example:

STRING FIRST-NAME DELIMITED BY SPACE
       LAST-NAME DELIMITED BY SPACE
       INTO FULL-NAME.

OPEN / CLOSE

Definition: OPEN prepares a file for INPUT, OUTPUT or EXTEND; CLOSE finishes with it.

Syntax:

OPEN INPUT file-name.
CLOSE file-name.

Example:

OPEN OUTPUT NOTES-FILE.
CLOSE NOTES-FILE.

STOP RUN

Definition: Ends the program immediately.

Syntax:

STOP RUN.

Example:

STOP RUN.

cobc

Definition: Install the free GnuCOBOL compiler ( cobc ).

Syntax:

cobc

Example:

*>   cobc -x hello.cob

↗ COBOL get started

hello.cob

Definition: Save your program as hello.cob , build it with cobc -x hello.cob and run ./hello .

Syntax:

hello.cob

Example:

*>   cobc -x hello.cob

↗ COBOL get started

SPACE

Definition: SPACE is a figurative constant for a blank.

Syntax:

SPACE

Example:

DISPLAY "A" SPACE "B".

↗ COBOL DISPLAY

UNSTRING

Definition: UNSTRING is the opposite of STRING : it splits a field at a delimiter, such as a comma, into several receiving fields.

Syntax:

UNSTRING

Example:

UNSTRING LINE-IN DELIMITED BY ","

↗ COBOL UNSTRING

SUBTRACT

Definition: COBOL has English-like verbs: ADD a TO b GIVING c , SUBTRACT , MULTIPLY and DIVIDE .

Syntax:

SUBTRACT

Example:

SUBTRACT Y FROM X GIVING R.

↗ COBOL arithmetic

MULTIPLY

Definition: COBOL has English-like verbs: ADD a TO b GIVING c , SUBTRACT , MULTIPLY and DIVIDE .

Syntax:

MULTIPLY

Example:

MULTIPLY X BY Y GIVING R.

↗ COBOL arithmetic

DIVIDE

Definition: COBOL has English-like verbs: ADD a TO b GIVING c , SUBTRACT , MULTIPLY and DIVIDE .

Syntax:

DIVIDE

Example:

DIVIDE X BY Y GIVING R REMAINDER Z.

↗ COBOL arithmetic

ROUNDED

Definition: Add ROUNDED to round to the nearest value.

Syntax:

ROUNDED

Example:

COMPUTE N ROUNDED = 7 / 2.

↗ COBOL ROUNDED and SIZE ERROR

END-COMPUTE

Definition: End the statement with END-COMPUTE .

Syntax:

END-COMPUTE

Example:

END-COMPUTE.

↗ COBOL ROUNDED and SIZE ERROR

.

Definition: An edited picture formats a number when you MOVE it: Z hides leading zeros, , and .

Syntax:

.

Example:

DISPLAY FUNCTION TRIM(ED).

↗ COBOL edited pictures

AND

Definition: Conditions use = , > , < , >= , <= , NOT , AND and OR .

Syntax:

AND

Example:

IF SCORE >= 60 AND SCORE <= 100

↗ COBOL IF...ELSE

END-IF

Definition: END-IF .

Syntax:

END-IF

Example:

END-IF
END-IF.

↗ COBOL IF...ELSE

WHEN

Definition: EVALUATE compares a value with several WHEN choices.

Syntax:

WHEN

Example:

WHEN 1 DISPLAY "Mon"
WHEN 2 DISPLAY "Tue"
WHEN 3 DISPLAY "Wed"

↗ COBOL EVALUATE

END-PERFORM

Definition: End inline loops with END-PERFORM .

Syntax:

END-PERFORM

Example:

END-PERFORM.

↗ COBOL PERFORM loops

NUM(2)

Definition: Read an element with a subscript in brackets, such as NUM(2) .

Syntax:

NUM(2)

Example:

05 NUM PIC 99 OCCURS 3 TIMES.
MOVE 10 TO NUM(1).
MOVE 20 TO NUM(2).

↗ COBOL tables (arrays)

FUNCTION

Definition: Call built-in functions with the word FUNCTION : UPPER-CASE , LOWER-CASE , REVERSE , TRIM , LENGTH , MAX , MIN , SQRT .

Syntax:

FUNCTION

Example:

DISPLAY FUNCTION TRIM(FUNCTION UPPER-CASE(TXT)).
MOVE FUNCTION LENGTH(FUNCTION TRIM(TXT)) TO N.
MOVE FUNCTION MAX(3 9 4) TO N.

↗ COBOL intrinsic functions

UPPER-CASE

Definition: Call built-in functions with the word FUNCTION : UPPER-CASE , LOWER-CASE , REVERSE , TRIM , LENGTH , MAX , MIN , SQRT .

Syntax:

UPPER-CASE

Example:

DISPLAY FUNCTION TRIM(FUNCTION UPPER-CASE(TXT)).

↗ COBOL intrinsic functions

REVERSE

Definition: Call built-in functions with the word FUNCTION : UPPER-CASE , LOWER-CASE , REVERSE , TRIM , LENGTH , MAX , MIN , SQRT .

Syntax:

REVERSE

Example:

DISPLAY FUNCTION TRIM(FUNCTION REVERSE(FUNCTION TRIM(TXT))).

↗ COBOL intrinsic functions

TRIM

Definition: Call built-in functions with the word FUNCTION : UPPER-CASE , LOWER-CASE , REVERSE , TRIM , LENGTH , MAX , MIN , SQRT .

Syntax:

TRIM

Example:

DISPLAY FUNCTION TRIM(FUNCTION UPPER-CASE(TXT)).
MOVE FUNCTION LENGTH(FUNCTION TRIM(TXT)) TO N.
DISPLAY FUNCTION TRIM(FUNCTION REVERSE(FUNCTION TRIM(TXT))).

↗ COBOL intrinsic functions

LENGTH

Definition: Call built-in functions with the word FUNCTION : UPPER-CASE , LOWER-CASE , REVERSE , TRIM , LENGTH , MAX , MIN , SQRT .

Syntax:

LENGTH

Example:

MOVE FUNCTION LENGTH(FUNCTION TRIM(TXT)) TO N.

↗ COBOL intrinsic functions

MAX

Definition: Call built-in functions with the word FUNCTION : UPPER-CASE , LOWER-CASE , REVERSE , TRIM , LENGTH , MAX , MIN , SQRT .

Syntax:

MAX

Example:

MOVE FUNCTION MAX(3 9 4) TO N.

↗ COBOL intrinsic functions

SQRT

Definition: Call built-in functions with the word FUNCTION : UPPER-CASE , LOWER-CASE , REVERSE , TRIM , LENGTH , MAX , MIN , SQRT .

Syntax:

SQRT

Example:

MOVE FUNCTION SQRT(16) TO N.

↗ COBOL intrinsic functions

FD

Definition: ASSIGN TO and described in the FILE SECTION with FD .

Syntax:

FD

Example:

FD NOTES-FILE.

↗ COBOL file handling

OPEN

Definition: Use OPEN , WRITE , READ ...

Syntax:

OPEN

Example:

OPEN OUTPUT NOTES-FILE.
OPEN INPUT NOTES-FILE.

↗ COBOL file handling

WRITE

Definition: Use OPEN , WRITE , READ ...

Syntax:

WRITE

Example:

WRITE NOTE-REC.

↗ COBOL file handling

CLOSE

Definition: AT END and CLOSE .

Syntax:

CLOSE

Example:

CLOSE NOTES-FILE.

↗ COBOL file handling

Example code

       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.
       PROCEDURE DIVISION.
           DISPLAY "Hello, COBOL!".
           STOP RUN.

Run this code in the online editor

← Welcome to the COBOL courseCOBOL introduction →