+ All Categories
Home > Documents > Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two...

Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two...

Date post: 17-Jan-2016
Category:
Upload: roland-horn
View: 225 times
Download: 0 times
Share this document with a friend
Popular Tags:
33
Chapter 2 Using Variables and Constant
Transcript
Page 1: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Chapter 2

Using Variables and Constant

Page 2: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

What is a Constant ?

• The data in COBOL programs falls in two broad categories:– Constants and Variables

• A constant is a value that cannot be modified while the program is running.

• Character constants are enclosed in quotation marks.

• Numeric constants are not.

Page 3: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

What is a Constant ?

IDENTIFICATION DIVISION.PROGRAM-ID. CONST.ENVIRONMENT DIVISION.DATA DIVISION.PROCEDURE DIVISION.PROGRAM-BEGIN.

DISPLAY "Hello world". DISPLAY 55.

String Constant

Numeric Constant

Page 4: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

What is a variable?

• A variable is a value that can be changed while the program is running.

• When a variable is created in a program, an area of memory is set aside to hold values.

• A variable is given a name. The name can be used in the program to refer to the value.

• The value stored in memory can be modified while the program is running by using the variable name.

Page 5: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Defining Numeric Variables in COBOL

• Variable names use the same characters as paragraph names: – A through Z, – 0 through 9, – hyphen (-).– E.g.

01 THE-NUMBER PICTURE 9999.

Page 6: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Defining Numeric Variables in COBOL

• A COBOL variable definition contains at least three parts:– The level number– The name– The PICTUREe.g.01 THE-NUMBER PICTURE 9999.

Page 7: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Defining Numeric Variables in COBOL

• In the syntax, the level number is 01. The level number 01 must be in Area A, columns 8 through 11.

• Variable definition is the name of the variable and, in this case, is THE-NUMBER. This is the

• Data name used to identify the variable.– The variable will be referred to by its data name, THE-

NUMBER• The name of the variable must start in Area B,

columns 12 through 72. – In this example, THE-NUMBER starts in column 12.

Page 8: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Defining Numeric Variables in COBOL

• The PICTURE defines two things about a variable: – the size of the variable (the number of bytes used

in memory for the value) – the type of data that can be stored in the variable.

• The picture 9999 indicates that four numeric characters can be stored in the variable named THE-NUMBER.

Page 9: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Defining Numeric Variables in COBOL

• The 9999 in the picture does not indicate that the variable contains the value 9999.

• It indicates that the variable can be used for numeric values in the range 0 through 9,999.– For example– The values 17 and 6,489 will both fit in THE-

NUMBER, but the value 65,413 is too large.

Page 10: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Defining Numeric Variables in COBOLIDENTIFICATION DIVISION.PROGRAM-ID. ADD01.

DATA DIVISION.WORKING-STORAGE SECTION.01 FIRST-NUMBER PICTURE 99.01 SECOND-NUMBER PICTURE 99.01 THE-RESULT PICTURE 999.

PROCEDURE DIVISION.

DISPLAY "Enter the first number.". ACCEPT FIRST-NUMBER. DISPLAY "Enter the second number.". ACCEPT SECOND-NUMBER.

COMPUTE THE-RESULT = FIRST-NUMBER + SECOND-NUMBER.

DISPLAY "The result is:".DISPLAY THE-RESULT.DISPLAY “The result is: “ THE-RESULT.STOP RUN.

Page 11: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Naming Variables in COBOL

• COBOL variable names are similar to paragraph and section names because they can use any of the uppercase alphabet characters.

• The digits 0 through 9, and the hyphen (but not as a starting character).

• COBOL variable and paragraph names are limited to 30 characters.

Page 12: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Naming Variables in COBOLValid Name Invalid Name Explanation of Invalid

NameTOTAL-DOLLARS TOTAL-$ Uses an invalid $ in the

nameSUM-OF-COLUMNS sum-of-columns Uses lowercase letters7-BY-5 7_BY_5 Uses the invalid _

character in thename

MINUS-RESULT -RESULT Starts with a hyphenBOEING-707-SEATS BOEING-707-MAXIMUM- SEATING-CAPACITY Exceeds 30 characters

Page 13: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Defining and Using Variables

• A numeric variable is used to store numbers.– Example

01 THE-NUMBER PICTURE IS 99.

• Variables that can hold character data are called alphanumeric variables.– Example

01 THE-MESSAGE PICTURE IS XXXXXXXXXX.

Page 14: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Defining and Using Variables

• An alphanumeric variable can also be used to hold numbers (such as storing 123 in a PICTURE IS XXX variable)

• But will not be able to use the values as numbers. – For example, you could display the PICTURE IS XXX

variable containing 123, but you couldn't use the COMPUTE verb to add 1 to it.

Page 15: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Defining and Using Variables

IDENTIFICATION DIVISION.PROGRAM-ID. HELLO02.DATA DIVISION.WORKING-STORAGE SECTION.

01 THE-NAME PICTURE XXXXXXXXXX.

PROCEDURE DIVISION.DISPLAY "Enter someone's name.".ACCEPT THE-NAME.DISPLAY "Hello " THE-NAME.STOP RUN.

Page 16: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Introducing the MOVE Verb

• The MOVE verb in COBOL is a general-purpose verb, used to store a value in a variable.– Syntax:

MOVE value TO variable.

• In this syntax, variable must be a variable defined in the DATA DIVISION, and value can be another variable or a constant.

Page 17: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Introducing the MOVE Verb

• Here are some examples:MOVE 12 TO THE-NUMBER.MOVE ONE-NUMBER TO ANOTHER-NUMBER.MOVE "XYZ" TO THE-MESSAGE.

• MOVE is used to set a variable to a specific value. – For example, – if you're going to use the variable THE-COUNTER as a counter

and you need the count to start at 1, you might use the following as one method of setting up the variable with a starting value:

MOVE 1 TO THE-COUNTER.

Page 18: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Introducing the MOVE Verb• It copies values from the source variable and stores

them in the target variable.Command Effect

MOVE 19 TO THE-NUMBER Stores 19 in the variable THE-NUMBER, or sets THE-NUMBER to avalue of 19

MOVE "Hello" TO THE-MESSAGE Stores Hello in the variable THE-MESSAGE, or setsTHE-MESSAGE to contain Hello

MOVE A-NUMBER TO THE-NUMBER Locates the variable named A-NUMBER, gets the value stored there,and copies it or moves it to the variable named THE-NUMBER

MOVE THE-OLD-NAME TO THE-NEW-NAME

Locates the variable named THE-OLD-NAME, gets the value storedthere, and copies it or moves it to the variable namedTHE-NEW-NAME

Page 19: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Extra Reading

• Read and test the programs– Formatting output– Program 2.8– Program 2.9– Layout and Punctuation– Program 2.10– Continuation Characters

Page 20: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Exercises

1. How many bytes of memory are used by the following variable?01 CUSTOMER-NAME PIC X(30).

2. What type of data can be stored in CUSTOMER-NAME?

3. If you move a value to CUSTOMER-NAME, such asMOVE "ABC Company" TO CUSTOMER-NAME.only the first 11 characters of the variable are filled with the value. What is placed in the remaining 19 character Positions?

Page 21: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Exercises

4. What is the largest number that can be moved using MOVE to the following variable?

01 UNITS-SOLD PIC 9(4).5. What is the smallest value that can be moved

using MOVE to UNITS-SOLD?6. If 12 is moved to UNITS-SOLD, as in

MOVE 12 to UNITS-SOLD.what values are stored in the four numeric positions of UNITS-SOLD?

Page 22: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Paragraph Names

• Paragraph names are used only as bookmarks, it is possible to insert more paragraph names into a program.

• Remember that you can assign your own paragraph names.

• The rules for naming paragraphs are similar to the rules for naming variables

• DO use uppercase paragraph names if you want your code to be portable.

Page 23: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Paragraph Names

• Paragraph Naming Rules:– A paragraph name can contain 30 characters. In fact, a

paragraph name can be longer than 30 characters, but the compiler will warn you that it will use only the first 30 characters of the name. The remaining characters can be included in the name but the compiler will ignore them.

– The characters can be A through Z, 0 through 9, and the hyphen (-). Some compilers also allow the lowercase characters a through z.

– The paragraph name must not start with the hyphen.– Paragraph names must start in Area A, columns 8 through

11, and must end with a period.

Page 24: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Paragraph NamesIDENTIFICATION DIVISION.PROGRAM-ID. ADD04.DATA DIVISION.WORKING-STORAGE SECTION.01 FIRST-NUMBER PIC 99.01 SECOND-NUMBER PIC 99.01 THE-RESULT PIC 999.

PROCEDURE DIVISION.PROGRAM-BEGIN.

DISPLAY "This program will add 2 numbers.".

GET-FIRST-NUMBER.DISPLAY "Enter the first number.".ACCEPT FIRST-NUMBER.

GET-SECOND-NUMBER.DISPLAY "Enter the second number.".DISPLAY "The result is " THE-RESULT.

PROGRAM-DONE.STOP RUN.

Page 25: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Reserved Words?

• Reserved words are reserved in the language to have a special meaning, and the programmer cannot use these words for some other purpose.PROGRAM-ID. DISPLAY.

• DO name programs, variables, and paragraphs with descriptive names that make their use obvious.

• DON'T name programs, variables, or paragraphs with reserved words.

Page 26: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

What is STOP RUN?

• STOP RUN can occur anywhere in the program• It will stop the execution. • In all the examples so far, the STOP RUN is

placed in its own separate paragraph to make it stand out as the end of the program

Page 27: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

What Is the PERFORM Verb?

• Suppose you had one action that you performed several times in a program. In top-to-bottom execution, you would have to code that same logic over and over.

• The PERFORM verb avoids this problem of coding repetitive actions.

Page 28: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

What Is the PERFORM Verb? IDENTIFICATION DIVISION. PROGRAM-ID. HELLO04.* This program illustrates the use of a

PERFORM DATA DIVISION. PROCEDURE DIVISION. PROGRAM-BEGIN.

DISPLAY "Today's message is:".PERFORM SAY-HELLO.

PROGRAM-DONE.STOP RUN.

SAY-HELLO.DISPLAY "Hello world".

• PERFORM SAY-HELLO indicates the following:1. Locate the paragraph

named SAY-HELLO.2. Jump to that

paragraph and start executing there.

3. When that paragraph ends, return to the end of this sentence (PERFORM SAY-HELLO).

Page 29: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

What Is the PERFORM Verb? – The Incorrect way IDENTIFICATION DIVISION. PROGRAM-ID. HELLO05.* This program illustrates the incorrect placement of a* Paragraph that is the target of a perform DATA DIVISION. PROCEDURE DIVISION. PROGRAM-BEGIN.

DISPLAY "Today's message is:".PERFORM SAY-HELLO.

SAY-HELLO.DISPLAY "Hello world".

PROGRAM-DONE.STOP RUN.

Page 30: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

What Is the PERFORM Verb?

• A PERFORM serves to break up a program into smaller, more manageable pieces.

• If you're changing the sales commission from 10 percent to 11 percent, it's much easier to search through a long program looking for a paragraph named CALCULATE-COMMISSION than to plow through a long list of code not broken into paragraphs.

Page 31: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

What Is the PERFORM Verb?

• DO locate repetitive actions in your programs, and create separate paragraphs containing those actions. Then PERFORM the paragraph wherever those actions are needed.

• DON'T keep typing the same code over and over in one program.

Page 32: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Exercise

1. If the code in a paragraph is designed to locate overdue customers, which of the following would be the best name for the paragraph?

a) LOCATE-CUSTOMERS.b) FIND-SOME-STUFF.c) LOCATE-OVERDUE-CUSTOMERS.

Page 33: Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.

Reading• Read and try the sample program at chapter 3


Recommended