DO Loop
Section
5 of the FORTRAN95
Manual introduces the topic of REPETITION in FORTRAN. One way to repeat one
or more statements is to use DO loop.
The syntax
of DO loop:
DO variable = initial_value, final_value[, increment]
[statements]
END DO
or
DO label variable = initial_value, final_value[, increment]
[statements]
label CONTINUE
If increment is not specified, it is
assumed to be 1.
For example
PROGRAM LINES
! Illustration of
DO-loops
IMPLICIT NONE
INTEGER L ! a counter
DO L = 1, 100 ! start of repeated section
PRINT *, L, ' I
must not talk in class'
END DO ! end of repeated section
END PROGRAM LINES
or
PROGRAM LINES
! Illustration of
DO-loops
IMPLICIT NONE
INTEGER L ! a counter
DO 100 L = 1, 100 ! start of repeated section
PRINT *, L, ' I
must not talk in class'
100 contunue ! end of repeated section
END PROGRAM LINES
Here is the flowchart of this
program; the DO loop is inside the dotted lines.
To
simplify the flowchart and show the DO loop more clearly, a new box can be used
for a Do loop.
It
is easy to use a DO loop to calculate a formula which uses ∑ or an addition of a series of
numbers. For example, 1+2+…..+100 or :
sum = 0
DO
I = 1, 100, 1
sum = sum + i
END
DO
Do
loops can be nested, one inside another.
For example
PROGRAM LINES
! Illustration of
DO-loops
IMPLICIT NONE
INTEGER L, K ! counters
DO L = 1, 100 ! start of repeated section
DO K = 1, 100 ! start of repeated section
PRINT
*, L,K, ' I must not talk in class'
END DO ! end of repeated section - K
END DO ! end of repeated section - L
END PROGRAM LINES
Here
is the flow chart for two nested Do loops:
loop
Exercises
1.
(optional) Draw flowchart of the following programs 1)
- 3).
2.
What are the outputs of the
following programs 1) - 3)?
3.
(optional) Check your answers by running the programs.
Use the program name as the FORTRAN file name (with the extension .f95) and run
the programs on the Salford Plato IDE. (Remember to add your name, group, course and
date at the beginning of each program in comment lines.)
1)
Program prog11
do 30 i = 10,2,-2
write(*,*) i
30 continue
stop
end
2)
PROGRAM NESTED
! Illustration of
nested DO-loops
IMPLICIT NONE
INTEGER I, J !
loop counters
DO I = 1, 6 !
start of outer loop
PRINT *, 'Outer
loop with I = ', I
DO J = 1, 3 !
start of inner loop
PRINT *, ' I, J =
', I, J
END DO
PRINT * !
a blank line
END
DO !
end of repeated section
END PROGRAM
NESTED
3)
PROGRAM XLOOP
! Illustration of
non-integer values
IMPLICIT NONE
INTEGER I !
loop counter
REAL DX !
increment
REAL X0 !
non-integral initial value
REAL X !
value to be output
X0 = 0.5 !
set initial value
DX = 0.3 !
set increment
DO I = 1, 10 !
start of repeated section
X = X0 + (I - 1) * DX ! actual value to be output
PRINT *, X
END
DO !
end of repeated section
END PROGRAM XLOOP
4.
Use a DO loop to display a temperature conversion table, between -100 C˚ to 100 C˚ at every 20 C˚, as shown:
Degrees Fahrenheit = Degrees Celsius ´ (9/5) +
32
C |
F |
-100 |
-148 |
-80 |
-112 |
-60 |
-76 |
-40 |
-40 |
-20 |
-4 |
0 |
32 |
20 |
68 |
40 |
104 |
60 |
140 |
80 |
176 |
100 |
212 |
FORTRAN file name: C2F.f95