1 0 -100 32767 +15Then we have real constants:

1.0 -0.25 2.0E6 3.333E-1The E-notation means that you should multiply the constant by 10 raised to the power following the "E". Hence, 2.0E6 is two million, while 3.333E-1 is approximately one third.

For constants that are larger than the largest real allowed, or that requires high precision, double precision should be used. The notation is the same as for real constants except the "E" is replaced by a "D". Examples:

2.0D-1 1D99Here 2.0D-1 is a double precision one-fifth, while 1D99 is a one followed by 99 zeros.

The next type is complex constants. This is designated by a pair of constants (integer or real), separated by a comma and enclosed in parantheses. Examples are:

(2, -3) (1., 9.9E-1)The first number denotes the real part and the second the imaginary part.

The fifth type is logical constants. These can only have one of two values:

.TRUE. .FALSE.Note that the dots enclosing the letters are required.

The last type is character constants. These are most often used as
an *array* of characters, called a *string*. These
consist of an arbitrary sequence of characters enclosed in apostrophes
(single quotes):

'ABC' 'Anything goes!' 'It is a nice day'Strings and character constants are case sensitive. A problem arises if you want to have an apostrophe in the string itself. In this case, you should double the apostrophe:

'It''s a nice day'

and an example isoperand operator operand

x + yThe result of an expression is itself an operand, hence we can nest expressions together like

x + 2 * yThis raises the question of precedence: Does the last expression mean

**All these operators are calculated left-to-right, except the exponentiation operator **, which has right-to-left precedence. If you want to change the default evaluation order, you can use parentheses.{exponentiation}*,/{multiplication, division}+,-{addition, subtraction}

The above operators are all binary operators.
there is also the unary operator `-`
for negation, which takes precedence over the others. Hence
an expression like `-x+y` means what you would expect.

Extreme caution must be taken when using the division operator, which has a quite different meaning for integers and reals. If the operands are both integers, an integer division is performed, otherwise a real arithmetic division is performed. For example, 3/2 equals 1, while 3./2. equals 1.5.

The interpretation is as follows: Evaluate the right hand side and assign the resulting value to the variable on the left. The expression on the right may contain other variables, but these never change value! For example,variable_name=expression

area = pi * r**2does not change the value of

real x x = x + 1will convert the integer one to the real number one, and has the desired effect of incrementing x by one. However, in more complicated expressions, it is good programming practice to force the necessary type conversions explicitly. For numbers, the following functions are available:

int real dble ichar charThe first three have the obvious meaning.

Example: How to multiply two real variables `x` and `y`
using double precision and store the result in the double
precision variable `w`:

w = dble(x)*dble(y)Note that this is different from

w = dble(x*y)

- Exercise A
- Calculate the value of the following Fortran 77 expressions (by hand):
2+1-10/3/4 2**3/3*2-5 -(3*4-2)**(3-2**1+1)/-2

- Exercise B
- Write a Fortran 77 program that declares
*x*and*y*as real variables and assigns them the values one third and two thirds, respectively. Let*w*be double precision and compare the values of*w*after the assignments*w = x*y*and*w = dble(x)*dble(y)*. Use the command`write(*,*)`to print your answers. Compare the two answers, and explain briefly why you did not (or did) get the answer 0.22222222...

[AMS Home Page] [Fortran Tutorial Home]

boman@sccm.stanford.edu