In parallel with the DO-EXIT construct, Fortran has a DO-CYCLE construct as follows:
where control-info is empty if the loop is a DO-END DO; otherwise, control-info contains all information that a counting DO should have.When the execution of a DO-loop encounters the CYCLE statement, the DO-loop starts next iteration immediately.DO control-info statements-1 CYCLE statements-2 END DO
This is not a recommended feature. So, if it is possible, do not use it.
Examples
- The following loop only displays 1, 2, 4 and 5. If the value of i is 1, 2, 4 or 5, the execution of the loop enters the ELSE part and displays the value of i. However, if i is 3, since i == 3 is .TRUE., theCYCLE statement is executed, which brings back to the beginning of the DO-loop starting the next iteration (i.e., the iteration corresponds to i=4).
INTEGER :: i DO i = 1, 5 IF (i == 3) THEN CYCLE ELSE WRITE(*,*) i END IF END DO - The following code has a DO-loop for processing the input value stored in Range. At the beginning of the loop, the value of Range is read in and checked. If the value is less than 2, the CYCLEstatement brings the control back to the beginning of the loop to read a new value for Range. This will continue until a value that is greater than or equal to 2. Then, the logical expression of the IF-THEN-END IF is .FALSE. and consequently the execution continues with "... process Range ...".
Please compare this example with the technique used in the second prime number example in which EXIT is used rather than CYCLE.INTEGER :: Range DO WRITE(*,*) 'An integer >= 2 please --> ' READ(*,*) Range IF (Range < 2) THEN WRITE(*,*) 'Input not in the required range' CYCLE END IF ... process Range ... END DO
A Programming Example
This problem solves a puzzle: RED x FOR = DANGER, where each letter represents a digit and different letters means different digits. Moreover, R, F and D cannot be zero.Write a program to find all solutions.Solution
! ----------------------------------------------------------
! This program solve the following puzzle:
! RED
! x FOR
! -------
! DANGER
! where each distinct letter represents a different digit.
! Moreover, R, F and D cannot be zero.
! ----------------------------------------------------------
PROGRAM Puzzle
IMPLICIT NONE
INTEGER :: R, E, D, F, O, A, N, G ! the digits
INTEGER :: RED, FOR, DANGER ! the constructed values
INTEGER :: Count ! solutions count
WRITE(*,*) 'This program solves the following puzzle:'
WRITE(*,*)
WRITE(*,*) ' RED'
WRITE(*,*) 'x FOR'
WRITE(*,*) '-------'
WRITE(*,*) ' DANGER'
WRITE(*,*)
Count = 0
DO R = 1, 9
DO E = 0, 9
IF (E == R) CYCLE
DO D = 1, 9
IF (D == R .OR. D == E) CYCLE
DO F = 1, 9
IF (F == R .OR. F == E .OR. F == D) CYCLE
DO O = 0, 9
IF (O == R .OR. O == E .OR. O == D .OR. &
O == F) CYCLE
DO A = 0, 9
IF (A == R .OR. A == E .OR. A == D .OR. &
A == F .OR. A == O) CYCLE
DO N = 0, 9
IF (N == R .OR. N == E .OR. N == D .OR. &
N == F .OR. N == O .OR. N == A) CYCLE
DO G = 0, 9
IF (G == R .OR. G == E .OR. G == D .OR. &
G == F .OR. G == O .OR. G == A .OR. &
G == N) CYCLE
RED = R*100 + E*10 + D
FOR = F*100 + O*10 + R
DANGER = D*100000 + A*10000 + N*1000 + G*100 + E*10 + R
IF (RED * FOR == DANGER) THEN
Count = Count + 1
WRITE(*,*) 'Solution ', Count, ':'
WRITE(*,*) ' RED = ', RED
WRITE(*,*) ' FOR = ', FOR
WRITE(*,*) ' DANGER = ', DANGER
WRITE(*,*)
END IF
END DO
END DO
END DO
END DO
END DO
END DO
END DO
END DO
END PROGRAM Puzzle
Click here to download this program. Program Output
The following is the output generated by the above program. There are two solutions:
This program solves the following puzzle:
RED
x FOR
-------
DANGER
Solution 1:
RED = 321
FOR = 563
DANGER = 180723
Solution 2:
RED = 481
FOR = 364
DANGER = 175084
Discussion
- This program uses a brute-force method. That is, it searches all possibilities.
- Since there are eight digits, R, E, D, F, O, A, N and G, each of which runs from 0 to 9 except for R, F and D which runs from 1 to 9, we need eight nested DO-loops.
- Since different letters represent different digits, at the very beginning of a DO-loop, we must make sure the value of its control variable is different from the values of all previous loops.
The above only shows three loops for R, E and D. At the beginning of the E loop, the value of E is checked to see if it is equal to the value of R. If they are equal, the CYCLE brings the control to the next iteration. Similarly, at the beginning of the D loop, the value of D is compared against the values of E and R. If they are equal, CYCLE causes the start of the next iteration. Note that D runs from 1 to 9.DO R = 1, 9 DO E = 0, 9 IF (E == R) CYCLE DO D = 1, 9 IF (D == R .OR. D == E) CYCLE ... other loops ... END DO END DO END DO - In the inner-most loop, the value of RED, FOR and DANGER are computed and compared. If RED*FOR is equal to DANGER, a solution is found and its values are displayed.
RED = R*100 + E*10 + D FOR = F*100 + O*10 + R DANGER = D*100000 + A*10000 + N*1000 + G*100 + E*10 + R IF (RED * FOR == DANGER) THEN ... display READ, FOR and DANGER ... END IF
- The concept of this program, except for the use of CYCLE, is similar to that of finding all three-digit Armstrong Numbers. Please compare these two programs.
No comments:
Post a Comment
=(*_*)------------------------(^_^)=
:::::|berkomentar dengan sopan adalah akhlak kemulian|:::::