Custom Search

Teach Yourself COBOL in 21 days Using Variables and Constants

Teach Yourself COBOL in 21 days

Using Variables and Constants

The DATA DIVISION is a critical subject to understand in COBOL. Today's lesson covers the basics, but you will be returning to the DATA DIVISION again and again throughout this book.

Today, you learn about the following topics:

· What is a constant?

· What is a variable?

· Defining numeric variables in COBOL.

· Naming variables in COBOL.

· More on using DISPLAY.

· Defining and using variables.

· Defining pictures.

· Using the MOVE verb.

What Is a Constant?

The data in COBOL programs falls into two broad categories: constants and variables.

New Term: A constant is a value that cannot be modified while the program is running.

You already have used constants in the hello.cbl program and examples in Day 1, "Your First COBOL Program." In Listing 1.1, the string "Hello world" is a constant.

In hello.cbl, there is no way to modify the display of "Hello world" without editing the program and recompiling it, as shown in Listing 2.1. This effectively creates a new program.

TYPE: Listing 2.1. Modifying a constant by editing and recompiling.

000100 IDENTIFICATION DIVISION.

000200 PROGRAM-ID. HELLO.

000300 ENVIRONMENT DIVISION.

000400 DATA DIVISION.

000500 PROCEDURE DIVISION.

000600

000700 PROGRAM-BEGIN.

000800 DISPLAY "I said, Hello world".

000900

001000 PROGRAM-DONE.

001100 STOP RUN.

If you want to display both messages, you have to use code similar to what you see in Listing 2.2; however, this merely uses two different constants in one program.


· Formatting output.

· Tips on layout and punctuation.

· Continuation characters.


TYPE: Listing 2.2. Adding another constant.

000100 IDENTIFICATION DIVISION.

000200 PROGRAM-ID. HELOHELO.

000300 ENVIRONMENT DIVISION.

000400 DATA DIVISION.

000500 PROCEDURE DIVISION.

000600

000700 PROGRAM-BEGIN.

000800 DISPLAY "Hello world".

000900 DISPLAY "I said, Hello world".

001000 PROGRAM-DONE.

001100 STOP RUN.

Numeric constants can be used in a similar way. Listing 2.3 includes examples of numeric constants (55 and 12.607). Note the difference between character constants, such as "Hello world", and numeric constants, such as 12.607. Character constants are enclosed in quotation marks. Numeric constants are not.

New Term: Character constants also are called string constants.

TYPE: Listing 2.3. String and numeric constants.

000100 IDENTIFICATION DIVISION.

000200 PROGRAM-ID. CONST.

000300 ENVIRONMENT DIVISION.

000400 DATA DIVISION.

000500 PROCEDURE DIVISION.

000600

000700 PROGRAM-BEGIN.

000800 DISPLAY "Hello world".

000900 DISPLAY 55.

001000 DISPLAY 12.607.

001100 PROGRAM-DONE.

001200 STOP RUN.

Character constants can contain spaces, such as the space between the words in "Hello world". Without the double quotation marks at each end, the compiler would have trouble recognizing all of the character constant; it would not know where the constant ended. The following is a classic example of this problem:

DISPLAY THIS IS THE DISPLAY.

DISPLAY is a COBOL verb. In this example, the first occurrence of the word DISPLAY is the COBOL verb for displaying information. The compiler could become confused as to whether the second occurrence of the word DISPLAY is part of the message to be displayed or is another DISPLAY verb. To keep the compiler from having a nervous breakdown, the programmer is required to write the following:

DISPLAY "THIS IS THE DISPLAY".

Every popular programming language includes a requirement that character constants be enclosed in some sort of quotation marks or other signaling characters to indicate the exact beginning and end of the character constant. COBOL uses double quotation marks at the beginning and end of the characters.

Numeric constants such as the following do not contain white space, and it is much easier for the compiler to recognize them as numbers:

DISPLAY 12.607.

Most popular languages do not require any special characters to signal a numeric constant.

New Term: White space is a general term to cover any blank characters. A space is white space, as is a tab, though you won't use tabs in COBOL programs. They are called white space because they print as white spaces on white paper when sent to a printer.