Introduction to FORTRAN    
     Present by
Chian-Yi Liu      
     02/25/2002








1. FORTRAN programming format

2. Basic Input/Output and Declaration  
 2.1 Output
 2.2 Declaration
     2.2.1 Integer, Real, Character
     2.2.2 COMPLEX
     2.2.3 CHARACTER
     2.2.4 PARAMETER
 2.3 Input (from keyboard)
 2.4 FORMATTED I/O  
     2.4.1 file formatted I/O 

3. Flow Control  
 3.1  Block-If Statement
 3.2  Logical Expressions

4.  Do-Loop 
 4.1 Conting Loops
 4.2 Nested Loops

5.  Compile and Run    
 





















Note:  The examples I show you all are based on FORTRAN 77.

   
   
     
     
1.  FORTRAN programming format
   
   column          description
  ------------  ------------------------------------------------------ 
   1~5		 Empty or numbers only
   1		 Comment if the first column is "c" or "C",    
                 FORTRAN compiler will ignore this line
   6		 Empty or other characters except 0.
   		 If exits any characters except 0, it means
		 continus line after last line.
   7~72		 FORTRAN program coding area
   after 72	 Remark, ignored by FORTRAN compiler
   
   PS. FORTRAN 90 doesn't have those rules shown above, but:
        -) any characters after "!" will ignore as remark or comment.
        -) if the end of each line or
              the beginning of each line is "&",
	   it means they are consecutive lines.
	   
   e.g.
   +================================================================
   |123456789111111111122222222223333333333444444444455555::7777:::
   |         012345678901234567890123456789012345678901234::1234:::
   +================================================================
   |C this program is written by Chian-Yi Liu at 02/25/2002
   |C it is used for demostration only.
   |
   |      PROGRAM EX101 
   |      REAL  A(10), B(10), C(7)
   |      
   |      DO i = 1, 7
   |        C(i) = A(i) + A(i+1) + A(i+2) +
   |     &         B(i) + B(i+1) + B(i+2)
   |      ENDDO
   |
   |      STOP
   |      END
   |
   |
   
   
2.  Basic Input/Output and Declaration     
 
2.1 Output     
 e.g.  
       
        
      PROGRAM EX201                            <--- you can name it as you kike

      WRITE(*,*) 'Hello, World!'               <--- main program

      STOP                                     <--- this line can be omit
      END                                      <--- the end of main program
      
      
      
   -)  WRITE(*,*)  , the first star (*) means the output will show on deault output
                     device (monitor)
		     the second star means "free format".
 2.2 Declaration
  2.2.1 Integer, Real, Character
  e.g.

 
       PROGRAM  EX202

       INTEGER   A
       REAL      B
       CHARACTER C

       A=1
       B=1.0
       C='C'

       WRITE(*,*) ' A=',A,' B=',B,' C=',C

       STOP
       END	       
                         
  -) "i,j,k,l,m,n, I,J,K,L,M,N" or any string starting with are default INTEGER 
     without any other declaration.


      FORTRAN 77              FORTRAN 90
     -------------------    -------------------    -----------------
      REAL*4  A              Real(kind=4)::a       single precision
      REAL*8  B              Real(kind=8)::b       double precision
      DOUBLE PRECISION B 
      
 
  e.g.   (Can you see the difference?)
  
       PROGRAM  EX203

       INTEGER   A
       REAL      B

       A = 2.5 + 3.0 / 2.0
       B = 2.5 + 3.0 / 2.0

       WRITE(*,*) ' A=',A,' B=',B

       STOP
       END	        
 
 2.2.2 COMPLEX
     FORTRAN is the only computer language supports complex type.

       FORTRAN 77              FORTRAN 90
     -------------------    -------------------    -----------------
      COMPLEX A              Complex::a             single precision
      COMPLEX*8  A           Complex(kind=4)::a     single precision
      COMPLEX*16 B           Complex(kind=8)::b     double precision
 

  e.g.
      PROGRAM  EX204

      COMPLEX  A,B

      A=(1.0,1.0)
      B=(1.0,2.0)

      WRITE(*,*) 'A=',A,' B=',B
      WRITE(*,*) 'A+B=',A+B
      WRITE(*,*) 'A-B=',A-B
      WRITE(*,*) 'A*B=',A*B
      WRITE(*,*) 'A/B=',A/B

      STOP
      END
    
 2.2.3 CHARACTER
 
       FORTRAN 77              FORTRAN 90
     -------------------    ---------------------    
      CHARACTER*1	     character(len=1)::a
      CHARACTER A*1	     character(1)::a
                             character*1::a
			     
      CHARACTER*80 B	     character(len=80)::b
      CHARACTER B*80         character(80)::b
                             character*80::b
			     
  e.g. (how to assign characters?)
      PROGRAM  EX205

      CHARACTER*80  STRING

      STRING='Hello, World!'
      WRITE(*,*) STRING

      STOP
      END

  e.g.  (the concatenation of strings)
      PROGRAM  EX206

      CHARACTER*10 FIRST_NAME
      CHARACTER*10 LAST_NAME
      CHARACTER*20 NAME

      FIRST_NAME = 'Scott'
      LAST_NAME  = ' Weaver'
      NAME       = FIRST_NAME//LAST_NAME

      WRITE(*,*) 'My name is ',NAME

      STOP
      END

 2.2.4 PARAMETER
 e.g.
      PROGRAM  EX207

      REAL       PI
      PARAMETER (PI=3.14159)

      WRITE(*, '(1X,A10,F5.2)' ) 'SIN(PI/6)=',SIN(PI/6.0)

      STOP
      END
   
   -) you can benefit from introducing "PARAMETER" in the 
      large, long-run program.


2.3 Input (from keyboard)
e.g.



      PROGRAM  EX208

      INTEGER  A
      REAL B
      CHARACTER*80 C

      WRITE(*,*) 'Please input a (integer) number:'
      READ(*,*) A
  
      WRITE(*,*) 'Please input a (real) number:'
      READ(*,*) B
      
      WRITE(*,*) 'Please input a string (up to 80 characters):'
      READ(*,*) C
      
      WRITE(*,*) A,B,C

      STOP
      END

2.4 FORMATTED I/O  
e.g.
      PROGRAM  EX209

      INTEGER      A
      REAL         B
      COMPLEX      C
      CHARACTER*20 D

      A=10
      B=123.45
      C=(1,2)
      D='Hello, World!'
 
      WRITE(*, 100 )  A
      WRITE(*, 200 )  B,B
      WRITE(*, 300 )  C
      WRITE(*, 400 )  D

100   FORMAT(1X, I5)
200   FORMAT(1X, F9.3, 2X, E15.7)
300   FORMAT(1X, F5.2, F5.2)
400   FORMAT(1X, A20)

      STOP
      END

  -) if the output is not as you define yet, it shows stars (*).
 
 2.4.1 file formatted I/O 
  There are two types of file formate, ASCII and binary.
  You can read and edit ASCII format files directly, (e.g. import into MS-Excel)
  but can't do these on binary files.  Binary files can save memory space (HD, tape, etc.) 
  and quick access.
e.g.  (ASCII)    
      PROGRAM  EX210
      
      OPEN(UNIT=10, FILE='my_first.txt')
      WRITE(10,*) 'Hello, World!'
            
      CLOSE(10)
      STOP
      END

  -) common uses of "OPEN" statement:
  
      OPEN(UNIT=number,  FILE='filename', FORM='....',STATUS='...', 
    &      ACCESS='...',    RECL=length,  ERR=lable)
   
    UNIT=number
       number muse be an "integer", and also could be a variable.
       avoid to use 1,2,5,6.  2,6 are default output device, i.e., monitor.
       1,5 are default input device, i.e., keyboard.
    FORM='FORMATTED' or 'UNFORMATTED'
       only two choices.
       'FORMATTED' means use ASCII format.
       'UNFORMATTED' means use binary format.
    STATUS='NEW' or 'OLD' or 'UNKNOWN'(default)
       'NEW' means open a new file.  If it exits, you will get error message 
             when run the program.
       'OLD' means an existent file. You can re-write the file.   
     ACCESS='SEQUENTIAL' or 'DIRECT'
     RECL=length
        valid only when you choose ACCESS='DIRECT'.
	it means the file is cut into small segments by the length you are given.  
     ERR=lable
         if any error occurs, jump into the labe you ask you want to continue
	 the program.

e.g.
    Please refer to {meto617_hw1_1.f} on attachement.
  

3.  Flow Control      
 3.1  Block-If Statement
e.g.

   If ( condiction ) THEN
      statement_1_1
      statement_1_2
          :
	  :
   ELSE
      statement_2_1
      statement_2_2
          :
	  :      
   ENDIF
   
 3.2  Logical Expressions
   -) Relational Operators

     Operator    Description
     --------  ------------------ 
      .EQ.      =  (EQual)
      .NE.      != (Not Equal)
      .GT.      >  (Great Then)
      .GE.      >= (Great or Equal)
      .LT.      <  (Less Then)
      .LE.      <= (Less or Equal)

   -)  Logical Operators

     Operator    Description
     --------  ------------------    
      .NOT.      Negation
      .AND.      Conjunction
      .OR.       Inclusive Disjunction
      .EQV.      Equivalence
      .NEQV.     Non-Equivalence
      
   -) DO NOT forget the dot (.) before and after the operator!
          
e.g.  
    +-----------|      
    | #1        |
    |       +---+-------+
    |       |   |     #2|
    |       | A |       |
    |       |   |       |
    |       +---+-------+
    |           |       			     
    +-----------+    			     

   If ( condiction_1 .GT. 0  .AND. condiction_2 .LT. 0) THEN
      statement
      statement
         :
	 :	
   ENDIF			
		     
4.  Do-Loop
  A loop is a programming structure in which a series of statements 
  is repeated.
  
4.1 Conting Loops
e.g.
      PROGRAM  EX401

      DO I = 10, 100, 5
        J= I * 2
	PRINT *, I, J
      ENDDO

      STOP
      END     			     
			     
e.g. 
      PROGRAM  EX402
      REAL I,J

      DO 50 I = 100, 10, -1.5
        J= I * 2.
	PRINT *, I, J
 50   CONTINUE

      STOP
      END     			     
			
4.2 Nested Loops
e.g.     

      PROGRAM  Nested_Loops

      DO I= 1, 360
           [statement #1_1]
	   [statement #1_2]
        
	DO   J= 1, 180
	   [statement #2] 
        ENDDO
	
           [statement #3]
		
      ENDDO	

      STOP
      END     			        
Questions:
  How many times does [statement #1_1] excute?       Ans: 1~360, 360 times
  How many times does [statement #1_2] excute?       Ans: 1~360, 360 times
  How many times does [statement #2]   excute?       Ans: 360 * 180 = 64800 times!
  How many times does [statement #3]   excute?       Ans: _______
     
   	
5.  Compile and Run   
  -) the default execution filename is "a.out"
  
   % f77 .f
   % ls 
   .f     a.out
   
  -)  % ./a.out