290 likes | 405 Views
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. This is a convenient method of setting variables to start with a known value . Variables are initialized with a VALUE IS clause.
E N D
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. This is a convenient method of setting variables to start with a known value. • Variables are initialized with a VALUE IS clause.
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.
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
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.
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.
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.
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.
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.
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:
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
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:
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.
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 (-).
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:
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-
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.
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.
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 dosomethingimpossible, such as divide by zero.
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.
Quiz 3. After the following move, THE-VALUE contains 000.00. Why? 01 THE-VALUE PIC 999V99. MOVE 1000.001 TO THE-VALUE.