Assignment Statements

An assignment statement gives a new value to a variable. The syntax is very simple:

          V = expression

where V is a variable.

The expression is evaluated and the resulting value becomes the new value of the variable, replacing whatever its previous value may have been.

A program often contains more assignment statements than any other kind of statement.


CHARACTER Assignment Statements

In assignment statements involving CHARACTER variables and values, characters are moved one at a time, left to right, from the expression's value to the variable.

If the variable's length is smaller, then the remaining characters in the expression's value are not copied at all.

If the variable's length is greater, then the remaining positions in the variable are filled with blanks.

That is to say, we have truncation on the right in one case and padding on the right with blanks in the other case.

By using substring notation, we could change a few characters in the middle of a string. For instance:

          CHARACTER(7) Nut = 'Filbert'
          Nut(3:5) = 'G?5'

and now Nut contains the value 'FiG?5rt'.


Assignment Statments involving Numbers

There is no problem if the variable and the expression are of the same type.

If we assign an INTEGER value to a REAL, the INTEGER value is converted to REAL, retaining as much precision as possible.

If we assign a REAL value to an INTEGER, the value is essentially converted to integer + fraction and the fraction is discarded. This can be stated as "rounding down to the nearest integer".

If we assign a REAL value to a COMPLEX, the REAL value is converted to a COMPLEX value with a zero imaginary part.

If we assign a COMPLEX value to a REAL, the imaginary part is discarded.

There is a restriction involving COMPLEX variables. While it is legal to assign a value to a COMPLEX variable Z using a literal, as in

         Z = (1.0, 3.4)

it is not acceptable to do so using a pair of REAL variables A and B, as in

         A = 1.0
         B = 3.4 
         Z = (A, B)

Instead, we would use the intrinsic function CMPLX:

         Z = CMPLX(A, B)