Lec4 1
Lec4 1
where:
control-variable is an integer variable,
initial-value, limit, and step-size are integer expressions.
20
Counter-Controlled DO Loops
DO control-variable
(cont.)
initial-value limit step-size
= , ,
statement-sequence
END DO
Note that if the termination test in step 2 is satisfied initially, the body of the loop
is never executed.
21
Example
DO Number = 1, 9, 3
WRITE(*,*) Number, Number**2
END DO
WRITE(*,*) Number, Number**2
1 1
4 16
7 49
10 100
22
Counter-Controlled DO Loops
(cont.)
• If the step size of a DO loop is negative, the control variable is
decremented rather than incremented, and repetition continues as long
as the value of the control variable is greater than or equal to the limit.
• The initial values of the control variable, the limit, and the step size are
determined before repetition begins, and cannot be changed during
execution of the DO loop.
• Also, the statements within a DO loop may use the value of the control
variable, but they must not modify the value of the control variable.
• An attempt to modify the value of the control variable will result in a
compile-time error.
• The number of repetitions is calculated as the larger of: 0 and the
integer part of:
limit initial -value step -size
step -size
23
Counter-Controlled DO Loops (cont.)
Examples
IMPLICIT NONE
INTEGER x, I, n, Last
PRINT (*,*) 'enter n‘
READ (*,*) n
Last = n
DO I = n-1, 1, -1
x = I*Last
Last = x
END DO
WRITE(*,*) x
END
24
Counter-Controlled DO Loops
(cont.)
The body of a DO loop may contain another DO loop. In this case,
the second DO loop is said to be nested within the first DO loop.
Example
DO M = 1, Last_M
DO N =1, Last_N
Product = M*N
WRITE(*,*) M, “ “, N, “ “, Product
END DO
END DO
25
General DO Constructs: DO-
EXIT Constructs
• The counter-controlled DO loop described in the previous section is used to
implement a repetition structure in which the number of iterations is determined
before execution of the loop begins. For many problems, the number of
iterations cannot be determined in advance, and a more general repetition
structure is required.
• Fortran 90 provides a DO-EXIT construct that can be used to implement such
repetition structures:
DO
statement-sequence1
IF (logical-expression) EXIT
statement-sequence2
END DO
26
Flow Chart Representation of a
DO EXIT Construct
DO
statement-sequence1
logical- TRUE
expression
FALSE
statement-sequence2
next statement
27
DO-WHILE Loops
DO WHILE(logical-expression)
statement-sequence
END DO
28
Now it is time for your first program
• Write a Fortran code to solve a quadratic equation
29
What About This Program?
• Write a Fortran code to calculate the area
under the following two curves
1.Y=2x2+ 3x +2 for the interval x=[0 4]
2.The curve given in Figure 2
50 10
45 9
40 8
35 7
30 6
25 5
20 4
15 3
10 2
5 1
0 0
0 1 2 3 4 1 2 3 4 5
30
What About This Program?
• Write a Fortran code to design cross section
of open channel using the best hydraulic
section (i.e., to determine the width of the
canal and the depth of water that gives the
minimum wetted perimeter)
• The inputs of your problem are (Discharge,
Manning’s coefficient, side slope, and water
slope )
• The outputs are the width and depth
31