# Constants and Variables

Data are constants provided to a program for use in computation (processing).

Results are constants produced as a result of computation.

We have seen that all information is represented in the computer in binary form. The type of information determines the way in which it is represented and the operations which may be performed on it.

## The CHARACTER type

A constant of type CHARACTER, (often called a string) is a sequence of characters which may be upper case alphabetic, numeric, blanks, and the following:

+ - * / = ( ) , . ' \$ :

When included in a FORTRAN statement, a string must be delimited by single quotes ('). A single quote may be included in a string by writing two consecutively. Only one is retained.

Example: 'WE''RE A'' JOCK TAMSON''S BAIRNS.'

## The INTEGER type

Constants of type INTEGER are integer numbers. An INTEGER constant is written as a sequence of decimal digits, optionally preceded by a sign (unary + or -).

Examples: 123 +1 0 4356 -4

INTEGER constants are represented in exact form. Their magnitude has a limit which depends on the word length of the computer.

## The REAL type

Constants of type REAL are numbers which may include a fractional part. A REAL constant is written in one of the following forms:

1. An integer part written as an INTEGER constant defined as above, followed by a decimal point, followed by a fractional part written as a sequence of decimal digits. Either the integer or the fractional part, but not both, may be omitted.
2. An INTEGER constant or a REAL constant defined as in (i), followed by a decimal exponent written as the letter 'E' followed by an INTEGER constant. The constant is a power of 10 by which the preceding part is multiplied.

Examples:

+123.4 -123.4 .6E-3 (0.6x10-3) 4.6E3 (4.6x103) 7E-3 2.

REAL constants are represented in approximate form. Their magnitude has a limit which depends on the word length of the computer.

## Variables

A variable is a unique name which a FORTRAN program applies to a word of memory and uses to refer to it. A variable consists of one to six upper case alphabetic characters and decimal digits, beginning with an alphabetic character.

Examples:

```	VOL	TEMP	A2	COLUMN	IBM370
```

Note:

Spaces are ignored by FORTRAN, e.g. 'COL UMN' is equivalent to 'COLUMN'

1. Clarity can be improved by choosing variables which suggest their usage, e.g.

DEGC MEAN STDDEV

The value of a variable is the constant stored in the word to which it refers. Each variable has a type, which stipulates the type of value it may have. The type of a variable may be specified explicitly, or assigned implicitly (by default).

### Explicit typing

The type of a variable may be assigned explicitly by a type specification statement. This has the form:

type variable_list

where type is the name of a type

and variable_list is a single variable or a list of variables, separated by commas.

The statement assigns the given type to all the variables in the list.

Examples:

INTEGER WIDTH

REAL NUM, K

Type specification statements are not compiled into executable machine code instructions. Instead the compiler records the names and types of the variables and reserves storage for them. Such non-executable statements must be placed at the beginning of a program, before the first executable statement.

### Implicit (or default) typing

If a variable is used without being included in a type specification, its type is assigned implicitly (by default) according to the following rule:

If the variable begins with a character from I to N, its type is INTEGER. Otherwise, it is REAL.

Thus TEMP is a REAL variable, while ITEMP is an INTEGER.

Note: Because a variable can be used without first being declared in a type specification, a misspelled variable is not in general detected as an error by the compiler. The program may compile and run, but produce incorrect results. Care should therefore be taken to get variable names right, and if unexpected results are obtained, variable names are one of the first things to check.

### Assigning a value

Before a variable can be used in computation, it must be assigned an initial value. This may be done by reading a value from input or by using an assignment statement.

The READ statement is used to assign values to variables by reading data from input. The simplest form of the READ statement is:

where variable_list is a single variable or a list of variables separated by commas. (The asterisk will be explained later).

This statement reads constants from the terminal, separated by spaces, commas, or new lines, and assigns them in sequence to the variables in the list. Execution of the program pauses until the right number of constants has been entered.

Example:

waits for three constants to be entered and assigns them in sequence to the variables VAR1, VAR2 and VAR3.

#### The assignment statement

The simplest form of assignment statement is:

variable = constant

This means that the constant is assigned as a value to the variable on the left-hand-side. Note that the '=' sign has a different meaning than in algebra. It does not indicate equality, but is an assignment operator.

Examples:

TEMP = 74.5

ITEMP = 100

#### Type rules

Whichever method is used to assign a value to a variable, the type of the value must be consistent with that of the variable. The rules are:

1. A CHARACTER value cannot be assigned to a numeric variable or vice versa.
2. An INTEGER value can be assigned to a REAL variable. The value assigned is the REAL equivalent of the integer.

Example: X = 5 is equivalent to X = 5.0

1. A REAL value can be assigned to an INTEGER variable. The value assigned is truncated by discarding the fractional part.:

Examples:

Value
Assigned

N = 0.9999    0

M = -1.9999   -1

[Contents]  [Previous]  [Next]  [Home]

NDP77
http://www.ndp77.net
webmaster Massimo F. ARENA
webmaster@ndp77.net
2004:02:14:17:30:17