Book Read Free

Michael Coughlan

Page 16

by Beginning COBOL for Programmers-Apress (2014) (pdf)


  ____________________________________________________________

  88 Single-Room VALUE "SINGLE".

  ____________________________________________________________

  2. Write an IF statement that uses the SET verb to manually set the condition name

  InvalidCode to true if DeptCode contains anything except 1, 6, or 8.

  ___________________________________________________________________

  ___________________________________________________________________

  ___________________________________________________________________

  ___________________________________________________________________

  ___________________________________________________________________

  ___________________________________________________________________

  3. assume the variable DeptCode in question 2 is described as

  01 DeptCode PIC 9.

  ______________________________________________________________________

  Write a level 88 condition name called InvalidCode that is automatically set to true when

  the statement ACCEPT DeptCode accepts any value other than 1, 6, or 8.

  102

  Chapter 5 ■ Control StruCtureS: SeleCtion

  4. in each of the following five groups of skeleton IF statements, state whether the statements

  in each group have the same effect (in the sense that they evaluate to true or false). answer

  yes or no.

  Do these statements have the same effect?

  Answer

  IF Num1 = 1 OR Num1 NOT = 1...

  IF NOT (Num1 = 1 AND Num1 = 2) ...

  IF TransCode IS NOT = 3 OR Total NOT > 2550 ...

  IF NOT (TransCode IS = 3 OR Total > 2550) ...

  IF Num1 = 31 OR Num2 = 12 AND Num3 = 23 OR Num4 = 6 ...

  IF (Num1 = 31 OR (Num2 = 12 AND Num3 = 23)) OR Num4 = 6 ...

  IF Num1 = 15 OR Num1 = 12 OR Num1 = 7 AND City = "Cork" ...

  IF (Num1 = 15 OR Num1 = 12 OR Num1 = 7) AND City = "Cork" ...

  IF (Num1 = 1 OR Num1 = 2) AND (Num2 = 6 OR Num2 = 8) ...

  IF Num1 = 1 OR Num1 = 2 AND Num2 = 6 OR Num2 = 8 ...

  5. Write an EVALUATE statement to implement the decision part of a game of rock, paper, scissors.

  Most of the program has been written for you. Just complete the EVALUATE. ADD a WHEN OTHER

  branch to the EVALUATE to detect when a player enters a code other than 1, 2, or 3.

  IDENTIFICATION DIVISION.

  PROGRAM-ID. Listing5-10.

  AUTHOR. Michael Coughlan.

  DATA DIVISION.

  WORKING-STORAGE SECTION.

  01 PlayerGuess-A PIC 9 VALUE 1.

  88 Rock-A VALUE 1.

  88 Paper-A VALUE 2.

  88 Scissors-A VALUE 3.

  01 PlayerGuess-B PIC 9 VALUE 2.

  88 Rock-B VALUE 1.

  88 Paper-B VALUE 2.

  88 Scissors-B VALUE 3.

  PROCEDURE DIVISION.

  BEGIN.

  DISPLAY "Guess for player A (1=rock, 2=scissors, 3=paper) : "

  WITH NO ADVANCING

  ACCEPT PlayerGuess-A

  DISPLAY "Guess for player B (1=rock, 2=scissors, 3=paper) : "

  WITH NO ADVANCING

  ACCEPT PlayerGuess-B

  103

  Chapter 5 ■ Control StruCtureS: SeleCtion

  EVALUATE _____________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  ______________________________________________________________

  prOGraMMING eXerCISe

  listing 4-2 is a program that accepts two numbers from the user, multiplies them together, and then displays the result. Modify the program so that

  • it also accepts an operator symbol (+ - / *).

  • it uses EVALUATE to discover which operator has been entered and to apply that operator to the

  two numbers entered.

  • it uses the condition name ValidOperator to identify the valid operators and only displays the

  result if the operator entered is valid.

  • the Result data item is changed to accommodate the possibility that subtraction may result in a

  negative value.

  • the Result data item is changed to accommodate the decimal fractions that may result from

  division. the result data item should be able to accept values with up to two decimal places (for

  example, 00.43 or 00.74).

  104

  Chapter 5 ■ Control StruCtureS: SeleCtion

  LaNGUaGe KNOWLeDGe eXerCISeS—aNSWerS

  1. For each of the following condition names, which do you consider to be inappropriately

  named? Suggest more suitable names for these only.

  01 Country-Code PIC XX.

  88 UnitedStates VALUE "US".

  * Change

  * Example of use - IF UnitedStates DISPLAY "We are in America" END-IF

  01 Operating-System PIC X(15).

  88 Windows VALUE " WINDOWS".

  * Change

  * Example of use - IF Windows DISPLAY "Windows is best" END-IF

  01 Room-Type PIC X(20).

  88 Double-Room VALUE "DOUBLE".

  88 Single-Room VALUE "SINGLE".

  * No change.

  * Example of use -IF Double-Room ADD DoubleRoomSurchage TO RoomRent END-IF

  2. Write an IF statement that uses the SET verb to manually set the condition name

  InvalidCode to true if DeptCode contains anything except 1, 6, or 8.

  IF NOT (DeptCode = 1 OR DeptCode = 6 OR DeptCode = 8) THEN

  SET InvalidCode TO TRUE

  END-IF.

  or, using implied subjects:

  IF NOT (DeptCode = 1 OR 6 OR 8) THEN

  SET InvalidCode TO TRUE

  END-IF.

  3. assume the variable DeptCode in question 2 is described as

  01 DeptCode PIC 9.

  88 InvalidCode VALUE 0, 2 THRU 5,7,9.

  Write a level 88 condition name called InvalidCode that is automatically set to true when

  the statement ACCEPT DeptCode accepts any value other than 1, 6, or 8.

  105

  Chapter 5 ■ Control StruCtureS: SeleCtion

  4. in each of the following five groups of skeleton IF statements, state whether the statements

  in each group have the same effect (in the sense that they evaluate to true or false). answer

  yes or no.

  Do these statements have the same effect?

  answer

  IF Num1 = 1 OR Num1 NOT = 1...

  YES

  IF NOT (Num1 = 1 AND Num1 = 2)...

  in the sense that they

  are both always true.

  IF TransCode IS NOT = 3 OR Total NOT > 2550 ...

  NO

  IF NOT (TransCode IS = 3 OR Total > 2550)...

  IF Num1 = 31 OR Num2 = 12 AND Num3 = 23 OR Num4 = 6...

  YES

  IF (Num1 = 31 OR (Num2 = 12 AND Num3 = 23)) OR Num4 = 6...

  the brackets only make

  explicit what is ordained

  by the precedence rules.

  IF Num1 = 15 OR Num1 = 12 OR Num1 = 7 AND City = "Cork"...

>   NO

  IF (Num1 = 15 OR Num1 = 12 OR Num1 = 7) AND City =

  in the first num1=7

  "Cork"...

  anD City=SpaCeS are

  anDed together but in

  the second City=“Cork”

  is anDed with the result

  of the expression in the

  parentheses

  IF (Num1 = 1 OR Num1 = 2) AND (Num2 = 6 OR Num2 = 8) ...

  NO

  IF Num1 = 1 OR Num1 = 2 AND Num2 = 6 OR Num2 = 8 ...

  5. Write an EVALUATE statement to implement the decision part of a game of rock, paper, scissors.

  Most of the program has been written for you. Just complete the EVALUATE. ADD a WHEN OTHER

  branch to the EVALUATE to detect when a player enters a code other than 1, 2, or 3.

  Listing 5-10. Rock, Paper, Scissors Game

  IDENTIFICATION DIVISION.

  PROGRAM-ID. Listing5-10.

  AUTHOR. Michael Coughlan.

  DATA DIVISION.

  WORKING-STORAGE SECTION.

  01 PlayerGuess-A PIC 9 VALUE 1.

  88 Rock-A VALUE 1.

  88 Paper-A VALUE 2.

  88 Scissors-A VALUE 3.

  01 PlayerGuess-B PIC 9 VALUE 2.

  88 Rock-B VALUE 1.

  88 Paper-B VALUE 2.

  88 Scissors-B VALUE 3.

  106

  Chapter 5 ■ Control StruCtureS: SeleCtion

  PROCEDURE DIVISION.

  BEGIN.

  DISPLAY "Guess for player A (1=rock, 2=scissors, 3=paper) : "

  WITH NO ADVANCING

  ACCEPT PlayerGuess-A

  DISPLAY "Guess for player B (1=rock, 2=scissors, 3=paper) : "

  WITH NO ADVANCING

  ACCEPT PlayerGuess-B

  EVALUATE TRUE ALSO TRUE

  WHEN Rock-A ALSO Rock-B DISPLAY "Draw"

  WHEN Rock-A ALSO Paper-B DISPLAY "Player B wins"

  WHEN Rock-A ALSO Scissors-B DISPLAY "Player A wins"

  WHEN Paper-A ALSO Rock-B DISPLAY "Player A wins"

  WHEN Paper-A ALSO Paper-B DISPLAY "Draw"

  WHEN Paper-A ALSO Scissors-B DISPLAY "Player B wins"

  WHEN Scissors-A ALSO Rock-B DISPLAY "Player B wins"

  WHEN Scissors-A ALSO Paper-B DISPLAY "Player A wins"

  WHEN Scissors-A ALSO Scissors-B DISPLAY "Draw"

  WHEN OTHER DISPLAY "Evaluate problem"

  END-EVALUATE

  STOP RUN.

  prOGraMMING eXerCISe aNSWer

  Listing 5-11. Simple Calculator

  IDENTIFICATION DIVISION.

  PROGRAM-ID. Listing5-11.

  AUTHOR. Michael Coughlan.

  *> Accepts two numbers and an operator from the user.

  *> Applies the appropriate operation to the two numbers.

  DATA DIVISION.

  WORKING-STORAGE SECTION.

  01 Num1 PIC 9 VALUE 7.

  01 Num2 PIC 9 VALUE 3.

  01 Result PIC --9.99 VALUE ZEROS.

  01 Operator PIC X VALUE "-".

  88 ValidOperator VALUES "*", "+", "-", "/".

  PROCEDURE DIVISION.

  CalculateResult.

  DISPLAY "Enter a single digit number : " WITH NO ADVANCING

  ACCEPT Num1

  DISPLAY "Enter a single digit number : " WITH NO ADVANCING

  107

  Chapter 5 ■ Control StruCtureS: SeleCtion

  ACCEPT Num2

  DISPLAY "Enter the operator to be applied : " WITH NO ADVANCING

  ACCEPT Operator EVALUATE Operator

  WHEN "+" ADD Num2 TO Num1 GIVING Result

  WHEN "-" SUBTRACT Num2 FROM Num1 GIVING Result

  WHEN "*" MULTIPLY Num2 BY Num1 GIVING Result

  WHEN "/" DIVIDE Num1 BY Num2 GIVING Result ROUNDED

  WHEN OTHER DISPLAY "Invalid operator entered"

  END-EVALUATE

  IF ValidOperator

  DISPLAY "Result is = ", Result

  END-IF

  STOP RUN.

  108

  Chapter 6

  Control Structures: Iteration

  The previous chapter dealt with COBOL’s selection constructs: IF and EVALUATE. In this chapter, you examine the last of the classic structured programming constructs: iteration.

  In almost every programming job, there is some task that needs to be done over and over again. The job of

  processing a file of records is an iteration of this task: get and process record. The job of getting the sum of a stream of numbers is an iteration of this task: get and add number. The job of searching through an array for a particular value is an iteration of this task: get next element and check element value. These jobs are accomplished using iteration constructs.

  Other languages support a variety of iteration constructs, each designed to achieve different things. In Modula-2

  and Pascal, While..DO and Repeat..Until implement pre-test and post-test iteration. The for loop is used for counting iteration. The many C-language derivatives use while and do..while for pre-test and post-test iteration, and again the for loop is used for counting iteration.

  COBOL supports all these different kinds of iteration, but it has only one iteration construct: the PERFORM verb (see Table 6-1). Pre-test and post-test iteration are supported by PERFORM WITH TEST BEFORE and PERFORM WITH TEST

  AFTER. Counting iteration is supported by PERFORM..VARYING. COBOL even has variations that are not found in other languages. PERFORM..VARYING, for instance, can take more than one counter, and it has both pre-test and post-test variations. Whereas in most languages the loop target is an inline block of code, in COBOL it can be either an inline block or a named out-of-line block of code.

  Table 6-1. Iteration Constructs and Their COBOL Equivalents

  C, C++, Java

  Modula-2, Pascal

  COBOL

  Pre-test

  while {}

  While..DO

  PERFORM WITH TEST BEFORE UNTIL

  Post-test

  do {}

  Repeat..Until

  PERFORM WITH TEST AFTER UNTIL

  while

  Counting

  for

  For..DO

  PERFORM..VARYING..UNTIL

  Paragraphs Revisited

  In the PROCEDURE DIVISION, a paragraph is a block of code to which you have given a name. A paragraph begins with the paragraph name (see Example 6-1) and ends when the next paragraph or section name is encountered or when the end of the program text is reached. The paragraph name must always be terminated with a period (full stop).

  There may be any number of statements and sentences in a paragraph; but there must be at least one sentence, and the last statement in the paragraph must be terminated with a period. In fact, as I mentioned in the previous chapter, there is a style of COBOL programming called the minimum-period style1-2, which you should adopt. This style suggests that there should be only one period in the paragraph. It is particularly important to adhere to this style when coding inline loops, because a period has the effect of delimiting the scope of an inline PERFORM.

  109

  Chapter 6 ■ Control StruCtureS: IteratIon

  Example 6-1. Two Paragraphs: ProcessRecord Ends Where ProcessOutput Begins

  ProcessRecord.

  DISPLAY StudentRecord

  READ StudentFile

  AT END MOVE HIGH-VALUES TO StudentRecord

  END-READ.

  ProduceOutput.

  DISPLAY "Here is a message".

  The PERFORM Verb

  Unless it is instructed otherwise, a computer running a COBOL program processes the statements in sequence, starting at the first statement of the PROCEDURE DIVISION and working its way down through the program until the STOP RUN, or the end of the program text, is reached. The PERFORM verb is one way of altering the sequential flow of control in a COBOL program. The PERFORM verb can be used for two major purposes;

  • To transfer control to a designated block of code

  • To execute a block of code iteratively
>
  Whereas the other formats of the PERFORM verb implement iteration of one sort or another, this first format is used to transfer control to an out-of-line block of code—that is, to execute an open subroutine. You have probably have come across the idea of a subroutine before. A subroutine is a block of code that is executed when invoked by name.

  Methods, procedures, and functions are subroutines. You may not have realized that there are two types of subroutine:

  • Open subroutines

  • Closed subroutines

  If you have learned BASIC, you may be familiar with open subroutines. If you learned C, Modula-2, or Java, you are probably familiar with closed subroutines.

  Open Subroutines

  An open subroutine is a named block of code that control (by which I mean program statement execution) can fall into, or through. An open subroutine has access to all the data items declared in the main program, and it cannot declare any local data items.

  Although an open subroutine is normally executed by invoking it by name, it is also possible, unless you are careful, to fall into it from the main program. In BASIC, the GOSUB and RETURN commands allow you to implement open subroutines. Example 6-2 is a short BASIC program that illustrates the fall-through problem. Two outputs are provided: one where the EXIT statement prevents fall-through and the other where control falls through into OpenSub because the EXIT statement has been removed.

  110

  Chapter 6 ■ Control StruCtureS: IteratIon

  Example 6-2. Open Subroutine in Yabasic3 Showing Output With and Without the EXIT Statement REM Demonstrates Open subroutines in Yabasic

  REM When the EXIT is removed, control falls

  REM through into OpenSub

  REM Author. Michael Coughlan

  PRINT "In main"

  GOSUB OpenSub

  PRINT "Back in main"

  EXIT

  LABEL OpenSub

  PRINT "In OpenSub"

  RETURN

  In some legacy COBOL programs, falling through the program from paragraph to paragraph is a deliberate

  strategy. In this scheme, which has been called gravity-driven programming, control falls through the program until it encounters an IF and GO TO combination that drives it to a paragraph in the code above it; after that, control starts to fall through the program again. Example 6-3 provides an outline of how such a program works (P1, P2, P3, and P4 are paragraph names).

 

‹ Prev