COBOL/Variables

From Wikiversity
Jump to navigation Jump to search

Variables in COBOL[edit | edit source]

In computer science, a variable is a named memory location where a program can store or retrieve data. The variable name is used to identify the memory location.

In COBOL, variables (known as "data items") must be described in terms of their size and type. Every data item used in a COBOL program needs to have been declared with a description entry in the data division.

  • Data items in a main program are declared in the working-storage section. They may be tables, records, file descriptor records, etc.
  • The name of a data item may use alphanumeric characters, with its first character alphabetic.

The Variables lesson in the Wikiversity course Programming Fundamentals provides additional background, readings, multimedia resources, key terms, and activities related to understanding and using variables.

Please study those materials to better understand and extend the following example program.

The data items in this example program are FAHRENHEIT and CELSIUS. Their size and type are declared by the PICTURE clause, described below.

variables.cbl[edit | edit source]

      *> This program converts a Fahrenheit temperature to Celsius.
      *>
      *> References:
      *>     https://www.mathsisfun.com/temperature-conversion.html
      *>     https://www.tutorialspoint.com/cobol/index.htm
      *>     https://open-cobol.sourceforge.io/doc/gnucobol.html

       IDENTIFICATION DIVISION.
       PROGRAM-ID. VARIABLES.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 FAHRENHEIT   PICTURE IS 999V99.
       01 CELSIUS      PICTURE IS ZZ9.99.

       PROCEDURE DIVISION.

           DISPLAY "Enter Fahrenheit temperature:".
           ACCEPT FAHRENHEIT.

           COMPUTE CELSIUS = (FAHRENHEIT - 32) * 5 / 9.

           DISPLAY FAHRENHEIT "° Fahrenheit is " CELSIUS "° Celsius".

           STOP RUN.

       END PROGRAM VARIABLES.

The PICTURE Clause[edit | edit source]

While most of the syntax in this example should be apparent, the PICTURE specifications are likely to be cryptic. In COBOL, PICTURE (often shortened to "PIC") describes the size and format of the data item. In doing so it also provides the conversion specifications for interpreting input or displaying output, is analogous to the Fortran Format statement, or the printf format string used in C and other languages.

The PICTURE clause is used to define the following items:[1][2]

  • Data type can be numeric, alphabetic, or alphanumeric. Numeric type consists of only digits 0 to 9. Alphabetic type consists of letters A to Z and spaces. Alphanumeric type consists of digits, letters, and special characters.
  • Sign can be used with numeric data. It can be either + or –.
  • Decimal point position can be used with numeric data. The assumed decimal position is the position assigned to the decimal point and is not included in the data.
  • Length defines the number of bytes used by the data item.

These symbols are used in a PICTURE clause:

  • 9 Numeric
  • A Alphabetic
  • X Alphanumeric
  • V Implicit Decimal
  • S Sign (+ or -)
  • P Assumed Decimal

Try It[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own Variables compiler / interpreter / IDE.

Math Words[edit | edit source]

One of the original goals of COBOL was to make the language easy to read and write. As a result, one design decision was to use English language verbs instead of mathematical symbols in writing assignment statements. For example, this sample program includes the following assignment statement:

COMPUTE CELSIUS = (FAHRENHEIT - 32) * 5 / 9.

This statement uses the mathematical symbols =, -, *, and / to indicate assignment, subtraction, multiplication, and division, respectively. The COMPUTE verb allows use of these mathematical operators.

Alternatively, English language verbs can be used to specify mathematical operations. The correspondence is shown in the table below.[3]

Mathematical Operator English Language Verb
COMPUTE X = A + B ADD A TO B GIVING X
COMPUTE X = A - B SUBTRACT B FROM A GIVING X
COMPUTE X = A * B MULTIPLY A BY B GIVING C
COMPUTE X = A / B DIVIDE B INTO A GIVING X
COMPUTE X = A / B DIVIDE A BY B GIVING X
COMPUTE X = A / B DIVIDE A BY B GIVING X REMAINDER R

Assignment[edit | edit source]

Part 1:

Modify the sample program above, replacing the line

COMPUTE CELSIUS = (FAHRENHEIT - 32) * 5 / 9.

With code that uses the English language verb form of the assignment statement. Make any other modifications to the program needed to accommodate this form of the assignment statement.

Run and test the modified program.

Part 2:

Modify the sample program above to print out a single paycheck. It is OK to use string literals for each field, however the paycheck must include:

  • A check number
  • The date
  • Pay to the order of name
  • Amount in decimal dollars and cents.
  • The above amount spelled out in words

Run and test the modified program.

See Also[edit | edit source]

References[edit | edit source]

  1. TutorialsPoint, COBOL Data Types, https://www.tutorialspoint.com/cobol/cobol_data_types.htm
  2. GnuCOBOL Programmer’s Guide Guide 2.2 Section 5.9.33. PICTURE https://open-cobol.sourceforge.io/HTML/gnucobpg.html#PICTURE
  3. TutorialsPoint COBOL Basic Verbs https://www.tutorialspoint.com/cobol/cobol_basic_verbs.htm