There are two forms of loops, the counting loop and the general loop. The syntax of the counting loop is the following:
where control-var is an INTEGER variable, initial-value and final-value are two INTEGER expressions, and step-size is also an INTEGER expression whose value cannot be zero. Note that step-size is optional. If it is omitted, the default value is 1. statements is a sequence of statements and is usually referred to as the body of the DO-loop. You can use any executable statement within a DO-loop, including IF-THEN-ELSE-END IF and even another DO-loop.DO control-var = initial-value, final-value, [step-size] statements END DO
The following are a few simple examples:
INTEGER :: Counter, Init, Final, Step READ(*,*) Init, Final, Step DO Counter = Init, Final, Step ..... END DO
INTEGER :: i, Lower, Upper Lower = .... Upper = .... DO i = Upper - Lower, Upper + Lower ..... END DO
INTEGER :: Count DO Count = -3, 4, 2 WRITE(*,*) Count, Count*Count, Count*Count*Count END DO
INTEGER, PARAMETER :: Init = 3, Final = 5 INTEGER :: Iteration DO Iteration = Init, Final WRITE(*,*) 'Iteration ', Iteration END DO
INTEGER :: a, b, c INTEGER :: List READ(*,*) a, b, c DO List = MAX(a, b, c), MIN(a, b, c), -2 WRITE(*,*) List END DO
Sum is initialized to zero. For each iteration, the value of Input, which is read in with READ, is added to the value of Sum.INTEGER :: Count, Number, Sum, Input Sum = 0 DO Count = 1, Number READ(*,*) Input Sum = Sum + Input END DO
For example, if the value of Number is 3, and the three input values are 3, 6, and 8 (on different lines), then the final value of Sum is 17 = 3+6+8. Let us look at it closely. Initially, Count receives a value of 1. Since 1 is less than the value of Number (=3), the loop body is executed. The READ statement reads the first input value 3 into Input and this value is added to Sum, changing its value from 0 to 1 (=0+1). Now, END DO is reached and the step-size (=1) is added to Count. Hence, the new value of Count is 2.
Since Count is less than Number, the second input value is read into Input. Now, Input holds 6. Then, 6 is added to the value of Sum, changing its value to 9 (=3+6). The next iteration reads in 8 and adds 8 to Sum. The new value of Sum becomes 17 (=9+8).
A simple modification can compute the average of all input numbers:
The above seems obvious. But, please note the use of the function REAL() that converts an INTEGER to a REAL. Without this conversion, Sum /Number is computed as dividing an integer by an integer, yielding an integer result. Consult singe mode arithmetic expressions for details.INTEGER :: Count, Number, Sum, Input REAL :: Average Sum = 0 DO Count = 1, Number READ(*,*) Input Sum = Sum + Input END DO Average = REAL(Sum) / Number
In the above, the DO-loop iterates N times. The first iteration multiplies Factorial with 1, the second iteration multiplies Factorial with 2, the third time with 3, ..., the i-th time with I and so on. Therefore, the values that are multiplied with the initial value of Factorial are 1, 2, 3, ..., N. At the end of the DO, the value of Factorial is 1*2*3*...*(N-1)*N.INTEGER :: Factorial, N, I Factorial = 1 DO I = 1, N Factorial = factorial * I END DO
INTEGER :: count DO count = -3, 4, 0 ... END DO
INTEGER :: a, b, c DO a = b, c, 3 READ(*,*) a ! the value of a is changed a = b-c ! the value of a is changed END DO
INTEGER :: a, b, c, d, e DO a = b+c, c*d, (b+c)/e READ(*,*) b ! initial-value is changed d = 5 ! final-value is changed e = -3 ! step-size is changed END DO
INTEGER :: i DO i = 10, -10 ..... END DO
You should not use this form of DO-loop in your programs. See the discussion of general DO for the details.REAL :: x DO x = -1.0, 1.0, 0.25 ..... END DO