[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ next ]


Fortran 90 Lessons for Computational Chemistry
Chapter 5 - Control Structures


5.1 Objectives

The main aims of this session consist of:

  1. presenting the different conditional control structures in Fortran (branching).

  1. presenting the different way of building loops in Fortran code.

These structures allows the programmer to control the program flow, allowing the conditional execution of statements according to the user input values or the values acquired by variables during the program execution.

It is extremely important to take into account before starting to write code in any programming language that a previous step should be accomplished. In encompasses having a clear idea of the problem, the inputs and outputs, the program structure, breaking complex tasks into simpler subtasks, and the optimal algorithm. A flow diagram can be of great help at this stage.

The division of the problem into simpler and simpler tasks is called top-down design. Each subtasks should be coded and checked in an independent manner.


5.2 Main items.

We provide a scheme of the main control structures, strting with conditionals and later of loops.


5.3 Example codes.


5.3.1 excode_5_1.f90

     PROGRAM ex_5_1
       !
       IMPLICIT NONE
       !
       REAL :: Grade
       CHARACTER(LEN = 2), DIMENSION(1:5) :: List_Grades=(/'D ','C ','B ','A ','A+'/)
       INTEGER :: IN
       ! READ NOTE
       PRINT *, "Student mark??"
       READ *, Grade
       !
       IF (Grade>=0.0.AND.Grade<5.0) THEN 
          IN=1
       ELSE IF (Grade>=5.0.AND.Grade<7.0) THEN 
          IN=2
       ELSE IF (Grade>=7.0.AND.Grade<9.0) THEN 
          IN=3
       ELSE IF (Grade>=9.0.AND.Grade<10.0) THEN 
          IN=4
       ELSE IF (Grade==10.0) THEN 
          IN=5
       ELSE
          IN=0
       ENDIF
     !
       IF (IN==0) THEN
          PRINT *, "The input : ", Grade," has a wrong value. Only [0,10]"
       ELSE
          PRINT *,  "The student grade is ", List_Grades(IN)
       ENDIF
     !
     END PROGRAM EX_5_1

5.3.2 excode_5_2.f90

     PROGRAM ex_5_2
       !
       IMPLICIT NONE
       !
       REAL :: Grade
       INTEGER :: Index, Integer_Grade
       CHARACTER(LEN=2), DIMENSION(1:5) ::  List_Grades=(/'D ','C ','B ','A ','A+'/)
       ! READ Grade
       PRINT *, "Nota del estudiante?"
       READ *, Grade
       !
       Integer_Grade = NINT(Grade)
       !
       SELECT CASE (Integer_Grade)
       CASE (0:4) 
          Index = 1
       CASE (5,6) 
          Index = 2
       CASE (7,8) 
          Index = 3
       CASE (9) 
          Index = 4   
       CASE (10) 
          Index = 5
       CASE DEFAULT 
          Index = 0
       END SELECT
       !
       IF (Index==0) THEN
          PRINT *, "The input grade : ", Grade," is out of bounds. Only  [0,10]."
       ELSE
          PRINT 100, Grade, List_Grades(Index)
       ENDIF
       !
     100 FORMAT(1X,'Student grade is ',F4.1,' (',A3,')')
       !
     END PROGRAM EX_5_2

5.3.3 excode_5_3.f90

     PROGRAM ex_5_3
       !
       IMPLICIT NONE
       !
       REAL :: PIover2 = ASIN(1.0)
       REAL :: ANGLE1 = 0.0, ANGLE2 = 0.0
       INTEGER :: I
       !
       DO I = 0, 16, 2
          ANGLE1 = I*PIO2/4.0
          !
          WRITE(*,*)
          WRITE(*,*) 'Cos(',I/2,'PI/4) = ',COS(ANGLE1),'; Cos(',I/2,'PI/4) = ',COS(ANGLE2)
          WRITE(*,*) 'Sin(',I/2,'PI/4) = ',SIN(ANGLE1),'; Sin(',I/2,'PI/4) = ',SIN(ANGLE2)
          WRITE(*,*) 
          !
          ANGLE2 = ANGLE2 + PIO2/2.0
          !
       ENDDO
     END PROGRAM ex_5_3

5.3.4 Programa ejemplo_5_4.f90

     PROGRAM excode_5_4
       !
       IMPLICIT NONE
       !
       REAL :: X_val = 0.0 
       REAL :: X_app = 0.0, X_sum = 0.0
       INTEGER :: I_flag = 1, I_count = 0
       !
       ! Taylor Series:  SIN(X) = X - X^3/3! + X^5/5! - X^7/7! + ... 
       WRITE(*,*) "Introduce the angle X (RAD) :"
       READ(*,*) X_val
       !
       I_count = 1
       X_app = X_val
       X_sum = X_val
       !
       PRINT*, '         Order     Approx.    SIN(X)       Approx. - SIN(X)'
       !
       DO WHILE (I_flag == 1) 
          !
          PRINT*, I_count, X_app, SIN(X_val), X_app - SIN(X_val)
          !
          X_sum = X_sum*(-1)*X_val*X_val/((I_count*2+1)*(I_count*2))
          X_app = X_app + X_sum
          !
          I_count = I_count + 1
          !
          WRITE(*,*) "STOP? (0 yes, 1 no)"
          READ(*,*) I_flag
          IF (I_flag /= 1 .AND. I_flag /= 0) I_flag = 1
          !
       ENDDO
       !
     END PROGRAM excode_5_4

[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ next ]


Fortran 90 Lessons for Computational Chemistry

0.0

Curro Pérez-Bernal mailto:francisco.perez@dfaie.uhu.es