Values in COBOL

COBOL has various value types including alphanumeric (strings), numeric (integers and decimals), and boolean. Here are a few basic examples.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. VALUES-EXAMPLE.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-STRING-1      PIC X(2) VALUE "CO".
       01 WS-STRING-2      PIC X(3) VALUE "BOL".
       01 WS-RESULT        PIC X(5).
       01 WS-NUM-1         PIC 9(2) VALUE 1.
       01 WS-NUM-2         PIC 9(2) VALUE 1.
       01 WS-SUM           PIC 9(2).
       01 WS-DECIMAL-1     PIC 9(2)V9(2) VALUE 7.00.
       01 WS-DECIMAL-2     PIC 9(2)V9(2) VALUE 3.00.
       01 WS-QUOTIENT      PIC 9(2)V9(2).
       01 WS-BOOL-1        PIC X VALUE 'T'.
       01 WS-BOOL-2        PIC X VALUE 'F'.
       
       PROCEDURE DIVISION.
           MAIN-PROCEDURE.
      * Strings, which can be concatenated
           STRING WS-STRING-1 WS-STRING-2
               DELIMITED BY SIZE
               INTO WS-RESULT
           END-STRING
           DISPLAY WS-RESULT
      
      * Integers
           ADD WS-NUM-1 TO WS-NUM-2 GIVING WS-SUM
           DISPLAY "1+1 = " WS-SUM
      
      * Decimals
           DIVIDE WS-DECIMAL-1 BY WS-DECIMAL-2 GIVING WS-QUOTIENT
           DISPLAY "7.00/3.00 = " WS-QUOTIENT
      
      * Booleans, with logical operators
           IF WS-BOOL-1 = 'T' AND WS-BOOL-2 = 'T'
               DISPLAY "TRUE"
           ELSE
               DISPLAY "FALSE"
           END-IF
      
           IF WS-BOOL-1 = 'T' OR WS-BOOL-2 = 'T'
               DISPLAY "TRUE"
           ELSE
               DISPLAY "FALSE"
           END-IF
      
           IF WS-BOOL-1 = 'F'
               DISPLAY "TRUE"
           ELSE
               DISPLAY "FALSE"
           END-IF
      
           STOP RUN.

To run the program, compile the COBOL code and execute the resulting program:

$ cobc -x values-example.cob
$ ./values-example
COBOL
1+1 = 02
7.00/3.00 = 2.33
FALSE
TRUE
FALSE

In this COBOL example:

  1. We define variables in the WORKING-STORAGE SECTION.
  2. Strings are concatenated using the STRING verb.
  3. Integer addition is performed using the ADD verb.
  4. Decimal division is done with the DIVIDE verb.
  5. Boolean operations are implemented using IF-ELSE statements.

Note that COBOL doesn’t have built-in boolean types, so we use ‘T’ for true and ‘F’ for false. Also, COBOL uses fixed-point decimal arithmetic, which is why the division result is more precise than floating-point arithmetic would be.