next up previous contents
Next: PRINT, READ, STOP and Up: Introduction to FORTRAN Previous: Comment Lines, Blank Lines,

Constants, Variables, Expressions and Assignments

FORTRAN has two types of numeric data, INTEGERS (or fixed point) e.g. -27, 0, 1234 and REALS (or floating point) e.g. 3.14159, 0.0, -.007, 7.E-5, -123.45E 9 (the E means raised to the power 10 so 100 is 1.E2 and .001 is 1.E-2). Integers may be at least 9 digits long and reals have an accuracy of about 7 digits and an exponent in the range -37 to +37. The limits are determined by the number of bits used to hold the numbers and the conventions used to convert the numbers into bit patterns. Consequently the limits will vary from one computer type to another. Also the user can request smaller integers (for greater memory capacity) or longer reals (for greater accuracy). VARIABLES have names starting with an alphabetic character optionally followed by up to 5 (or on the VMS 31) alphanumeric characters. So some valid names are at A, B123, FRED and ZZ9. Variables may hold integers or reals and it is up to the user to tell the compiler which hold which. For example:-

    REAL  Z
    INTEGER  ISUM
The user need not declare which are integers and which are reals if he or she stays with the implicit rule that all variables starting I,J,K,L,M or N are integers and the rest are reals. For compatibility all users are VERY STRONGLY URGED to obey this rule! EXPRESSIONS are made by combining constants and variables using:-
    +-     addition, subtraction
    */      multiplication, division
    **     exponentiation
    ()     brackets

The above list is shown in ascending priority so that expressions follow normal algebriac rules. Within any one priority the expression is evaluated left to right. So

    3+2*4 is 11 (not 20)
    8/2*2 is 8  (not 2)
    3**2*4 is 36 (not 6561)
When dividing integers the result is always rounded towards 0 so 3/2=1 and -3/2=-1. Expression may mix integers and reals in which case the integer number is always first converted to a real before being used. So if I contains the integer 3 and A the real 2.0 then I/A would produce the real 2.5. (Note the value in I is not changed). ASSIGNMENTS give new values to variables. They have the general form
variable=expression
For example
    N=1
    A=-27.5
    X=Y+Z/A
    J=J+1
    L=Z+I

Note that the expression can involve the variable whose value is being changed. So if J has the value 2 then after J=J+1 it will have the value 3. If a real expression is assigned to an integer then it is rounded towards zero and if an integer expression is assigned to a real then it is converted by adding a zero fractional part.

Exercise 2
Type in and run the following program
        A=2.5
        B=5
        C=B/A
        PRINT *,C
        STOP
        END

If you have typed it in properly the answer should be 2.0. Now change the variable A to IA and rerun the program. The first line has a real expression assigned to an integer, so it will be rounded towards zero and IA will contain 2. On the third line B/IA is a mixed expression so the value in IA will be converted to 2.0 before use and the result should be 2.5. Finally change B to IB and change the second line to IB=-5. Run the program again. This time IB/IA is an integer expression which produces an integer result, again rounded towards zero. It is then stored in C as -2.0

* * *


next up previous contents
Next: PRINT, READ, STOP and Up: Introduction to FORTRAN Previous: Comment Lines, Blank Lines,
n west (APC)
2000-03-08