FORTRAN Expressions and assignments

Home Computational Physics FORTRAN FORTRAN Expressions and assignments


The simplest form of an expression is a constant. There are 6 types of constants, corresponding to the 6 data types. Here are some integer constants:
Then we have real constants:
The E-notation means that you should multiply the constant by 10 raised to the power following the "E".
Hence, 5.0E3 is two million, while 7.283E-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".
Here 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 parentheses. 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:
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):
'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'


The simplest expressions are of the form
operand operator operand
and an example is
x + y
The result of an expression is itself an operand, hence we can nest expressions together like
x + 2 * y
This raises the question of precedence: Does the last expression mean x + (2*y) or (x+2)*y? The
precedence of arithmetic operators in Fortran 77 are (from highest to lowest):
** {exponentiation}
*,/ {multiplication, division}
+,- {addition, subtraction}
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.

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 assignment has the form
variable_name = expression
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,
area = pi * r**2
does not change the value of pi or r, only area.

Type conversion

When different data types occur in the same expression, type conversion has to take place, either explicitly
or implicitly. Fortran will do some type conversion implicitly. For example,
real x
x = x + 1
will 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:
The first three have the obvious meaning. ichar takes a character and converts it to an integer, while char does exactly the opposite.
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)