6. Expressions and assignment

Constants

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:

      1
      0
      -100
      32767
      +15

Then we have real constants:

      1.0
      -0.25
      2.0E6
      3.333E-1

The 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.

Double precision constants are used for constants that are larger than the largest real allowed, or that require high precision. The notation is the same as for real constants except the "E" is replaced by a "D". Here are some examples:

      2.0D-1
      1D99

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

Complex constants are 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.

Logical constants can only have one of two values:

      .TRUE.
      .FALSE.

Note that the dots enclosing the letters are required.

Character constants 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'

Expressions

The simplest non-constant 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. When in doubt, you may use parentheses to insure the correct order of operation.

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 (note the decimal points).

Assignment

A variable 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 the variable assignment does not change their 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

This 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:

      int
      real
      dble
      ichar
      char

The first three have the obvious meaning, while ichar takes a character and converts it to an integer, and char does exactly the opposite.

For example, the following multiplies two real variables x and y using double precision, then stores the result in the double precision variable w:

      w = dble(x)*dble(y)

Note that this is different from:

      w = dble(x*y)

Copyright © 1995-7 by Stanford University. All rights reserved.

[ Index ]