Before a computer can be used to solve the carbon dating problem, there must be a way of communicating the amount of remaining carbon in the artifact to the computer. It is also necessary to tell the computer how to solve the equation. These actions are accomplished with the assignment statement and arithmetic operations.
The simplest form of an expression is a constant, and the assignment may be used to give a variable its first value in a program, that is, to initialize it. Assume that pi has been declared to be a REAL variable. The assignment
stores the number 3.141593 in the memory location reserved for the variable pi. pi can now be used elsewhere in the program in place of the constant 3.141593.
Example
In the carbon dating problem an assignment statement could be used to assign a value to the memory location containing the value for the remaining portion of carbon 14. First a variable for this purpose would need to be typed as a REAL
REAL carbonThen an assignment statement could be used to give the variable a value
carbon = 0.5
It is also possible to use a DATA statement to initialize variables.
Two variables can also store the same value. Suppose I and J have been declared INTEGER variables. Then the following statements cause the number stored in the location reserved for I to be fetched from memory and stored in the location reserved for J. The contents of I are unchanged.
If the expression involves an arithmetic operation, it is evaluated until a single number results. This single number is then stored in the memory location reserved for the variable name.
Example
An assignment statement will be used in the carbon 14 dating problem to establish a memory location for the age of the artifact. First, the variable must be typed as a REAL
REAL ageThen, an expression will be written to calculate the remaining portion of carbon 14 and placed into the variable age.
age = (-log(carbon))/0.0001216
It is important to note that the variable name must appear on the left side of the equal sign.
Assignment statements should not be interpreted as algebraic equations in the usual sense. The equal sign should be interpreted as "is assigned the value of" or "is replaced by".
Table 1 lists the form of operators used in FORTRAN arithmetic expressions.
Operation | Algebraic Form | FORTRAN Form |
---|---|---|
addition | A + B | A + B |
subtraction | A - B | A - B |
multiplication | A x B | A * B |
division | A / B | A / B |
exponentiation | A ** n |
Example
Then Table 2 shows some possible assignments and their meaning.
Assignment | Meaning |
---|---|
total = 0.0 | stores the value 0.0 in the variable total |
kount = kount + 1 | adds 1 to the current value of kount |
total = 2.0 * radius | multiplies the value of the variable radius by 2.0 and stores the resulting value in the variable total |
sum = total | stores the value of the variable total in the variable sum |
cir = pi * radius ** 2 | squares the value of the variable radius, multiplies the result by pi, and stores the resulting value in the variable cir |
Since an arithmetic expression can contain more than one operation, it is necessary to establish a hierarchy of operations. The hierarchy of operations is the same in FORTRAN as it is in mathematics:
Example
Just as there is a need for a hierarchy of operations, there is also a need for a hierarchy of types. The hierarchy of data types in FORTRAN is
In a mixed-type expression like (2/4.0), the operand lower in hierarchy gets advanced to the higher type before the operation is carried out. So, the expression (2/4.0) is converted to (2.0/4.0) before the division is performed. The division is, therefore, real division which results in the real value 0.5.
A real value that appears in an integer declaration statement will be truncated to an integer. Care must be taken to avoid truncating values due to mixed mode assignments. For example, if AREA is declared to be an integer variable and assigned the value 11.5878, the result will be AREA = 11 after truncation.
To avoid confusion and unexpected results, do not mix types in expressions or assignment statements.
2. What is the value of each of the following FORTRAN expressions?
3. Identify the errors in the following assignment statements:
4. What value is stored for x or i in each of the following assignment statements assuming that x is declared to be a REAL and i is declared to be an INTEGER?