!
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
!
!         <<<<<---------------------------------------------->>>>>
!         <<<<<  3.  C O N T R O L   S T A T E M E N T S     >>>>>
!         <<<<<---------------------------------------------->>>>>
!
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

! GO TO statement: branching 
!
! It allows to JUMP to a given statement identified by a LABEL

  GO TO 10 

! Example 

     X = Y + 3.
     GO TO 204
 10  X = X + 2.
 204 Z = X + 4.

! The statement labelled 10 is jumped over, and can be executed only
! if there is a GO TO 10 somewhere in the code. The code then 
! is resumed SEQUENTIALLY from this statement


!***********************************************************************
!  CONDITIONAL EXPRESSIONS
!***********************************************************************

! The IF statement has the following form

! IF ( scalar-logical-expression ) action-statement 
!
! The expression "scalar-logical-expression" is tested. If it has
! the value .TRUE. the statement "action-statement" is executed.
! Otherwise control is passed to the following statement in the code.

! EXAMPLES

  IF ( FLAG ) GO TO 6
  IF ( X-Y > 0.) X = 0.
  IF ( COND .AND. P .LT. Q ) S(I) = T(J)


! The IF statement allows the execution of a SEQUENCE of statements
! (a BLOCK) to depend on a condition and the execution of 
! ALTERNATIVE sequences to depend on ALTERNTATIVE conditions

! THERE ARE THREE TYPES OF IF CONSTRUCT
!========================================
!
! TYPE 1
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

! This has the following form:
!
! [name:]  IF ( scalar-logical-expression ) THEN 
!               block
!          END IF [name] 
!
! The construct can be optionally given a name. Here this is made
! clear by square brackets (a convention that will be followed
! throughout).
!
! The sequence of statements "block" is executed if the 
! logical expression "scalar-logical-expression" evaluates to .TRUE.
! Otherwise the block is skipped.

! Example
!
  SWAP:   IF ( X < Y ) THEN 
              TEMP = X
              X = Y
              Y = TEMP
          END IF SWAP

! TYPE 2
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

! In the second form of the IF construct, an alternative block is 
! specified. This will be executed if the logical condition tested
! evaluates to .FALSE. 
!
! This has the following form:
!
! [name:]  IF ( scalar-logical-expression ) THEN 
!               block_1
!          ELSE 
!               block_2
!          END IF [name] 

! Example

   IF ( X .LT. Y ) THEN 
       X = -X
   ELSE
       Y = -Y
   END IF

! TYPE 3
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

! In the third and more general form of the IF construct, a series 
! of independent tests are performed, each with an associated 
! block (series of statements). The tests are made one after the other 
! until one is fullfilled. Control passes then on to the END IF
!
! This has the following form:
!
! [name:]  IF ( scalar-logical-expression ) THEN 
!               block_1
!          ELSE IF ( scalar-logical-expression ) THEN 
!               block_2
!          ....
!          ELSE IF ( scalar-logical-expression ) THEN 
!               block_n
!          ELSE 
!               block_final
!          END IF [name] 

! There can be:
!
!     ANY NUMBER (including zero) of ELSE IF statements
!     ZERO or ONE ELSE statements

! Example

   IF ( X .LT. Y ) THEN 
       X = -X
   ELSE IF ( X .EQ. 0 ) THEN 
       PRINT*, ' X IS ZERO'
       STOP
   ELSE 
       Y = -Y
   END IF


! It is permitted to NEST an arbitrary number of IF constructs
! within one another
!
! Example
!
   IF ( I < 0 ) THEN 
       IF ( J < 0 ) THEN 
           X = 0.
           Y = 0.
       ELSE
           Z = 0.
       END IF   
   ELSE IF ( K < 0 ) THEN 
       Z = 1.
   ELSE
       X = 1.
       Y = 1.
   END IF 


! Note that nested statements are usually INDENTED. This is noy 
! compulsory but it s STRONGLY recommended, as it improves the 
! readability of the code and follows naturally the logic of 
! the sequence of statements.


!***********************************************************************
!  THE DO CONSTRUCT
!***********************************************************************
            
! Many problems in mathematics and physics require, for their 
! representation in a programming language, the ability to ITERATE
!
! If we wish to sum all the elements of an array of length 10,
! we might do the following
!
  REAL, DIMENSION(10) :: A
  REAL SUM

  SUM = A(1)
  SUM = SUM + A(2)
  .....
  SUM = SUM + A(10)

! There is a facility in Fortran known as the DO construct that allows
! to iterate a statement. The same ten lines above can be reduced
! to the following bit of code

  SUM = 0.
  DO I=1, 10
     SUM = SUM + A(I)
  END DO

! I is an integher that takes the values 1 through 10 and CANNOT
! be modified within the LOOP.

! The DO statement has more general forms. If we wished to sum the 
! fourth to ninth elements we would write

  SUM = 0.
  DO I=4, 9
     SUM = SUM + A(I)
  END DO

! If we wished to sum all odd elements, we would write

  SUM = 0.
  DO I=1, 9, 2
     SUM = SUM + A(I)
  END DO

! The third integer specifies that we want the index to be incremented
! in STEPS of 2.

! The three integers specifying a DO construct need not be constants,
! but INTEGER EXPRESSIONS
!
! Example
!
  DO I=J+4, M, -K(J)**2

! The first value is J+4, subsequent elements are DECREMENTED by 
! K(J)**2 until the value of M is reached
!
! We see that a DO loop can also RUN BACKWARD as well as FORWARD
!
! The general form of a DO loop clause is then 
!
!
!  [name:]  DO variable = expr_1, expr_2 [, expr_3]
!               block
!           END DO [name]
!
! where expr_i are all SCALAR INTEGER EXPRESSIONS and name is the 
! optional name of the particular construct 
!
! The number of iterations in a DO loop is given by the expression
!
!
!  MAX( (expr_2 - expr_1 + expr_3)/expr_3, 0 )
! 
! where MAX(a,b) returns either the values of a or b, whichever 
! is the largest
!
! 
! Therefore, the loop

   DO I=1, N

! will not be executed at all if the value of N is zero or less!!


! The statement
!
!  DO [name]  
!
! will specify an ENDLESS loop
!
! A means to exit from an endless loop, or indeed from ANY loop 
! is thus required. This is accomplished through the statement
!
!  EXIT [name]
!
! Execution of an EXIT statement causes control to be transferred 
! to the next executable statement after the END DO statement to 
! which it refers.
! If no name is specified it is terminates execution of the 
! INNERMOST DO loop in which it is enclosed.

! The value of the LOOP index is incremented at the end of each 
! iteration for use in the subsequent iteration. This index
! is available outside the loop
!
! Example

  DO I=1, N
      ....
      IF ( I .EQ. J ) EXIT      
      ...
  END DO
  L = I

! Three cases are possible 
!
! 1. If at execution time N <= 1 then I is set to 1 but 
!    the loop is not executed and control passes to the 
!    statement L = I. In this case L will be 1
! 2. If at execution time 1 < N < J, then the loop will be 
!    executed N times. When reaching the end of the loop 
!    for the N-th time, I will be incremented by one and 
!    thus in this case L will be N+1
! 3. If at execution time N >= J, the loop will be executed 
!    until I=J. At this point, due to the EXIT statement, control
!    is transferred to the assignement L = I. Obviously, in thsi 
!    case L will have the value J 
!
! ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! ONE HAS TO BE VERY CAREFUL TO USE LOOP INDEXES OUTSIDE THE LOOP
! ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


! DO loops can be nested  provided the range of one loop is 
! completely contained in the range of another.

! Example, matrix multiplication A = B x C
!

  DO I=1, N
     DO J=1, M
        A(I,J) = 0.0
        DO K=1, L
            A(I,J) = A(I,J) + B(I,K) * C(K,J)
        END DO
     END DO
  END DO

! Another construct makes use of a LABEL to identify the loop
!
! Example:

    DO 30 I=1, N
       A(I) = EXP ( - I*2 )
 30 END DO

! or equivalently 

    DO 30 I=1, N
       A(I) = EXP ( - I*2 )
 30 CONTINUE

! if a label is chosen, each loop must have a separate label


!***********************************************************************
! ILLEGAL BRANCHING 
!
! It is illegal for the range of a DO construct (or an IF) to be 
! only PARTIALLY contained in a block of another construct:
!

  DO I=1, N                      ! This is a legal construct
      IF ( ... ) THEN 
            ....
      END IF
      ...
  END DO

  IF ( ... ) THEN                 ! This is a legal construct
      DO I=1, N
          ....
      END DO
      ...
  END IF

  DO I=1, N                       ! This is NOT a legal construct !!
      IF ( ... ) THEN 
            ....
      END DO
      ...
  END IF

  IF ( ... ) THEN                 ! This is NOT a legal construct !!
      DO I=1, N
          ....
      END IF
      ...
  END DO


  





    
