## 3 ASSIGNMENT STATEMENTS and ARITHMETIC OPERATIONS

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.

### Assignment Statements

The assignment statement establishes a name for a memory location and places a value into that location. The general form of an assignment statement in FORTRAN is
variablename = expression

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

pi = 3.141593

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 carbon
```
Then 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.

I = 13
J = I

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

```
INTEGER a,b
REAL cir

a = 4                 stores the value 4 in the memory location reserved
for the variable
a b = 3 + a           evaluates (3 + 4) then stores the resulting value (7)
in the memory location reserved for the variable b

cir = 2.0 * 3.14159 * 6.0     2.0 x 3.14159 x 6.0 is evaluated and the
resulting value is stored in the variable cir
```

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 age
```
Then, 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.

Table 1: Arithmetic Operators
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

Assume a program contains the following declarations and assignment statements:

REAL total, sum, cir, radius, pi
INTEGER kount

Then Table 2 shows some possible assignments and their meaning.

Table 2: Assignments
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:

1. operations within parentheses
2. exponentiation
3. multiplication and division (in order from left to right)
4. addition and subtraction (in order from left to right)

Example

```
Expression             Order of Evaluation

24 / (10 +2)
\ /             1). 10 + 2 = 12
24 / 12
\ /                2). 24/12 = 2
2
```
Example
```
Expression             Order of Evaluation

5 + 16 / 2 * 3
\ /               1). 16/2 = 8
5 + 8 * 3
\ /             2). 8*3 = 24
5 + 24
\ /                3). 5 + 24 = 29
29
```
Example
```
Expression             Order of Evaluation

(32 / (6 + 2)) ** 2
\ /            1). 6 + 2 = 8
(32 / 8) **2
\ /               2). 32/8 = 4
4 ** 2
\ /             3). 4**2 = 16
8
```

### Mixed Mode Operations

When the operands are of the same type, the result of the expression will be of that type. Mixing types (mixed mode operations) can cause some surprising results and should be avoided. The value produced by an operation is not only determined by the operator, but also by the types of its operands. For example, the expression (2.0/4.0) has the value 0.5 because a real divided by a real results in a real. The expression (2/4) has the value 0 because the result of an integer divided by an integer must be an integer. The result is truncated to the integer part of 0.5 which is 0.

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

1. double precision and complex
2. real
3. integer

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.

### EXERCISES

1. Write FORTRAN expressions for each of the following mathematical expressions. Assume that x, y, a, r, c, and b have been declared to be REAL variables.
A.
B.
C.
D.

2. What is the value of each of the following FORTRAN expressions?

A.
B.
C.
D.
E.

3. Identify the errors in the following assignment statements:

A.
B.
C.

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?

A.
B.
C.
D.