Custom Search

mainframe cobol programes for reversing the test and display,testing multiple conditions

Reversing the test and display.

004100 DISPLAY-THE-WORDS.

004200

004300 DISPLAY "The words sorted in ASCII order are:".

004400 IF WORD-1 <>

004500 DISPLAY WORD-1

004600 DISPLAY WORD-2.

004700 IF WORD-1 NOT <>

004800 DISPLAY WORD-2

004900 DISPLAY WORD-1.

You should also try a version of wrdsrt01.cbl that tests incorrectly in DISPLAY-THE-WORDS, This version tests for LESS THAN and GREATER THAN. Try coding this one as badsrt.cbl and satisfy yourself that the results are identical unless you enter the exact string for WORD-1 and WORD-2, such as ABC and ABC. Note that the test fails to display anything for this condition.

An incorrect version of the test.

004100 DISPLAY-THE-WORDS.

004200

004300 DISPLAY "The words sorted in ASCII order are:".

004400 IF WORD-1 <>

004500 DISPLAY WORD-1

004600 DISPLAY WORD-2.

004700 IF WORD-1 > WORD-2

004800 DISPLAY WORD-2

004900 DISPLAY WORD-1.

The indentation chosen for the IF is completely arbitrary. As long as the IF starts in and stays within Area B, the arrangement is up to you. Listing 4.8 and Listing 4.9 are equally valid, but in Listing 4.9 it is difficult to tell what is going on, and Listing 4.8 looks a bit sloppy.


DO/DON'T:
DO
indent IF conditions carefully. An IF controls all statements up to the period at the end of the sentence.

DON'T use sloppy indenting on an IF. Correct indentation gives a good visual clue of which parts of the program are controlled by the IF.


Sloppy indenting of an IF.

004100 DISPLAY-THE-WORDS.

004200

004300 DISPLAY "The words sorted in ASCII order are:".

004400 IF WORD-1 <>

004500 DISPLAY WORD-1

004600 DISPLAY WORD-2.

004700 IF WORD-1 NOT <>

004800 DISPLAY WORD-2

004900 DISPLAY WORD-1.

Failing to indent an IF.

004100 DISPLAY-THE-WORDS.

004200

004300 DISPLAY "The words sorted in ASCII order are:".

004400 IF WORD-1 <>

004500 DISPLAY WORD-1

004600 DISPLAY WORD-2.

004700 IF WORD-1 NOT <>

004800 DISPLAY WORD-2

004900 DISPLAY WORD-1.

Testing Multiple Conditions

An IF test also can be used to test more than one condition. Conditions can be combined by using AND, OR, or combinations of both. Listing 4.10 is a short menu program. A menu program is designed to display a series of options on the screen and let the user pick one option to execute. In this menu program, the user has a choice of displaying one of three possible messages.

Combining tests using OR.

000100 IDENTIFICATION DIVISION.

000200 PROGRAM-ID. MENU01.

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

000400* THIS PROGRAM DISPLAYS A THREE CHOICE MENU OF

000500* MESSAGES THAT CAN BE DISPLAYED.

000600* THE USER ENTERS THE CHOICE, 1, 2 OR 3, AND

000700* THE APPROPRIATE MESSAGE IS DISPLAYED.

000800* AN ERROR MESSAGE IS DISPLAYED IF AN INVALID

000900* CHOICE IS MADE.

001000*------------------------------------------------

001100 ENVIRONMENT DIVISION.

001200 DATA DIVISION.

001300 WORKING-STORAGE SECTION.

001400

001500 01 MENU-PICK PIC 9.

001600

001700 PROCEDURE DIVISION.

001800 PROGRAM-BEGIN.

001900

002000 PERFORM GET-THE-MENU-PICK.

002100

002200 PERFORM DO-THE-MENU-PICK.

002300

002400 PROGRAM-DONE.

002500 STOP RUN.

002600

002700* LEVEL 2 ROUTINES

002800 GET-THE-MENU-PICK.

002900

003000 PERFORM DISPLAY-THE-MENU.

003100 PERFORM GET-THE-PICK.

003200

003300 DO-THE-MENU-PICK.

003400 IF MENU-PICK <>

003500 MENU-PICK > 3

003600 DISPLAY "Invalid selection".

003700

003800 IF MENU-PICK = 1

003900 DISPLAY "One for the money.".

004000

004100 IF MENU-PICK = 2

004200 DISPLAY "Two for the show.".

004300

004400 IF MENU-PICK = 3

004500 DISPLAY "Three to get ready.".

004600

004700* LEVEL 3 ROUTINES

004800 DISPLAY-THE-MENU.

004900 DISPLAY "Please enter the number of the message".

005000 DISPLAY "that you wish to display.".

005100* Display a blank line

005200 DISPLAY " ".

005300 DISPLAY "1. First Message".

005400 DISPLAY "2. Second Message".

005500 DISPLAY "3. Third Message".

005600* Display a blank line

005700 DISPLAY " ".

005800 DISPLAY "Your selection (1-3)?".

005900

006000 GET-THE-PICK.

006100 ACCEPT MENU-PICK.

006200

Here are sample output results from menu01.cbl for a valid and an invalid response:

OUTPUT:

Please enter the number of the message

that you wish to display.

1. First Message

2. Second Message

3. Third Message

Your selection (1-3)?

2

Two for the show.

C>

C>

Please enter the number of the message

that you wish to display.

1. First Message

2. Second Message

3. Third Message

Your selection (1-3)?

5

Invalid selection

C>

C>

ANALYSIS: The valid menu selections are 1, 2, and 3. The test that the value entered is in a range at lines 003400 through 003500, ending with a display of an invalid entry message at line 003600. If the entered MENU-PICK is less than 1 or greater than 3, it is invalid. Note that the OR on line 003400 combines the two tests within one IF. An OR test is true if either of the tests is true.

Read the comments in the program, because they explain some of the options used to improve the look of the displayed menu. The levels in the comments relate to the level of PERFORM. Routines in level 2 are being performed from the top level of the program, PROGRAM-BEGIN. Routines in level 3 are performed from within routines at level 2.

An AND test is true only if both conditions being tested are true. Listing 4.11 asks the user to enter a number between 10 and 100, excluding 10 and 100. Therefore, the valid range of entries for this program is 011 through 099. Remember that ACUCOBOL will require that you enter the leading zero.