ACCEPT, DISPLAY,
PERFORM..TIMES, COMPUTE,
IF
Introduction
📚 Read the tutorial: COBOL Iteration Constructs
In this exercise you will amend the Iteration-Calculator.cbl program. This program is a version of the example iteration program shown in the introductory COBOL lecture. Note — use your browser's back button to get back to this page.
Download the Iteration-Calculator.cbl program and save it to the \WorkArea directory on drive D:.
Load the program into the GnuCOBOL environment.
Examining IterCalc
Compile and run the program Iteration-Calculator.cbl. Use the Animator to step through the program and examine the contents of the Variables/Identifiers.
Examine the program and attempt to answer the following questions:
- What is the maximum number of digits that the variable Num1 will hold?
- What is the maximum number of digits that Num2 will hold?
- What is the maximum number of digits that Result will hold?
- If a user enters more digits than the receiving variable will hold what happens to the number entered?
- No validation is done on values entered by the user. Examine the program and predict what will happen if an operator other than + or * is entered.
Test your answers by running the program and using the Animator to follow the flow of control through the program (Step) and to examine the contents of the variables.
Amending IterCalc
Amend the program so that instead of doing a fixed number of calculations (three in Iteration-Calculator.cbl) it asks the user to enter the number of calculations required and then executes the loop that many times.
The version of the PERFORM used in this program has the
syntax PERFORM Identifier/Literal
TIMES. That is, either an identifier or a literal can be
used to indicate how many times the loop is to be executed. The current version of
Iteration-Calculator.cbl uses a literal. You must change it so that it uses an identifier.
When run with the same data your amended program should produce the results shown below. For purposes of illustration, user input is shown enlarged and in bold.
Enter the number of calcs required :- 2 Enter First Number : 3 Enter Second Number : 3 Enter operator (+ or *) : * Result is = 9 Enter First Number : 3 Enter Second Number : 3 Enter operator (+ or *) : + Result is = 6
Making a new program
Amend the program again. This time create a new program called Countdown.cbl (use Save As from the GnuCOBOL File menu). This program should get the user's name from the keyboard and should then display a count-down before displaying the name that was entered.
The program should produce results similar to those shown in the example run below. For purposes of illustration, user input is shown enlarged and in bold.
Enter your name :- Mike Ryan Enter the count-down start value :- 05 Getting ready to display your name. 05 04 03 02 01 Your name is Mike Ryan
Conclusion
You may be wondering if COBOL provides some sort of counting loop, like the FOR loop in
other languages, which you could use in this exercise. COBOL does provide such a looping
structure in the form of the PERFORM..VARYING.
The format (simplified) for this version is PERFORM
VARYING counter
FROM start BY
step UNTIL terminatingcondition.