Compaq COBOL AAQ2G1FTK User Manual

Page 115

Advertising
background image

Handling Nonnumeric Data

3.4 Testing Nonnumeric Items

ASCII bytes, take part in the comparison, and zeros are not supplied for P
characters in the PICTURE character-string.

The compiler does not accept a comparison between a noninteger numeric operand
and a nonnumeric operand. If you try to compare these two items, you receive a
diagnostic message at compile time.

3.4.1.2 Comparison Operations

If the two operands are acceptable, the compiler compares them character by
character. The compiler starts at the first byte and compares the corresponding
bytes until it either encounters a pair of unequal bytes or reaches the last byte of
the longer operand.

If the compiler encounters a pair of unequal characters, it considers their relative
position in the collating sequence. The operand with the character that is
positioned higher in the collating sequence is the greater operand.

If the operands have different lengths, the comparison proceeds as though the
shorter operand were extended on the right by sufficient ASCII spaces (decimal
32) to make both operands the same length.

If all character pairs are equal, the operands are equal.

3.4.2 Class Tests for Nonnumeric Items

An IF statement with a class condition tests the value in a nonnumeric data item
(USAGE DISPLAY only) to determine whether it contains numeric, alphabetic, or
user-defined data and uses the result to alter the flow of control in the program.
For example:

IF ITEM-1 IS NUMERIC...
IF ITEM-2 IS ALPHABETIC...
IF ITEM-3 IS NOT NUMERIC...

If the data item consists entirely of the ASCII characters 0 to 9, with or without
the operational sign, the class condition is NUMERIC. If the item consists
entirely of the ASCII characters A to Z (upper- or lowercase) and spaces, the class
condition is ALPHABETIC.

The ALPHABETIC-LOWER test is true if the operand contains any combination
of the lowercase alphabetic characters a to z, and the space. Otherwise the test is
false.

The ALPHABETIC-UPPER test is true if the operand contains any combination
of the uppercase alphabetical characters A to Z, and the space. Otherwise, the
test is false.

You can also perform a class test on a data item that you define with the CLASS
clause of the SPECIAL-NAMES paragraph.

A class test is true if the operand consists entirely of the characters listed in the
definition of the CLASS-NAME in the SPECIAL-NAMES paragraph. Otherwise,
the test is false.

When the reserved word NOT is present, the compiler considers it and the
next key word as one class condition defining the class test to be executed.
For example, NOT NUMERIC determines if an operand contains at least one
nonnumeric character.

Handling Nonnumeric Data 3–5

Advertising