University of York

Department of Physics

Introductory FORTRAN Programming

To make a computer do a calculation, no matter how simple, you must first describe every step of the calculation in a way that the computer can understand. Such a collection of steps forms a program and is written in a language. There are many launguages which can be used, each having there own particular advantages for solving certain problems. You may have heard of some of them: eg BASIC, PASCAL, C, COBOL and BASIC. These notes are concerned with the FORTRAN language. It is a widely used language within the scientific community. Its origins lie in the early use of computers for numerical calculations (FORmula TRANslation), but has been updated over the years. These notes refer to FORTRAN 77.

There is a lot of similarity between the steps of a calculation written out in English and the steps or statements of the corresponding FORTRAN program. This not only makes it easy to write the program, but also reduces the chance of a mistake being made in writing the program. As with all languages there are many dialects, and FORTRAN is no exception. Thus some FORTRAN statements which are understood by some computers will not be understood by others. However, FORTRAN is one of the most standardised computer languages and so this is not usually a problem. These note closely follow standard FORTRAN 77, the version at the University of York does allow a number of extensions to the FORTRAN standard.

Some general terms in programming:

Constants numbers which do not change in a program. eg 36, -13.49, 21.3, 1.789E-4, (2.3,4.6)

	36 is an integer constant
	-13.49, 21.3 & 1.789E-4 are floating point or real constants
	(2.3,4.6) is a complex constant

The distinction between these constants arises from the fact that they are stored differently within the computer.

Note there is a difference between 0 (zero) and O (letter).

Variables are like "boxes" in which numbers are stored in a program allowing them to change when a program is run. eg A, X, COEFF; in general up to 6 letters or letters & digits can be used (must start with a letter).

Integer variables must begin with one of the letters I, J, K, L, M, N.

Otherwise they are assumed to be real, eg ICOEFF is an integer variable, whereas COEFF is real.

Expressions 	+  - 	add, subtract		|    Increasing priority (down this list)
		*  /	multiply, divide	|    in the order in which
		**  	exponential		|    the operations are
		() 	brackets		|    performed.

	  eg A+B   A*B    A**B (A to the power B)

If there is equal priority then the expression is evaluated from left to right.

Statement instruction in the program telling the computer what to do in this step. Only one statement per line is allowed in FORTRAN, though one statement can extend to many lines.

Statements are then executed in the order in which they are entered.

In FORTRAN, statements start in column 7 and go up to column 72. The first six columns have special meaning:

		column 1	C or * to begin a comment line (not FORTRAN statement).
		column 2-5	may be used for statement labels
		column 6	continuation symbol (not 0 or space) if the preceding statement is
				longer than column 72 it can be continued on the next line.
		column 73+	comments

More on statements:

Assign a value to a variable:
			X=2.5
			Y=X**2+5  (= x squared plus 5)
			COEFF=X**(2+5) (= x to the power 7)
READ*		Read data in from the terminal
		eg       READ*,I,X
		you would type into the terminal values for I and X (separated by commas)
		when you ran the program. Remember that I is an integer!

PRINT*		allows values and text to be printed on the terminal screen. 
		eg
		 C THIS PROGRAM PRINTS 'HELLO' AND Y VALUE
		       Y=3.4
		       PRINT*,'HELLO' 
		       PRINT*,'Y = ',Y
		       END

		numbers, variables, expressions, or a string of characters 
		enclosed in single quotes, are allowed in the PRINT statement.

END		tells the computer the program is finished.

GO TO Unconditional jump to another part of the program.
	eg 		GO TO 100
			.
			.
		    100 PRINT*,'THIS STATEMENT HAS LABEL 100'

	WARNING: Infinite loops are possible!		   10 A=B**2
							      GO TO 10

IF (logical expression) executable statement
	Conditional jump depending if the logical expression is TRUE or FALSE. 
	If TRUE then the program executes the statement.
	If FALSE the statement on the next line is executed. 
	eg     IF (I.NE.0) GO TO 100
	relational operators:
		.LT.		less than
		.GT.		greater than
		.EQ.		equal to
		.GE.		greater or equal
		.LE. 		less or equal
		.NE.		not equal

IF (logical expression) THEN
      statement
      statement
     .
     .
END IF		Block IF. If the logical expression is TRUE
		then the statements between the THEN and END IF are executed.
		If it is FALSE the program jumps to the statements immediately following the END IF.

Standard functions

		SIN(X)	the sine of X where X is in radians
		COS(X)	the cosine
		TAN(X)	the tangent
		ATAN(X)	the inverse tangent
		EXP(X)	e to the power X
		LOG(X)	natural logarithm
		ABS(X)	absolute value of X
		SQRT(X)	square root of X for X>=0
		INT(X)	integer value of X. INT(1.7)=1  INT(-1.3)=-2
		SIGN(X)	-1, 0 or +1 depending on sign of X

Program Example:

Newton's Square Root method:

     Xn+1 = 1/2(Xn + A/Xn)  Xn->sqrt(A) n->INFINITY

C NEWTON'S SQUARE ROOT
      PRINT*,'NEWTON'S SQR(A), INPUT A'
   10 READ*,A
      IF (A.GE.0.0) GO TO 15
      PRINT*,'NUMBER IS NEGATIVE, TRY AGAIN'
      GO TO 10
   15 PRINT*,'A = ',A,'SQRT(A) = ',SQRT(A)
      X=A
C INITIALISE ITERATION COUNTER
      I=0
C PRINT OUT COLUMN HEADINGS
      PRINT*,'ITERATION NO.        ESTIMATE OF SQRT'
C CALCULATE NEXT ESTIMATE OF SQR
   10 Y=(X+A/X)/2.0
C INCREMENT ITERATION COUNTER
      I=I+1
      PRINT*,I,Y
C CHECK IF CONVERGED ON SOLUTION
      IF (ABS(X-Y).LT.X*1.0E-5) GO TO 20
      X=Y
C CHECK IF MAX. NO. OF ITERATIONS EXCEEDED
      IF (I.LT.20) GO TO 10
      PRINT*,'MAX. NO. OF ITERATIONS EXCEEDED'
   20 STOP
      END