DEV Community

Cover image for #12in24: Learning COBOL in 1h.
Alex Martínez
Alex Martínez

Posted on

#12in24: Learning COBOL in 1h.

The title is even more clickbaiting thean the previous article: #12in24: Learning Perl in 4h. But read me out: Yes, you can learn COBOL (Common Business-Oriented Languge) in just 1 hour!

I will use this video to learn the basics: COBOL Course - Programming with VSCode.

Why learning COBOL?

Well, COBOL has been around for over 60 years and is still used in many operations like card payments, in fact, everyday about 3 trillion dollars in finance gets handled by COBOL.

This language, Business-Oriented, was designed around data processing with high accuracy, efficiency, ease of reading and writing.

COBOL pretty much runs the world: It has been kept up to date to support new requirements and 1.5 billion lines of COBOL are written every year!

Learning this language will give you a very valuable set of skills that can be used in finance (the people that hold the money and will likely pay quite well), Big Data or Live Events Lighting (once I saw a lighting desk running programs in COBOL).

Basics

  • There are different COBOL flavours, this will be COBOL written specifically for IBM Z Mainframe OS.
  • COBOL is column dependent - 5 key areas of a 72 char line:

COBOL Areas

1. Sequence Number Area (1-6): Blank or to provide context.
2. Indicator Area (7): Multi-purpose area - * for comments or to continue a previous line...
3. A Area (8-11): Structure.
4. B Area (12-72): Actual statements area.
5. Id Area (73-80): Ignored, used for any purpose.

The full list of reserved words can be found here.

Structure

DIVISIONS > SECTIONS > PARAGRAPHS > SENTENCES > STATEMENTS

(tl;dw)

Basic variables

  • Basic rules apply: no reserved words, no weird chars or spaces (letters, numbers and dashes are fine) and no longer that 30 chars.
  • Type of variable and size using PIC clause: PIC M(N) where M is the value type and (N), optional, the length.

    - PIC 9(4): Number value with a length of 4 numbers. We can use V for decimals: PIC 9(2)V99 could be 24.59.
    - PIC A: Alphabetic value.
    - PIC X(8): Alphanumeric value with a length of 8 chars.

For the sake of efficiency you can as well use literals like: ZERO/ZEROES, SPACE/SPACES, LOW-VALUE, HIGH-VALUE, NULL/NULLS.

Divisions

DATA DIVISION & PROCEDURE DIVISION

DATA DIVISION is where all the program data goes. Then in PROCEDURE DIVISION MOVE and COMPUTE are used to alter the value of variables. A simple program to undertand COBOL so far:

1    678   12
|    | |   |
       IDENTIFICATION DIVISION.
       PROGRAM-ID. PAYROLL00.
       DATA DIVISION. 
       WORKING-STORAGE SECTION. 
      *VARIABLES
       77  FNAME       PIC X(15).
       77  SNAME       PIC X(15).
       77  RATE        PIC 9(3).
       77  HOURS       PIC 9(3).
       77  GROSS-PAY   PIC 9(5).

       PROCEDURE DIVISION.
      *MOVE STATEMENTS
           MOVE "Alex" TO FNAME.
           MOVE "Espinosa" TO SNAME.
           MOVE 100 TO RATE.
           MOVE 40 TO HOURS.
      *CALCULATE GROSS-PAY WITH COMPUTE     
           COMPUTE GROSS-PAY = HOURS * RATE.

      *DISPLAY
           DISPLAY "Name: " FNAME.
           DISPLAY "Surname: " SNAME.
           DISPLAY "Hour worked: " HOURS.
           DISPLAY "Hourly rate: " RATE.
           DISPLAY "Gross Pay: " GROSS-PAY.
Enter fullscreen mode Exit fullscreen mode

As you can see, it's pretty easy stuff: set variables in the DATA DIVISION and then we assign values to variables and we display the values in the PROCEDURE DIVISION.

Paragraphs

Paragraphs are chunks of code defined in the PROCEDURE DIVISION starting at column A and can have the name you like. The termination of a pragraph is a period and can contain one to many COBOL sentences.

Conditionals

1    678   12
|    | |   |
      *CONDITIONAL
           IF GROSS-PAY < 6000 THEN
              DISPLAY FNAME " is being underpaid!"
           ELSE DISPLAY FNAME "is being paid a fair salary."
           END-IF.
Enter fullscreen mode Exit fullscreen mode

We can also use EVALUATE and WHEN as if it was a switch statement:

1    678   12
|    | |   |
           EVALUATE GROSS-PAY 
              WHEN (GROSS-PAY < 6000)
                 DISPLAY FNAME " is being underpaid!"
              WHEN (GROSS-PAY >= 6000)
                 DISPLAY FNAME "is being paid a fair salary."
           END-EVALUATE. 
Enter fullscreen mode Exit fullscreen mode

Loops

As COBOL is pretty much like plain english I believe there is no need to explain this type of loop:

1    678   12
|    | |   |
           MOVE 'THE NUMBER IS: ' TO MSG-HEADER OF PRINT-REC.
           PERFORM VARYING COUNTER FROM 01 BY 1 UNTIL COUNTER EQUAL 11
           MOVE COUNTER TO MSG-TO-WRITE
           WRITE PRINT-REC
           END-PERFORM.

           CLOSE PRINT-LINE.
           STOP RUN.
Enter fullscreen mode Exit fullscreen mode

Program linkage

We use the CALL keyword to call another program:

CALL 'PROG1' ...
Enter fullscreen mode Exit fullscreen mode

Intrinsic functions

These are functions ready-made for us to use within our COBOL programs. There are 6 categories:

  1. MATH
  2. STATISTICS
  3. DATE/TIME
  4. FINANCIAL
  5. CHARACTER-HANDLING
  6. GENERAL

To use them we use FUNCTION NAME-OF-THE-FUNCTION(PARAMETERS).

Project

As COBOL is frequently used in finance, I decided to build a simple banking program. The user is able to add and withdraw money, check his balance and exit the program:

IDENTIFICATION DIVISION.
PROGRAM-ID. SIMPLEBANKING.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 ACCT-BALANCE         PIC 9(7)V99 VALUE 1000.00.
01 TRS-AMOUNT           PIC 9(7)V99.
01 CHOICE               PIC X.

PROCEDURE DIVISION.
MAIN-PROCEDURE.
    DISPLAY "Welcome to the Simple Banking Program".
    PERFORM UNTIL CHOICE = '4'
        DISPLAY "1. Deposit"
        DISPLAY "2. Withdraw"
        DISPLAY "3. Check Balance"
        DISPLAY "4. Exit"
        DISPLAY "Enter your choice (1-4): "
        ACCEPT CHOICE

        EVALUATE CHOICE
            WHEN '1'
                PERFORM DEPOSIT
            WHEN '2'
                PERFORM WITHDRAW
            WHEN '3'
                PERFORM CHECK-BALANCE
            WHEN '4'
                DISPLAY "Thank you for using the banking program. Goodbye!"
            WHEN OTHER
                DISPLAY "Invalid choice. Please enter again."
        END-EVALUATE
    END-PERFORM.

    STOP RUN.

DEPOSIT.
    DISPLAY "Enter the amount to deposit: "
    ACCEPT TRS-AMOUNT
    ADD TRS-AMOUNT TO ACCT-BALANCE
    DISPLAY "Amount deposited successfully."
    PERFORM CHECK-BALANCE.

WITHDRAW.
    DISPLAY "Enter the amount to withdraw: "
    ACCEPT TRS-AMOUNT
    IF TRS-AMOUNT <= ACCT-BALANCE
        SUBTRACT TRS-AMOUNT FROM ACCT-BALANCE
        DISPLAY "Amount withdrawn successfully."
    ELSE
        DISPLAY "Insufficient funds."
    END-IF
    PERFORM CHECK-BALANCE.

CHECK-BALANCE.
    DISPLAY "Your current balance is: ", ACCT-BALANCE.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)