[go: up one dir, main page]

0% found this document useful (0 votes)
51 views13 pages

Lec4 1

The document discusses repetition structures in Fortran, specifically counter-controlled DO loops which allow repeated execution of a statement sequence a specified number of times by initializing a control variable, comparing it to a limit at each step, and incrementing it by a step size until the comparison is no longer true. It provides examples of DO loops, including nested loops, and introduces the DO-EXIT construct for more flexible repetition when the number of iterations is unknown prior to execution.

Uploaded by

heshambm
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)
51 views13 pages

Lec4 1

The document discusses repetition structures in Fortran, specifically counter-controlled DO loops which allow repeated execution of a statement sequence a specified number of times by initializing a control variable, comparing it to a limit at each step, and incrementing it by a step size until the comparison is no longer true. It provides examples of DO loops, including nested loops, and introduces the DO-EXIT construct for more flexible repetition when the number of iterations is unknown prior to execution.

Uploaded by

heshambm
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/ 13

Repetitive Execution

The repetition structure or loop makes possible the


repeated execution of one or more statements,
called the body of the loop.

There are two basic types of repetition:

1. Repetition controlled by a counter in which the


body of the loop is executed once for each value
of some control variable in a specified range of
values.
2. Repetition controlled by a logical expression in
which the decision to continue or to terminate
repetition is determined by the value of some
logical expression. 19
Counter-Controlled DO Loops
DO constructs for counter-controlled loops have the following form:

DO control-variable = initial-value, limit, step-size


statement-sequence
END DO

where:
control-variable is an integer variable,
initial-value, limit, and step-size are integer expressions.

step-size must be non-zero.


step-size may be omitted, in which case it is assumed to be 1.

20
Counter-Controlled DO Loops
DO control-variable
(cont.)
initial-value limit step-size
= , ,
statement-sequence
END DO

When this loop is executed:


1. The control variable is assigned the initial value.
2. The control variable is compared with the limit to see if it is:
• less than or equal to the limit, for a positive step size
• greater than or equal to the limit, for a negative step size
3. If so, the sequence of statements, called the body of the loop, is
executed
4. the step size is added to the control variable, and step 2 is repeated.
Otherwise, repetition terminates and control is passed to the statement
following the 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

READ(*,*) number DO I = 1, n/10


DO I = 1, number x = I*10
sum = sum + I END DO
END DO

DO I= -n,(2*n-1), 2 DO count = 0, n, interval


x = SIN(REAL(I)) sum = sum + count
END DO END DO

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

where either statement-sequence1 or statement-sequence2 can be omitted.


If (logical-expression) evaluates to true, the loop ends, and control is passed to
the statement following the END DO statement.

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

A DO-WHILE Loop is an alternative form of a DO-EXIT Loop

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

You might also like