COBOL/Variables

Variables in COBOLEdit

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 must be described in terms of their size and type. Every variable used in a COBOL program should have a declaration in the DATA DIVISION.

  • Variables should be declared in the WORKING-STORAGE section. Variables can be arrays, records, file descriptor records, etc.
  • The name of a variable 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 variables in this example program are FAHRENHEIT and CELSIUS. Their size and type are declared by the PICTURE clause, described below.

variables.cblEdit

*> 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    PIC 999V99.
01 CELSIUS       PIC ZZ9.99.

PROCEDURE DIVISION.

DISPLAY "Enter Fahrenheit temperature:".
ACCEPT FAHRENHEIT.

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

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

The Picture ClauseEdit

While most of the syntax in this example should be apparent, the PIC specifications are likely to be cryptic. In COBOL, PIC is short for “Picture” and it provides the conversion specifications for interpreting input or displaying output. This 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 ItEdit

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

Math WordsEdit

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

AssignmentEdit

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 AlsoEdit

ReferencesEdit

  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