# The Assignment Statement

The assignment statement has the following form:
```variable = expression
```
Its purpose is saving the result of the expression to the right of the assignment operator to the variable on the left. Here are some rules:
• The expression is evaluated first with the rules discussed in the single mode or the mixed mode expressions pages.
• If the type of the expression is identical to that of the variable, the result is saved in the variable.
• Otherwise, the result is converted to the type of the variable and saved there.
• If the type of the variable is INTEGER while the type of the result is REAL, the fractional part, including the decimal point, is removed making it an integer result.
• If the type of the variable is REAL while the type of the result is INTEGER, then a decimal point is appended to the integer making it a real number.
• Once the variable receives a new value, the original one disappears and is no more available.
• CHARACTER assignment follows the rules stated in the discussion of the PARAMETER attribute.

### Examples:

• The program segment below declares three INTEGER variables. The first assignment statement saves an integer value to variable Unit. The second saves a real number 100.99 into variable Amount. However, since Amount is an INTEGER variable, the real value 100.99 is converted to an integer, 100, and saved into Amount. Thus, after the second assignment completes, variable Amount holds 100. The third assignment computes the single mode expression, yielding a result 500 = 5*100. Thus, variable Total receives 500.
```INTEGER :: Total, Amount, Unit

Unit   = 5
Amount = 100.99
Total  = Unit * Amount
```
• In the following, PI is a PARAMETER and is an alias of 3.1415926. The first assignment statement puts integer value 5 into integer variable Radius. The expression in the second assignment is first evaluated, yielding a result 78.539815, which is then saved into REAL variable Area.
```REAL, PARAMETER :: PI = 3.1415926
REAL            :: Area

Area   = (Radius ** 2) * PI
```
• In the following, Counter is an INTEGER variable initialized to zero.

The meaning of the first assignment is computing the sum of the value in Counter and 1, and saves it back to Counter. Since Counter's current value is zero, Counter + 1 is 1+0 = 1 and hence 1 is saved into Counter. Therefore, the new value of Counter becomes 1 and its original value 0 disappears.

The second assignment statement computes the sum of Counter's current value and 3, and saves the result back to Counter. Thus, the new value of Counter is 1+3=4.

```INTEGER  ::  Counter = 0

Counter = Counter + 1
Counter = Counter + 3
```
• The following swaps the values in A and B, with the help of C. That is, after completing the following three assignment statements, A and B have 5 and 3, respectively.

Initially, A and B are initialized to 3 and 5, respectively, while C is uninitialized. The first assignment statement puts A's value into C, making A=3, B=5 and C=3.

The second assignment statements puts B's value into A. This destroys A's original value 3. After this, A = 5, B = 5 and C = 3.

The third assignment statement puts C's value into B. This makes A=5, B=3 and C=3. Therefore, the values in A and B are exchanged.

```INTEGER  :: A = 3, B = 5, C

C = A
A = B
B = C
```
The following is another possible solution; but, it uses one more variable.
```INTEGER :: A = 3, B = 5, C, D

C = A
D = B
A = D
B = C
```

### An Important Note:

A name declared with the PARAMETER attribute is an alias of a value and is not a variable. Therefore, it cannot be used on the left-hand side of =, although it can be used on the right-hand side. The following is wrong!
```INTEGER, PARAMETER  :: InchToCM = 2.54, factor = 123.45
INTEGER             :: X = 15

InchToCM = factor * X
```