chapter 5 using data and cobol operators. initializing variables when you define a variable in...

29
Chapter 5 Using Data and COBOL Operators

Upload: sophia-craig

Post on 30-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Chapter 5

Using Data and COBOL Operators

Page 2: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Initializing Variables

• When you define a variable in WORKING-STORAGE, you also can assign it an initial value. This is a convenient method of setting variables to start with a known value.

• Variables are initialized with a VALUE IS clause.

Page 3: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Initializing Variables

• If you want a variable to have a default value that will be used in the program, you must initialize it in WORKING-STORAGE.

• A variable that is not initialized has an undefined value until something is moved to it.

• An undefined value is one that can contain any value.

Page 4: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Initializing Variables

• For numeric variables, this can become a problem. If you attempt to use the DISPLAY (ADD 1) statement with a numeric variable that contains an undefined value, you probably will produce an error.

• This does not cause a compiler error, but usually causes an error while the program is running.

• The• program usually aborts with a message such as this:– ATTEMPT TO PERFORM ARITHMETIC WITH NON-NUMERIC DATA

OR– VARIABLE THE-NUMBER DOES NOT CONTAIN NUMERIC DATA

Page 5: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Initializing Variables

• Initializing a variable with a VALUE in WORKING-STORAGE is the same as using MOVE to give it a value. Thereafter, you can use MOVE to assign values to the variable later in the program.

• Variables that are not initialized contain undefined values until a MOVE moves something to them.

Page 6: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

SPACES and ZEROS• It is a good practice to initialize variables in

WORKING-STORAGE. The usual practice is to initialize – numeric variables to zero – alphanumeric variables to spaces.

• DO initialize variables in the DATA DIVISION when they are defined, or in the PROCEDURE DIVISION before they are used.

• DON'T perform any arithmetic functions on an uninitialized numeric variable.

Page 7: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

SPACES and ZEROS

Page 8: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Truncate Values

Page 9: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Truncate Values

• A truncated value occurs when a value that is too large for a numeric variable is moved to the numeric variable,

• or when a value that is too long for an alphanumeric variable is moved to the alphanumeric variable.

Page 10: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Truncate Values

• The compiler conveniently fills variables with blanks or zeroes when short or small values are moved to them, or when short or small values are used to initialize them.

• An alphanumeric variable truncates the right end of the value until the value fits in the variable.

• A numeric variable truncates the left end of the value until the value fits.

Page 11: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Truncate Values

Page 12: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Truncate Values

Page 13: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Multiple move statements

Page 14: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Decimal Number

• COBOL is a business language, which should be able to deal with decimal numbers, dollars and cents, and percentages.

• The character in a numeric PICTURE that represents a decimal point is an uppercase V.

• The following variable holds values ranging from 000.00 to 999.99.– E.g. 01 THE-VALUE PIC 999V99.

Page 15: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Decimal Number

• Any constant values that you move to a decimal variable or use to initialize a decimal variable are written in conventional format, as in these examples:

Page 16: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Decimal Number

• If you attempt to move a value containing too many decimals, the number is truncated on the right, and some of the decimal information will be lost. In this example, THE-VALUE ends up containing 467.23

Page 17: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Decimal Number

• Truncation still takes place from the high end as well. In this example, THE-VALUE ends up containing 923.46 because the number is truncated on both the left and the right:

Page 18: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Positive and Negative Numbers

• COBOL numbers can also contain a positive or negative sign. The PICTURE character for a sign is an initial S.

• The S must be the first character in the picture.

Page 19: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Displaying Decimal and Sign

• Numbers that contain an S for a sign (positive or negative) or a V for a decimal are stored in memory in a special format that speeds up calculations.

• However, this format does not display correctly.

• The PICTURE character for a sign in a numeric variable that will be used for a DISPLAY is the minus sign (-).

Page 20: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Displaying Decimal and Sign

• The following variable holds the values -999.99 through 999.99 for display purposes:

• The display sign (-) displays only when the value is negative. If DISPLAY-VALUE contains -46.17, it displays as the following:

Page 21: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Displaying Decimal and Sign

• The - also can be placed at the end of the picture rather than at the beginning.

• It is fairly common in business programs to see display values specified as follows:– E.g. 01 THE-DISPLAY-VALUE PIC 999999.99-.

• In a display variable, you can suppress the display of leading zeroes, using Z to replace 9 in the picture of the variable. Here is an example:– E.g. 01 THE-DISPLAY-VALUE PIC ZZZZZ9.99-

Page 22: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Editing Characters

• The minus sign (-), decimal point (.), comma (,) and the character Z are called editing characters.

• A numeric variable that contains an editing character is called an edited numeric variable.

• Edited numeric variables should be used only to display values and should not be used in calculations.

Page 23: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

COBOL Numeric Operations

• The COBOL COMPUTE verb is a general-purpose verb that can be used to calculate results.

• Arithmetic expressions in the COMPUTE verb use the arithmetic operators: + (addition), - (subtraction), *(multiplication), and / (division).

• You can use parentheses to affect the order in which operations are performed.

Page 24: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

COBOL Numeric Operations

Page 25: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

COBOL Numeric Operations

• The COMPUTE verb has two optional clauses: ROUNDED and ON SIZE ERROR.– ROUNDED rounds the result up or down as

necessary, based on the results of the calculation.– The ON SIZE ERROR logic is performed if the result

is larger than the variable that is used to store the result.

– The statement that follows ON SIZE ERROR also is executed if a COMPUTE statement attempts to dosomething impossible, such as divide by zero.

Page 26: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

COBOL Numeric Operations

Page 27: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

COBOL Numeric Operations

Page 28: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Quiz

1. What is the value in BIG-NUMBER after the MOVE?01 BIG-NUMBER PIC 9(5).MOVE 4976 TO BIG-NUMBER

2. What is the value in SMALL-NUMBER after the MOVE?01 SMALL-NUMBER PIC 9(2).MOVE 4976 TO SMALL-NUMBER.

Hint: Numbers are truncated from the left.

Page 29: Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value

Quiz

3. After the following move, THE-VALUE contains 000.00. Why?01 THE-VALUE PIC 999V99.MOVE 1000.001 TO THE-VALUE.