Just like an IF-THEN-ELSE-END IF can contain another IF-THEN-ELSE-END IF (see nested IF for the details), a DO-loop can contain other DO-loops in its body. The body of the contained DO-loop, usually referred to as the nested DO-loop, must be completely inside the containing DO-loop. Note further that an EXIT statement only brings the control out of the inner-most DO-loop that contains the EXIT statement.
Suppose we have the following nested DO loops:
Each iteration of the outer DO starts with statements-1. When the control reaches the inner DO, statements-2 is executed until some condition of the inner DO brings the control out of it. Then, statements-3 is executed and this completes one iteration. Any EXIT in the inner DO brings the control out of the inner DO to the first statement in statement-3.DO statements-1 DO statement-2 END DO statement-3 END DO
The following are a few simple examples:
- In the following nested loops, the outer one has i running from 1 to 9 with step size 1. For each iteration, say the i-th one, the inner loop iterates 9 times with values of j being 1, 2, 3, 4, 5, 6, 7, 8, 9. Therefore, with i fixed, the WRITE is executed 9 times and the output consists of i*1, i*2, i*3, ..., i*9.
INTEGER :: i, j DO i = 1, 9 DO j = 1, 9 WRITE(*,*) i*j END DO END DO
- The following shows a nested DO-loop. The outer one lets u run from 2 to 5. For each u, the inner DO lets v runs from 1 to u-1. Therefore, when u is 2, the values for v is from 1 to 1. When u is 3, the values for v are 1 and 2. When u is 4, the values for v are 1, 2, and 3. Finally, when u is 5, the values for v are 1, 2, 3 and 4.
INTEGER :: u, v INTEGER :: a, b, c DO u = 2, 5 DO v = 1, u-1 a = 2*u*v b = u*u - v*v c = u*u + v*v WRITE(*,*) a, b, c END DO END DO
u Values for v 2 1 3 1 2 4 1 2 3 5 1 2 3 4
u v a b c 2 1 4 3 5 3 1 6 8 10 2 12 5 13 4 1 8 15 17 2 16 12 20 3 24 7 25 5 1 10 24 26 2 20 21 29 3 30 16 34 4 40 9 41 - It is obvious that the inner DO-loop computes the sum of all integers in the range of 1 and i (i.e., Sum is equal to 1+2+3+...+i). Since i runs from 1 to 10, the following loop computes ten sums: 1, 1+2, 1+2+3, 1+2+3+4, ...., 1+2+3+...+9, and 1+2+3+...+9+10.
INTEGER :: i, j, Sum DO i = 1, 10 Sum = 0 DO j = 1, i Sum = Sum + j END DO WRITE(*,*) Sum END DO
- The program below uses Newton's method for computing the square root of a positive number. In fact, it computes the square roots of the numbers 0.1, 0.1, ..., 0.9 and 1.0.
REAL :: Start = 0.1, End = 1.0, Step = 0.1 REAL :: X, NewX, Value Value = Start DO IF (Value > End) EXIT X = Value DO NewX = 0.5*(X + Value/X) IF (ABS(X - NewX) < 0.00001) EXIT X = NewX EBD DO WRITE(*,*) 'The square root of ', Value, ' is ', NewX Value = Value + Step END DO
REAL :: Start = 0.1, End = 1.0, Step = 0.1 REAL :: X, NewX, Value Value = Start DO IF (Value > End) EXIT ! ! the inner loop computes the result in NewX ! WRITE(*,*) 'The square root of ', Value, ' is ', NewX Value = Value + Step END DO
No comments:
Post a Comment
=(*_*)------------------------(^_^)=
:::::|berkomentar dengan sopan adalah akhlak kemulian|:::::