Compaq COBOL
User Manual


Previous Contents Index

1.5.2 Setting and Controlling Switches Externally

On Tru64 UNIX systems, to set switches from outside the image, use the SETENV command to change the status of program switches, as follows:


% setenv COBOL_SWITCHES "switch-list"

To remove switch settings:


% unsetenv COBOL_SWITCHES                             

To check switch settings, enter this command:


% printenv COBOL_SWITCHES          Shows switch settings.   
 

The switch-list can contain up to 16 switches separated by commas. To set a switch on, specify it in the switch-list. A switch is off (the default) if you do not specify it in the switch-list.

For example:


% setenv COBOL_SWITCHES "1,5,13"   Sets switches 1, 5, and 13 ON. 
 
% setenv COBOL_SWITCHES "9,11,16"  Sets switches 9, 11, and 16 ON. 
 
% setenv COBOL_SWITCHES " "        Sets all switches OFF.   <>
 

On OpenVMS Alpha systems, to set switches from outside the image or for a process, use the DCL DEFINE or ASSIGN command to change the status of program switches as follows:


$ DEFINE COB$SWITCHES "switch-list"

The switch-list can contain up to 16 switches separated by commas. To set a switch ON, specify it in the switch-list. A switch is OFF (the default) if you do not specify it in the switch-list.

For example:


$ DEFINE COB$SWITCHES "1,5,13"   Sets switches 1, 5, and 13 ON. 
 
$ DEFINE COB$SWITCHES "9,11,16"  Sets switches 9, 11, and 16 ON. 
 
$ DEFINE COB$SWITCHES " "        Sets all switches OFF.

The order of evaluation for logical name assignments is image, process, group, system. System and group assignments (including Compaq COBOL program switch settings) continue until they are changed or deassigned. Process assignments continue until they are changed, deassigned, or until the process ends. Image assignments end when they are changed or when the image ends.

You should know the system and group assignments for COB$SWITCHES unless you have defined them for your process or image. To check switch settings, enter this command:


$ SHOW LOGICAL COB$SWITCHES 

Use the DCL DEASSIGN command to remove the switch-setting logical name from your process and reactivate the group or system logical name (if any):


$ DEASSIGN COB$SWITCHES 

To change the status of external switches during execution, follow these steps:

  1. Interrupt the image with a STOP (literal-string) COBOL statement. (See the Compaq COBOL Reference Manual for more information.)
  2. Use the DCL DEFINE command to change switch settings.
  3. Continue execution with the DCL CONTINUE command. Be sure not to force the interrupted image to exit by entering a command that executes another image.

For information about these DCL commands, see the OpenVMS DCL Dictionary.<>

Following is a simple program that displays a message depending on the state of the logical name COB$SWITCHES (on OpenVMS Alpha systems) or the environment variable COBOL_SWITCHES (on Tru64 UNIX systems):


IDENTIFICATION DIVISION. 
PROGRAM-ID. TSW. 
 
ENVIRONMENT DIVISION. 
CONFIGURATION SECTION. 
SPECIAL-NAMES. 
    SWITCH 12 IS SW12 ON IS SW12-ON OFF IS SW12-OFF. 
 
PROCEDURE DIVISION. 
01-S. 
    DISPLAY "**TEST SWITCHES**". 
    IF SW12-ON 
       DISPLAY "SWITCH 12 IS ON". 
    IF SW12-OFF 
       DISPLAY "SWITCH 12 IS OFF". 
 
    DISPLAY "**END**". 
    STOP RUN. 
END PROGRAM TSW. 

On OpenVMS, to test the previous program on an OpenVMS Alpha system, compile and link it and then type the following:


$ DEFINE COB$SWITCHES 12
$ RUN TSW

The output is as follows:


**TEST SWITCHES** 
SWITCH 12 IS ON 
**END**                                                 <>

To test the previous program on a Tru64 UNIX system, compile and link it and then type the following:


% setenv COBOL_SWITCHES 12
% tsw

The output is as follows:


**TEST SWITCHES** 
SWITCH 12 IS ON 
**END**                                                       

1.6 Special Information for Year 2000 Programming

Even subsequent to the turn of the millennium, there still exist potential disruptions in previously problem-free software where there are instances of a two-digit year field that should be a four-digit field. Programmers need to correct all such fields, as Compaq cannot prevent problems that originate in application code.

Two-digit year formats used in controlling fields, or as keys in indexed files, can cause program logic to become ambiguous. It is a fundamental rule to use four-digit years instead of two-digit years in areas where sequential operations are driven from these values or for comparison of these values.

Compaq COBOL provides programmer access to four-digit and two-digit year formats:
4-digit FUNCTION CURRENT-DATE
4-digit FUNCTION DATE-OF-INTEGER
4-digit FUNCTION DATE-TO-YYYYMMDD
4-digit FUNCTION DAY-OF-INTEGER
4-digit FUNCTION DAY-TO-YYYYDDD
4-digit FUNCTION INTEGER-OF-DATE
4-digit FUNCTION INTEGER-OF-DAY
4-digit FUNCTION TEST-DATE-YYYYMMDD
4-digit FUNCTION TEST-DAY-YYYYDDD
4-digit FUNCTION WHEN-COMPILED
4-digit FUNCTION YEAR-TO-YYYY
   
2-digit ACCEPT FROM DATE
2-digit ACCEPT FROM DAY
4-digit ACCEPT FROM DATE YYYYMMDD
4-digit ACCEPT FROM DAY YYYYDDD

Compaq COBOL offers date functions that can be used in program logic that makes decisions about year order. The full four-digit year handled by the six functions listed should be used in internal program logic decisions that are based on years. External displays of year information can continue to use two-digit formats when that is appropriate.

You should check program logic in code that uses ACCEPT, to verify that millennium transition dates are properly handled.

The use of two-digit years in applications does not automatically create a problem, but a problem could exist. Programmers need to inspect each of their applications for two-digit year dependencies and change any such instances to check the full four-digit year value.


Chapter 2
Handling Numeric Data

Numeric data in Compaq COBOL is evaluated with respect to the algebraic value of the operands.

This chapter describes the following topics concerning numeric data handling:

2.1 How the Compiler Stores Numeric Data

Understanding how data is stored will help you in the following situations:

The storage considerations applicable to tables are described in Chapter 4.

For each numeric data item, Compaq COBOL stores the numeric value, and a sign (if an S appears in the PICTURE clause).

The USAGE clause of a numeric data item specifies the data's internal format in storage. When you do not specify a USAGE clause, the default usage is DISPLAY. For further information about internal representations, see the USAGE clause tables in the Compaq COBOL Reference Manual.

2.2 Specifying Alignment

In Compaq COBOL, all records, and elementary items with level 01 or 77, begin at an address that is a multiple of 8 bytes (a quadword boundary). By default, the Compaq COBOL compiler will locate a subordinate data item at the next unassigned byte location. However, the SYNCHRONIZED clause, the -align flag (on Tru64 UNIX), the /ALIGNMENT qualifier (on OpenVMS Alpha), and alignment directives can be used to modify this behavior, causing some numeric data items to be aligned on a 2-, 4-, or 8-byte boundary. You can thus tune data alignment for optimum performance, compatibility with Compaq COBOL for OpenVMS VAX, or flexibility. (See Chapter 16, Managing Memory and Data Access and Chapter 15, Optimizing Your Compaq COBOL Program in this manual, and the SYNCHRONIZED clause in the Compaq COBOL Reference Manual for a complete discussion of alignment.)

2.3 Sign Conventions

Compaq COBOL numeric items can be signed or unsigned. Note the following sign conventions:

Do not use unsigned numeric items in arithmetic operations. They usually cause programming errors and are handled less efficiently than signed numeric items. The following example shows how unsigned numeric items can cause errors:


DATA DIVISION 
. 
. 
. 
01 A PIC 9(5) COMP VALUE 2. 
01 B PIC 9(5) COMP VALUE 5. 

Then:


SUBTRACT B FROM A.       (A = 3) 
 
SUBTRACT 1 FROM A.       (A = 2) 

However:


COMPUTE A = (A - B) - 1    (A = 4) 

The absence of signs for the numeric items A and B results in two different answers after parallel arithmetic operations have been done. This occurs because internal temporaries (required by the COMPUTE statement) are signed. Thus, the result of (A--B) within the COMPUTE statement is --3; --3 minus 1 is --4 and the value of A then becomes 4.

2.4 Invalid Values in Numeric Items

All Compaq COBOL arithmetic operations store valid values in their result items. However, it is possible, through group moves or REDEFINES, to store data in numeric items that do not conform to the data definitions of those items.

The results of arithmetic operations that use invalid data in numeric items are undefined. You can use the -check decimal flag (on the Tru64 UNIX operating system) or the /CHECK=DECIMAL qualifier (on the OpenVMS Alpha operating system) to validate numeric digits when using display numeric items in a numeric context; note that this flag or qualifier causes a program to terminate abnormally if there is invalid data. In the case of data with blanks (typically, records in a file), you can use the -convert leading_blanks flag (on Tru64 UNIX) or the /CONVERT qualifier (on OpenVMS Alpha) to change all blanks to zeroes before performing the arithmetic operation. If you specify both the -check decimal and the -convert leading_blanks flags (on Tru64 UNIX), or both the /CHECK=DECIMAL and the /CONVERT qualifiers on OpenVMS Alpha, the conversion of blanks will be done prior to the validation of the resulting numeric digits. Note that the use of either or both of these qualifiers increases the execution time of the program. See Compaq COBOL online Help (at the OpenVMS Alpha system prompt), or man cobol (on Tru64 UNIX) for more information.

2.5 Evaluating Numeric Items

Compaq COBOL provides several kinds of conditional expressions used for evaluating numeric items. These conditional expressions include the following:

The following sections explain these conditional expressions in detail.

2.5.1 Numeric Relation Test

A numeric relation test compares two numeric quantities and determines if the specified relation between them is true. For example, the following statement compares item FIELD1 to item FIELD2 and determines if the numeric value of FIELD1 is greater than the numeric value of FIELD2:


IF FIELD1 > FIELD2 ... 

If the relation condition is true, the program control takes the true path of the statement.

Table 2-1 describes the relational operators.

Table 2-1 Numeric Relational Operator Descriptions
Operator Description
IS [NOT] GREATER THAN
IS [NOT] >
The first operand is greater than (or not greater than) the second operand.
IS [NOT] LESS THAN
IS [NOT] <
The first operand is less than (or not less than) the second operand.
IS [NOT] EQUAL TO
IS [NOT] =
The first operand is equal to (or not equal to) the second operand.
IS GREATER THAN OR
EQUAL TO
IS >=
The first operand is greater than or equal to the second operand.
IS LESS THAN OR EQUAL TO
IS <=
The first operand is less than or equal to the second operand.

Comparison of two numeric operands is valid regardless of their USAGE clauses.

The length of the literal or arithmetic expression operands (in terms of the number of digits represented) is not significant. Zero is a unique value, regardless of the sign.

Unsigned numeric operands are assumed to be positive for comparison. The results of relation tests involving invalid (nonnumeric) data in a numeric item are undefined.

2.5.2 Numeric Sign Test

The sign test compares a numeric quantity to zero and determines if it is greater than (positive), less than (negative), or equal to zero. Both the relation test and the sign test can perform this function. For example, consider the following relation test:


IF FIELD1 > 0 ... 

Now consider the following sign test:


IF FIELD1 POSITIVE ... 

Both of these tests accomplish the same thing and always arrive at the same result. The sign test, however, shortens the statement and makes it more obvious that the sign is being tested.

Table 2-2 shows the sign tests and their equivalent relation tests.

Table 2-2 Sign Tests
Sign Test Equivalent Relation Test
IF FIELD1 POSITIVE ... IF FIELD1 > 0 ...
IF FIELD1 NOT POSITIVE ... IF FIELD1 NOT > 0 ...
IF FIELD1 NEGATIVE ... IF FIELD1 < 0 ...
IF FIELD1 NOT NEGATIVE ... IF FIELD1 NOT < 0 ...
IF FIELD1 ZERO ... IF FIELD1 = 0 ...
IF FIELD1 NOT ZERO ... IF FIELD1 NOT = 0 ...

Sign tests do not execute faster or slower than relation tests because the compiler substitutes the equivalent relation test for every correctly written sign test.

2.5.3 Numeric Class Tests

The class test inspects an item to determine if it contains numeric or alphabetic data. For example, the following statement determines if FIELD1 contains numeric data:


IF FIELD1 IS NUMERIC ... 

If the item is numeric, the test condition is true, and program control takes the true path of the statement.

Both relation and sign tests determine only if an item's contents are within a certain range. Therefore, certain items in newly prepared data can pass both the relation and sign tests and still contain data preparation errors.

The NUMERIC class test checks alphanumeric or numeric DISPLAY or COMP-3 usage items for valid numeric digits. If the item being tested contains a sign (whether carried as an overpunched character or as a separate character), the test checks it for a valid sign value. If the character position carrying the sign contains an invalid sign value, the NUMERIC class test rejects the item, and program control takes the false path of the IF statement.

The ALPHABETIC class test check is not valid for an operand described as numeric.

2.5.4 Success/Failure Tests

The success/failure condition tests the return status codes of COBOL and non-COBOL procedures for success or failure conditions. You test status-code-id as follows:


You can use the SET statement to initialize or alter the status of status-code-id (which must be a word or longword COMP integer represented by PIC 9(1 to 9) COMP or PIC S9(1 to 9) COMP), as follows:


The SET statement is typically in the called program, but the calling program may also SET the status of status-code-id. The SUCCESS class condition is true if status-code-id has been set to SUCCESS, otherwise it is false. The FAILURE class condition is true if status-code-id has been set to FAILURE, otherwise it is false. The results are unspecified if status-code is not set.

Example 2-1 shows the significant COBOL code relevant to a success/failure test.

Example 2-1 Success/Failure Test

...
PROGRAM-ID.  MAIN-PROG. 
...
O1   RETURN-STATUS      PIC S9(9) COMP. 
...
    CALL "PROG-1" GIVING RETURN-STATUS. 
    IF RETURN-STATUS IS FAILURE PERFORM FAILURE-ROUTINE. 
...
PROGRAM-ID. PROG-1. 
.... 
WORKING-STORAGE SECTION. 
01  RETURN-STATUS     PIC S9(9) COMP. 
PROCEDURE DIVISION GIVING RETURN-STATUS. 
...
    IF NUM-1 = NUM-2 
              SET RETURN-STATUS TO SUCCESS 
    ELSE 
              SET RETURN-STATUS TO FAILURE. 
...
    EXIT PROGRAM. 
END PROGRAM PROG-1. 
END PROGRAM MAIN-PROG. 


Previous Next Contents Index