330 likes | 490 Views
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 is a value that cannot be modified while the program is running. Character constants are enclosed in quotation marks.
E N D
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 is a value that cannot be modified while the program is running. • Character constants are enclosed in quotation marks. • Numericconstants are not.
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
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.
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.
Defining Numeric Variables in COBOL • A COBOL variable definition contains at least three parts: • The level number • The name • The PICTURE e.g. 01 THE-NUMBER PICTURE 9999.
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.
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.
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.
Defining Numeric Variables in COBOL IDENTIFICATION 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.
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.
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.
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.
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.
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.
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.
Introducing the MOVE Verb • It copies values from the source variable and stores them in the target variable.
Extra Reading • Read and test the programs • Formatting output • Program 2.8 • Program 2.9 • Layout and Punctuation • Program 2.10 • Continuation Characters
Exercises • How many bytes of memory are used by the following variable? 01 CUSTOMER-NAME PIC X(30). • What type of data can be stored in CUSTOMER-NAME? • If you move a value to CUSTOMER-NAME, such as MOVE "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?
Exercises • What is the largest number that can be moved using MOVE to the following variable? 01 UNITS-SOLD PIC 9(4). • What is the smallest value that can be moved using MOVE to UNITS-SOLD? • 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?
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.
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.
Paragraph Names IDENTIFICATION 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.
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.
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
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.
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:". PERFORMSAY-HELLO. PROGRAM-DONE. STOP RUN. SAY-HELLO. DISPLAY "Hello world". • PERFORM SAY-HELLO indicates the following: • Locate the paragraph named SAY-HELLO. • Jump to that paragraph and start executing there. • When that paragraph ends, return to the end of this sentence (PERFORM SAY-HELLO).
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.
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.
What Is the PERFORM Verb? • DOlocate repetitive actions in your programs, and create separate paragraphs containing those actions. Then PERFORM the paragraph wherever those actions are needed. • DON'Tkeep typing the same code over and over in one program.
Exercise • If the code in a paragraph is designed to locate overdue customers, which of the following would be the best name for the paragraph? • LOCATE-CUSTOMERS. • FIND-SOME-STUFF. • LOCATE-OVERDUE-CUSTOMERS.
Reading • Read and try the sample program at chapter 3