Custom Search

testing an if statement in cobol,writing the rest of the if,COBOL comparison operators.

What Can You Test with IF

The condition in an IF verb is a test of one value against another for equality or inequality.

New Term: The symbols used to compare two values are called comparison operators. The short and long versions of these comparisons are all comparison operators. IS NOT EQUAL, NOT =, =, IS EQUAL, NOT <, >, GREATER THAN, and NOT GREATER THAN are all examples of comparison operators. Tables 4.1 and 4.2 list all of the comparison operators.

Table 4.1 lists the comparisons that can be made and describes their effects.

COBOL comparison operators.

Comparison Operator

Description

IF x IS EQUAL y

True if x equals y

IF x IS LESS THAN y

True if x is less than y

IF x IS GREATER THAN y

True if x is greater than y

IF x IS NOT EQUAL y

True if x does not equal y

IF x IS NOT LESS THAN y

True if x is not less than y (or is equal to or greater than y)

IF x IS NOT GREATER THAN y

True if x is not greater than y (or is equal to or less than y)

The word IS in a comparison is optional, and EQUAL, GREATER THAN, and LESS THAN can be shortened to =, >, and <, respectively. Table 4.2 compares the possible versions of comparisons.

More COBOL comparison operators.

Optional Operator

Shortest Version

IF x EQUAL y

IF x = y

IF x LESS THAN y

IF x <>

IF x GREATER THAN y

IF x > y

IF x NOT EQUAL y

IF x NOT = y

IF x NOT LESS THAN y

IF x NOT <>

IF x NOT GREATER THAN y

IF x NOT > y

Listing 4.4 repeats yesno03.cbl, using the shortened comparisons.

yesno03.cbl with shorter comparisons.

000100 IDENTIFICATION DIVISION.

000200 PROGRAM-ID. YESNO04.

000300*--------------------------------------------------

000400* This program asks for a Y or N answer, and then

000500* displays whether the user chose yes or no.

000600* The edit logic allows for entry of Y, y, N, or n.

000700*--------------------------------------------------

000800 ENVIRONMENT DIVISION.

000900 DATA DIVISION.

001000 WORKING-STORAGE SECTION.

001100

001200 01 YES-OR-NO PIC X.

001300

001400 PROCEDURE DIVISION.

001500 PROGRAM-BEGIN.

001600

001700 PERFORM GET-THE-ANSWER.

001800

001900 PERFORM EDIT-THE-ANSWER.

002000

002100 PERFORM DISPLAY-THE-ANSWER.

002200

002300 PROGRAM-DONE.

002400 STOP RUN.

002500

002600 GET-THE-ANSWER.

002700

002800 DISPLAY "Is the answer Yes or No? (Y/N)".

002900 ACCEPT YES-OR-NO.

003000

003100 EDIT-THE-ANSWER.

003200

003300 IF YES-OR-NO = "y"

003400 MOVE "Y" TO YES-OR-NO.

003500

003600 IF YES-OR-NO = "n"

003700 MOVE "N" TO YES-OR-NO.

003800

003900 DISPLAY-THE-ANSWER.

004000 IF YES-OR-NO = "Y"

004100 PERFORM IT-IS-VALID

004200 DISPLAY "You answered Yes.".

004300

004400 IF YES-OR-NO = "N"

004500 PERFORM IT-IS-VALID

004600 DISPLAY "You answered No.".

004700

004800 IT-IS-VALID.

004900 DISPLAY "Your answer is valid and".

005000

For numeric values, all these tests make sense. Less than and greater than are both conditions that easily can be established when you are testing two numbers. But what are you testing when you compare two alphanumeric variables?

When a condition test is performed on alphanumeric variables, the tests usually compare the characters in the two alphanumeric values on the left and right sides of the comparison operator, in ASCII order. (See Appendix B, "ASCII.")

New Term: The sequence in which the characters appear in the ASCII chart is known as the ASCII collating sequence. Collate means to assemble in some sort of order--in this case, ASCII order.

ASCII is not the only collating sequence. IBM mainframes use a collating sequence called EBCDIC. In the ASCII collating sequence, numbers appear before uppercase letters, and uppercase letters appear before lowercase letters. In the EBCDIC collating sequence, lowercase letters appear before uppercase letters and numbers appear last. Punctuation characters vary quite a bit in the EBCDIC and ASCII collating sequences. Collating sequences also vary for different spoken languages. Castillian Spanish treats the letter combinations ch and ll as single letters so that llanero sorts after luna and chico sorts after corazon. The examples in this book are based on the English ASCII collating sequence.

In ASCII order, A is less than B, AB is less than ABC, and the uppercase letters are less than the lowercase letters; so, ABC is less than abc. When an alphanumeric variable contains the digits 0 through 9, the digits are less than the characters, so 1BC is less than ABC. Spaces are the lowest of all, so three spaces are less than 00A. Refer to Appendix B for the complete set and sequence of ASCII characters.

Listing 4.5 will accept two words from a user and then display them in ASCII order. You can use this program any time you want to find out the actual ASCII order for two values. The testing is done in the paragraph DISPLAY-THE-WORDS, which starts at line 004100. The actual tests, at lines 004500 and 004900, use a greater than (>) and a not greater than (NOT >) comparison to decide which word to display first.