Why Repetition?: Read 8 Real Numbers and Compute Their Average
Why Repetition?: Read 8 Real Numbers and Compute Their Average
Why Repetition?
--------------------------------------------------------------------
REAL X , SUM
SUM = 0
repeat the following two statements 1000 times
READ*, X
SUM = SUM + X
------------------------------------------------------------------------
REAL X, SUM
SUM = 0
repeat the following two statements N times
READ*, X
SUM = SUM + X
--------------------------------------------------------------------
INTEGER N
REAL X, SUM
SUM = 0
N = 100
DO 5 K = 1, N, 1
READ*, X
SUM = SUM + X
5 CONTINUE
The DO LOOP
DO N index = initial, limit, increment
N CONTINUE
DO 99 K = 15, 4 , -2
PRINT*, K
99 CONTINUE
END
15
13
11
9
7
5
Examples on The DO Loop:
INTEGER SUM, K
SUM = 0
DO 11 K = 1, 7
SUM = SUM + 3 **K
11 CONTINUE
PRINT*, 'SUM = ' , SUM
END
THE CONTINUE STATEMENT
• Before each iteration, the index is checked to see if it has passed the limit.
• If the index passed the limit, the loop iterations stop. Otherwise, the next
iteration begins.
DO 15 K = 1 , 5 , 2
PRINT*, K
15 CONTINUE
• The loop above is executed 3 times. The value of K outside the loop is 7
• If the increment is positive the initial must be less than the limit. otherwise the
loop body will not be executed.
• If the increment is negative the limit must be less than the initial. Otherwise the
loop body will not be executed.
• If the values of the initial and the limit are equal, the loop executes only
once.
DO loops rules
• Index of DO loop must be a variable of either INTEGER or REAL types.
• The value of the DO loop index cannot be modified inside the loop.
INTEGER M
DO 124 M = 1 , 10 , 0.5
PRINT*, M
124 CONTINUE
PRINT*, M
END
• The index after the loop is the value that has been incremented
and found to pass the limit.
INTEGER M, J
DO 111 M = 1, 2
DO 122 J = 1, 6, 2
PRINT*, M, J
122 CONTINUE
111 CONTINUE
END
The output of the above program is:
1 1
1 3
1 5
2 1
2 3
2 5
Nested DO Loops
Example: Consider the following program.
INTEGER M, J
DO 111 M = 1, 2
DO 122 J = 1, 6, 2
PRINT*, M, J
122 CONTINUE
PRINT*, M, J
111 CONTINUE The output of the above program is:
PRINT*, M, J
END 1 1
1 3
1 5
INTEGER M, J 1 7
DO 111 M = 1, 2 2 1
DO 122 J = 1, 6, 2 2 3
122 PRINT*, M, J 2 5
111 PRINT*, M, J 2 7
PRINT*, M, J 3 7
END
Exercises The output
DO 1 K = 2, 3 2 1
DO 2 M = 1, 4, 2 2 3
2 PRINT*, K, M 2 5
1 PRINT*, K, M 3 1
PRINT*, K, M 3 3
END 3 5
--------------------------------------- 4 5
DO 1 K = 2, 3
DO 2 M = 1, 4, -2
2 PRINT*, K, M 2 1
1 PRINT*, K, M 3 1
PRINT*, K, M 4 1
END
-----------------------------------------
DO 1 K = 2, 3, -1
DO 2 M = 1, 4, 2
2 PRINT*, K, M 2 ???
1 PRINT*, K, M
PRINT*, K, M
END
What is the output of the following program?
INTEGER K, M, N
N=0
DO 10 K = -5, 5
N=N+2
DO 20 M = 3, 1 The output
N=N+3 33
20 CONTINUE
N=N+1
10 CONTINUE
PRINT*, N
END
The WHILE LOOP
DO WHILE ( condition )
Block of statements
END DO
Examples on The WHILE LOOP:
Solution:
• Read the grades of the students , the last input would include a negative grade
• Compute and print the average and the number of students in the class.
Solution:
Solution:
REAL N, SUM
SUM = 0.0
DO 100 N = 1, 99
SUM = SUM + N / (N + 1)
100 CONTINUE
PRINT*, 'SUM = ' , SUM
END
Example 4: Series Summation using a WHILE loop:
Question: Write a FORTRAN program which calculates
the sum of the following series :
Solution:
REAL N, SUM
N=1
SUM = 0.0
DO WHILE(N .LE. 99)
SUM = SUM + N / (N + 1)
N=N+1
END DO
PRINT*, 'SUM = ' , SUM
END
Example 5: Alternating Sequences/ Series: Alternating
sequences, or series, are those which have terms alternating
their signs from positive to negative. In this example, we find
the sum of an alternating series.
Question: Write a FORTRAN program that evaluates the
following series to the 100th term.
1 - 3 + 5 - 7 + 9 - 11 + 13 - 15 + 17 - 19 + ...
Solution:
Solution:
Solution:
INTEGER M, FACT
PRINT*, 'ENTER AN INTEGER NUMBER'
READ*, M
PRINT*, 'INPUT: ', M
IF (M .GE. 0) THEN
FACT = 1
DO WHILE (M .GT. 1)
FACT = FACT *M
M=M-1
END DO
PRINT*, 'FACTORIAL IS ', FACT
ELSE
PRINT*, 'NO FACTORIAL FOR NEGATIVES'
ENDIF
END
Nested WHILE Loops
Example: Consider the following program.
INTEGER M, J INTEGER M, J
DO 111 M = 1, 2 M=1
DO 122 J = 1, 6, 2 DO WHILE ( M .LE. 2)
PRINT*, M, J J=1
122 CONTINUE DO WHILE (J .LE. 6)
111 CONTINUE PRINT*, M, J
END J=J+2
END DO
M=M+1
END DO
END
The output of the above program is:
1 1
1 3
1 5
2 1
2 3
2 5
Infinite loop
INTEGER X
X=5
DO WHILE ( X .GT. 0)
PRINT*, X
X=X+1
END DO
END
Implied Loops
Output:
100 99 98 97 96 95 94 93 92 91 90 89 88 87
Nested Implied Loops
Example: Consider the following program.
INTEGER M, J
DO 111 M = 1, 2
DO 122 J = 1, 6, 2
PRINT*, M, J
122 CONTINUE
111 CONTINUE
END
PRINT*, ((M, J, J = 1, 6, 2) , M = 1, 2)
1 1 1 3 1 5 2 1 2 3 2 5
Nested Implied Loops
Example: Consider the following program.
INTEGER M, J
DO 111 M = 1, 2
DO 122 J = 1, 6, 2
PRINT*, M, J
122 CONTINUE
PRINT*, M, J
111 CONTINUE
PRINT*, M, J
END
PRINT*, ((M, J, J = 1, 6, 2) , M, J, M = 1, 2) , M, J
1 1 1 3 1 5 1 7 2 1 2 3 2 5 2 7 3 7
Exercises
PRINT*, ((K, M, M = 1, 4, 2) , K, M, K = 2, 3) , K, M
2 1 2 3 2 5 3 1 3 3 3 5 4 5
2 1 3 1 4 1
2 ???
Repetition Constructs in Subprograms
Exercise
C FUNCTION SUBPROGRAM
LOGICAL FUNCTION PRIME(K)
INTEGER N, K
PRIME = . TRUE .
DO 10 N = 2, K / 2
IF (MOD(K , N) .EQ. 0) THEN
PRIME = . FALSE .
RETURN
ENDIF
10 CONTINUE The output
RETURN
END T F
C MAIN PROGRAM
LOGICAL PRIME
PRINT*, PRIME (5), PRIME (8)
END