Fortran Lecture 3
Fortran Lecture 3
Deterministic DO loops
The number of times the section is repeated is stated explicitly.
Non-deterministic DO loops
The number of repetitions is not stated in advance.
Fortran 90 Control Structures 2/10
If step is positive
The control-var receives the value of initial
If step is negative
The control-var receives the value of initial
Count down loop always requires a step. That is the loop will not
work without a specified step
Deterministic Do loop Example 5/10
What do we need ?
If construct
Deterministic Do loop
Dimension attribute
PROGRAM func x0
! this program solve a function with 11 data point
IMPLICIT NONE
INTEGER, PARAMETER :: i start = 0, i end = 10
INTEGER :: icount
REAL, DIMENSION (i start : i end) :: f xy !array declaration
DO icount = i start, i end !do loop with step =1
IF ( icount < 4 ) THEN !1st condition
f xy (icount) = 0
ELSE IF ( icount >= 4 .AND. icount <= 5) THEN !2nd condition
f xy ( icount ) = 0.1 * (icount - 4)
ELSE IF ( icount >= 5 .AND. icount <= 6 ) THEN !3rd condition
f xy( icount ) = 20 - 0.1 * ( icount - 4 )
ELSE !last condition
f xy ( icount ) = 0
END IF
PRINT*, icount, f xy ( icount )
END DO
END PROGRAM func x0
Fortran 90 Control Structures 6/10
DO
< statement body >
IF (< logical-expression > ) EXIT
< statement body >
END DO
DO
< statement body >
IF (< logical-expression > ) CYCLE
< statement body >
END DO
X (−1)n ∞
pi 1 1 1 1
= 1 − + − + − ...
4 3 5 7 9 2n + 1
n=0