0% found this document useful (0 votes)
10 views12 pages

Fortran Lecture 3

Uploaded by

Manozer Mensah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Fortran Lecture 3

Uploaded by

Manozer Mensah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Fortran 90 Control Structures 1/10

DO and DO WHILE loops

If a block of code is to to performed repeatedly it is put inside a DO loop


There are two basic types of DO loops:

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

DO loops - ( Deterministic DO loop)

The number of times the section is repeated is stated explicitly.


The syntax is as follows:
DO control-var = initial, final [ , step ]
< Statements >
END DO
control-var is an integer variable, initial, final and step are integer
or integer expression; however, step cannot be zero.
If step is omitted, its default value is 1
statements are executable statement of the DO LOOP
The control-var runs from initial in increments of step until it
reaches the value final and then the loop terminates.
If step is positive, then DO counts up; if step is negative, then DO
counts down
Fortran 90 Control Structures 3/10

DO loops - ( Deterministic DO loop)

If step is positive
The control-var receives the value of initial

If the value of the control-var is less than or equal to the value of


final, the statements part is executed. Then, the value of step is
added to control-var, and goes back and compares the value of
control-var and final.

If the value of control-var is greater than the value of final, the


DO-loop completes and the statement following END DO is
executed.
Fortran 90 Control Structures 4/10

DO loops - ( Deterministic DO loop)

If step is negative
The control-var receives the value of initial

If the value of control-var is greater than or equal to the value of


final, the statements part is executed. Then, the value of step is
added to control-var, goes back and compares the value of
control-var and final.

If the value of control-var is less than the value of final, the


DO-loop completes and the statement following END DO is
executed.

Count down loop always requires a step. That is the loop will not
work without a specified step
Deterministic Do loop Example 5/10

Sovle the function f(x) numerically in the domain 0 ≤ x ≤ 10




 0 x <4
0.1(x − 4) 4≤x ≤5

f (x) =

 20 − 0.1(x − 4) 5≤x ≤6
0 x >6

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 loops - ( Non-deterministic DO loop)

The number of repetitions is not stated in advance. The enclosed section


is repeated until some condition is or is not met. Note that this loop can
be infinite without the condition.
This may be done in two alternative ways:
The first requires a logical reason for stopping looping
The second requires a logical reason for cycling looping.

Non-deterministic DO-loops are particularly good for:


1 summing power series (looping stops when the absolute value of a
term is less than some given tolerance)
2 single-point iteration (looping stops when the change is less than a
given tolerance)
Fortran 90 Control Structures 7/10

Non-deterministic Do loop - ( with Exit condition)

In these, the loop is terminated by a conditional exit statement, ie. an IF


statement or logical IF which executes exit action.
The syntax is as follows:

DO
< statement body >
IF (< logical-expression > ) EXIT
< statement body >
END DO

The loop continues until some logical expression evaluates as TRUE.


Then it jumps out of the loop and continues with the code after the
loop. In this form a TRUE result tells you when to stop looping.
Fortran 90 Control Structures 8/10

Non-deterministic Do loop - ( with Cycle condition)

The CYCLE statement starts the next iteration (ie. executing


statements again).
The syntax is as follows:

DO
< statement body >
IF (< logical-expression > ) CYCLE
< statement body >
END DO

The action of the IF statement if it is TRUE is to cause the program to


cycle immediately to the next iteration of the loop, skipping any
remaining statements in the current cycle.
Fortran 90 Control Structures 9/10

Non-deterministic Do loop - ( Exit and Cycle Example)

PROGRAM exit cycle exam


IMPLICIT NONE
INTEGER :: i
INTEGER, PARAMETER :: istart = 1, iend = 5, ilimit=3
DO i = istart, iend
IF (i == ilimit) EXIT ! IF statement condition to exit loop
PRINT*, i
END DO
END PROGRAM exit cycle exam
Please re-run the program by replacing the EXIT with CYCLE and exam
the difference in the output.
Non-deterministic Do loop Example 10/10
Pi ( π ) can be estimated from the series

X (−1)n ∞
pi 1 1 1 1
= 1 − + − + − ...
4 3 5 7 9 2n + 1
n=0

We could re-write the series as a sum of all positive terms:



X 8
π= an where an =
(4n + 1)(4n + 3)
n=0

Write a fortran program to evaluate pi from the series until an term is


less than a given threshold (torelance).
What do we need?
condition to exit the loop
set the torelance
increments of the loop
PROGRAM cal pi
!to calculate pi from series
IMPLICIT NONE
REAL, PARAMETER :: eps = 0.001 ! torelance value
INTEGER :: ncount ! loop counter
REAL :: pival ! store the pi value
REAL :: series val ! store the series value
ncount = 0 ! assign initial counter
pival = 0.0 ! assign initial pi value
DO
series val = 8.0 / ( ( 4 * ncount + 1 ) * ( 4 * ncount + 3 ) )
pival = pival + series val ! update the pi value
ncount = ncount + 1 !update the counter
IF ( series val < eps ) EXIT ! condition to exit loop
END DO
PRINT 10, ’# of steps = ’, ncount, ’ ,value of pi = ’, pival
10 FORMAT (a,I2,2x,a,F6.4) !print format
END PROGRAM cal pi

You might also like