7 LOOPS

In all programming languages there are three general types of structures: sequence, selection, and looping. A sequence structure contains steps that are executed sequentially while a selection structure tests a condition to determine which steps are to be executed. Looping, the ability to repeat a block of statements, is the primary structure used to solve massive computations. It is used for mathematical techniques requiring iterative processing and, also, the processing of different data sets using the same, or similar, actions. It is in applications requiring a loop structure that computers are most useful and where programming is essential to solving the problem.

Example

A mathematical process that can be accomplished by using a DO loop is averaging a set of numbers.
Problem Statement
Compute the average of a set of numbers.

Input/Output Description
The user is prompted for and enters the number of values to be averaged.
The output is the average of the set of numbers.

Algorithm
  1. Prompt for and get the number of values in the set of numbers to be averaged.
  2. Initialize the value of sum to 0.0.
  3. Enter a number from the set.
  4. Add the number to sum.
  5. Repeat steps two and three until all of the numbers are entered.
  6. Divide the total by the number of values in the set.
  7. Display the average.

CODE
c FORTRAN 77
 
c This program averages a series of numbers input from the keyboard.
 
        PROGRAM average
 
c Type variables.
        REAL data, sum, avg
        INTEGER num,i
 
c Prompt for and enter the number of numbers to average.
        PRINT *,'Enter the number of numbers to average.'
        READ *,num

c Initialize sum to 0.0 
        sum = 0.0

c The loop goes from 1 to number of values to average.
        DO 100 i = 1, num
 
c Prompt for and enter a number.
           PRINT *,'Enter a value for the number'
              READ *,data
 
c Add the number to the total.
              sum = sum + data
100     CONTINUE
 
c Calculate the average.
        avg = sum/real(num)
 
c Print the results.
        PRINT *,'The average = ',avg
 
        STOP
        END

Sample Output
(4,23,45,34,67 entered.)
Enter the number of numbers to average.
4
Enter a value for the number
23
Enter a value for the number
45
Enter a value for the number
34
Enter a value for the number
67
The average =     42.2500

The syntax of a FORTRAN 77 DO loop is

DO label k = start#, final#, [increment]
instruction block
label CONTINUE

where
DO marks the beginning of the loop
label is the label number of the CONTINUE statement that marks the end of the loop
k is a variable used as the loop counter; its value may not be changed inside the loop
start# is the initial value given to the loop counter
final# is the value that determines when the DO loop is completed
increment represents the value to be added to the loop counter each time the loop is executed.

There are five steps involved in the execution of a DO loop

In the average program example, the loop counter is initialized with the statement DO 100 i = 1, num. The final# is the variable num that is entered from the keyboard. The instruction block reads the number from the keyboard and adds it to the total. The loop counter is incremented at the end of the loop. The control of the program returns to the statement DO 100 i = 1, num where the value of the loop counter is compared to final#. When the value of i is greater than num, program control is passed to the statement "avg = sum/real(num)".

Start#, final#, and increment may be constants, variables whose values have previously been assigned a value, or expressions. If there is not an increment value, it is assumed to be 1. The increment may be positive or negative.

Example 1
DO 20 i = 2,10,2
PRINT *, i
20 CONTINUE

Output
2
4
6
8
10

Example 2
DO 20 n = 25,5,-5
PRINT *,n
20 CONTINUE

Output
25
20
15
10
5

It is possible to nest DO loops, that is, to have a DO loop in the body of another DO loop. In nested DO loops, the inside loop will completely execute before the loop counter of the outside loop is incremented.

EXERCISES

1. Identify the error in each of the following code segments and correct it. Then key in each code segment as a program, compile, and execute. (Remember to include PROGRAM name, type statements, STOP, and END.)
A.
DO i 50 = 1,10
PRINT *,i
50 CONTINUE

B.
DO 75 k = 3,17,2
PRINT *,k
k = k +2
75 CONTINUE

C.
DO 50 p = 1,10
READ *, x
sum = sum + x
50 CONTINUE
avg = sum / p
PRINT *, avg
STOP

D.
READ *,x
DO 50 n = x, y
q = x/y
PRINT *, q
50 CONTINUE
2. Key in the average program, compile and execute it.

3. Modify the Roller Coaster Program to include a DO loop. Calculate the lengths of the vertical supports at distances from 2 to 40 feet from the high point of the track. Output the values for every two feet. Add a statement to determine the total length of material needs for vertical supports.

4. Write, compile, and execute a program which uses a DO loop to sum values entered from the keyboard. The number of values to be entered should be prompted for and entered by the user.

5. Write, compile, and execute a program which uses a nested DO loop to calculate and print the multiplication facts from 1 x 1 to 12 x 12.

6. Reforestation Program

Problem Description
Timber management agencies are concerned with the reforestation of a cut area. They want to determine how much of an area not to cut so the cut area is reforested in a certain number of years. A mathematical model can be determined by expressing the growth as a function of the amount of timber standing and the reforestation rate. It is assumed that the reforestation takes place at a known rate per year. If 1000 acres are left standing after an area has been cut and the reforestation rate is 0.08, then 1000+ 0.08*1000, or 1080 acres are forested at the end of the first year. At the end of the second year, the number of acres forested is 1080 + 0.08*1080. This iterative process continues for the number of years in question. Compute the number of acres forested at the end of each year for 20 years
for a given area.

Input/Output Description
The input consists of the total acres of land, the number of acres with trees, and the reforestation rate. The output should take the form of a table with a row of data for each of 20 years. Each row of information contains the number of acres reforested during that year and the total number of acres forested at the end of the year.

Algorithm

  1. Read the total number of acres, the number of uncut acres, and the reforestation rate.
  2. If the number of uncut acres is greater than the total number of acres print an error message and ask for new values for uncut acres and total acres.
  3. Print headings for the current year, number of uncut acres, and number of reforested acres.
  4. For each year, calculate the number of reforested acres and uncut acres.
  5. Print the current year, the number of reforested acres, and the number of uncut acres.

Modify the timber program in the following way: