COBOL/Loops
Loops in COBOL
editIn computer science, a loop is a sequence of statements which is specified once but which may be carried out several times in succession. The code "inside" the loop (the body of the loop) is executed a specified number of times, or once for each of a collection of items, or until some condition is met, or indefinitely.
COBOL provides a variety of loop control statements, including:
- PERFORM THRU—execute a series of paragraphs by giving the first and last paragraph names in the sequence. After executing the last paragraph, the control is returned.
- PERFORM UNTIL—a paragraph is executed until the given condition becomes true. The default condition is ‘With test before’ and it indicates that the condition is checked before executing statements in a paragraph.
- PERFORM TIMES—a paragraph will be executed the number of times specified.
- PERFORM VARYING—a paragraph will be executed until the condition in UNTIL phrase becomes true.
The example program demonstrates both the PERFORM UNTIL structure and the PERFORM VARYING structure. Note the counter is incremented in the UNTIL-LOOP paragraph, and not directly within the PERFORM UNTIL loop statement.
The loops lesson in the Wikiversity course Programming Fundamentals provides additional background, readings, multimedia resources, key terms, and activities related to understanding and using loops.
Please study those materials to better understand and extend the following example program.
loops.cbl
edit*> This program demonstrates Until (While) and Varying (For) loop counting using user-designated
*> start, stop, and increment values.
*>
*> References:
*> https://www.tutorialspoint.com/cobol/index.htm
*> https://open-cobol.sourceforge.io/doc/gnucobol.html
IDENTIFICATION DIVISION.
PROGRAM-ID. LOOPS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BEGIN PIC 9(2).
01 FINISH PIC 9(2).
01 INCREMENT PIC 9(2).
01 COUNTER PIC 9(2).
PROCEDURE DIVISION.
MAIN.
DISPLAY "Enter starting value:".
ACCEPT BEGIN.
DISPLAY "Enter ending value:".
ACCEPT FINISH.
DISPLAY "Enter increment value:".
ACCEPT INCREMENT.
DISPLAY "UNTIL-LOOP".
SET COUNTER TO BEGIN.
PERFORM UNTIL-LOOP UNTIL COUNTER > FINISH.
DISPLAY "VARYING-LOOP"
PERFORM VARYING-LOOP VARYING COUNTER FROM BEGIN BY INCREMENT UNTIL COUNTER > FINISH.
STOP RUN.
UNTIL-LOOP.
DISPLAY COUNTER.
ADD INCREMENT TO COUNTER.
VARYING-LOOP.
DISPLAY COUNTER.
Try It
editCopy and paste the code above into one of the following free online development environments or use your own Loops compiler / interpreter / IDE.
Assignment
editPart 1:
In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n:
For example,
The value of 0! is 1, according to the convention for an empty product.
Modify the example program to compute the factorial of a number supplied as input.
Run and test the program. Be sure to use 0, negative numbers, non-integers, and large numbers as test cases.
Part 2:
In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
and
for n > 1.
The beginning of the sequence is thus:
Modify the example program to compute and display the first n numbers in the Fibonacci sequence.
Run and test the program.
Part 3:
An amortization schedule is a table detailing each periodic payment on an amortizing loan (typically a mortgage), as generated by an amortization calculator. Amortization refers to the process of paying off a debt (often from a loan or mortgage) over time through regular payments. A portion of each payment is for interest while the remaining amount is applied towards the principal balance. The percentage of interest versus principal in each payment is determined in an amortization schedule. The schedule differentiates the portion of payment that belongs to interest expense from the portion used to close the gap of a discount or premium from the principal after each payment.
The formula for calculating the payment amount is shown below.
where
- A = payment Amount per period
- P = initial Principal (loan amount)
- r = interest rate per period
- n = total number of payments or periods
Example: What would the monthly payment be on a 5-year, $20,000 car loan with a nominal 7.5% annual interest rate?
P = $20,000
r = 7.5% per year / 12 months = 0.625% per period
n = 5 years * 12 months = 60 total periods
The resulting amortization schedule is partially shown here:
Payment | Amount | Interest | Principal | Balance |
---|---|---|---|---|
1 | 400.76 | 125.00 | 275.76 | 19,724.24 |
2 | 400.76 | 123.28 | 277.48 | 19446.76 |
3 | 400.76 | 121.54 | 279.22 | 19167.54 |
... | ... | ... | ... | ... |
Modify the example program to compute and display an amortization table. The program should request the payment amount per period, initial principal (loan amount), interest rate per, period, and the total number of payments or periods as inputs.
Run and test the program