From edo@wharfrat.as.arizona.edu Mon Aug 27 10:00:36 2001
Received: from wharfrat.as.arizona.edu (wharfrat.as.arizona.edu [128.196.209.89])
	by lithops.as.arizona.edu (8.9.1/8.9.1) with ESMTP id KAA02675
	for <jill@lithops.as.arizona.edu>; Mon, 27 Aug 2001 10:00:36 -0700 (MST)
Received: (from edo@localhost)
	by wharfrat.as.arizona.edu (8.9.1/8.9.1) id KAA27560
	for jill; Mon, 27 Aug 2001 10:00:36 -0700 (MST)
Date: Mon, 27 Aug 2001 10:00:36 -0700 (MST)
From: "E. Olszewski" <edo@as.arizona.edu>
Message-Id: <200108271700.KAA27560@wharfrat.as.arizona.edu>
To: jill@as.arizona.edu
Subject: fortran
Content-Length: 5756
X-Lines: 198
Status: RO

Phys 205, Aug 31 1999


Syntax of FORTRAN programs
A cheat sheet for neophytes
--------------------------

  There are three basic parts for a FORTRAN program,
the program statement and introductory comments,
declaration of variables and the code itself.

********** 1) The program statement and introductory comments **********
--------------------------------------------------

***  A) Comment statements ***

c
c Col 1 comment (c, C or !)
! you can put a ! after a line of code and the rest of the line will be
! treated as a comment statement, ie  "a=a+1 !increment a"
c Cols 2 through 5 are reserved for statement numbers
c Cols 7 through 72 are used for FORTRAN statements
c Col 6 is for continution marks.  If your FORTRAN statement
c   is longer than 72-7=55 spaces long then any character in column
c   6 means this line is continuation of previous line.

***  B) Program statement ***

c++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
c
      program addup
c
c++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
c


********** 2) Declarations **********
---------------

c Always start with "implicit none".  That forces you to declare every
c variable you use so compiler will help find spelling and other errors.
c
c Here are examples of declarations
c
      implicit none
c
      integer i,j,k
      real*4 coeff1, coeff2, coeff3 !"real*4" is the same as "real" but you're
                                    ! forced to know that the variable takes 
                                    !up 4 bytes. this may seem pedantic, but 
                                    !it's useful in debugging
      real*4 dist(100)            !dimensioned variables, dist(1), dist(2),
                                  !dist(3)...dist(100)
      real*8 x,y !some of you have used "double precision" instead of real*8
                 !real*8 forces you to remember that a double precision number
                 !takes up 8 bytes of memory, which is an aid for debugging
      character*80 filename
      logical first_time








********** 3) FORTRAN code **********
 
***  A) Examples of do loop syntax ***

c Loops through to enddo, back to do, till i > 10
      do i=1,10
        lots of equations
      enddo

c Loops through to enddo, back to do, increments i by 2 till i > 10
c In example above if no step size is given then defaults step size
c of 1 is used.
      do i=1,10,2
        lots of equations
      enddo

c Example of nested do loop
c   sets i=1; does j=1,10
c   then sets i=2; then does j=1,10
c   and on and on
      do i=1,10
        do j=1,10
         fortran stuff
        enddo
      enddo

***  B) Examples of "if" syntax  ***

c The relational operators are .gt. .lt. .ge. .le. .eq. .ne.

      if (i .lt. 17)a=5

      if ((a-b) .gt. 10.)then
        bunch of fortran done when (a-b) > 10.
      elseif ((a-b) .gt. 5.)
        bunch of fortran done when 10. >= (a-b) > 5.
      else
        some other fortran done when (a-b) <= 5.
      endif

      if ((sin(d)-5.) .ne. 0.15)then
        whatever fortran you'd like
      endif

***  C) Examples of arithmetic operations ***

      a=b
      i=j+1
      b=b+1.        !do math on right side of = and put it into variable on left
      dist(34)=a*3.
      i=i/j         !NB: "integer arithmetic" is different from "real arithmetic"
      d=cos(a)      !a is angle measured in radians
      a=mod(i,4)    !the "remainder function". 
                    !if i=1,a=1; if i=2,a=2; if i=3,a=3;
                    !   if i=4,a=0; if i=5,a=1; etc
      a=min(b,c)    !a will = smaller value of b and c
      a=max(b,c)    !a will = larger value of b and c

c there are many other INTRINSIC FUNCTIONS. you can find them in FORTRAN BOOKS
c and then need to know the syntax for your particular compiler
      call srand(some integer)
      a=rand(0) !getting random numbers











***  D) Formatted input and output ***

In general:
      read(unit,format)variables
      write(unit,format)variables

unit: 1 through 4 are system defined (do not use)
      5 is the keyboard
      6 is the screen
      7 through 999 can be defined by user.  Default is file called fort.7, fort.8, etc
          unless open statement is used link unit to a file.

The basic formats are "i" for integer
                      "f" for floating point ("real")
                      "e" for exponential
                      "a" for character string
                      "x" for skipping spaces
                      "/" for new line

      read(5,*)a       !read variable a from keyboard in free format
      write(6,*)a      !write variable a to screen in free format

      write(6,*)'The average values are ',ave_1,' and ',ave_2

      write(6,100)a            !write a according to format give in line 100
 100  format(' a = ',f10.5)    !write string inside ' ' and then a in float format

      city_name='Tucson'
      write(13,"(' the city is ',a20)city_name

      open(10,file='list.dat')
      write(10,200)i,j,a,b
 200  format(' i,j =',2i5,/,' a,b =',2e15.5)
      close(10)

c
c Example of reading line containing several values
c

      integer rah,ram,decd,decm
      real ras,decs,mag,color
      character*6 number
      character*25 proper_name
      character*1 ds
      read(23,300)number,proper_name,rah,ram,ras,ds,decd,decm,decs,mag,color
 300  format(a6,2x,a20,1x,i2,1x,i2,1x,f5.2,1x,a1,i2,1x,i2,1x,f4.1,1x,f5.2,
     $        1x,f5.2)

            !This will read one line from unit 23 (file) which may look like:

M120_5  RU Canis Venaticorum 02:12:12.12 -56:56:56.0  9.68 -1.23

            !NB: $ in column 6 in format means line is continuation of previous line





***c as we teach you new FORTRAN we will add new pages to this cheat sheet ***

