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.
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.'
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.
Constants of type REAL are numbers which may include a fractional part. A REAL constant is written in one of the following forms:
+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.
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.
VOL TEMP A2 COLUMN IBM370
Spaces are ignored by FORTRAN, e.g. 'COL UMN' is equivalent to 'COLUMN'
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).
The type of a variable may be assigned explicitly by a type specification statement. This has the form:
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.
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:
READ *, variable_list
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.
READ *, VAR1, VAR2, VAR3
waits for three constants to be entered and assigns them in sequence to the variables VAR1, VAR2 and VAR3.
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.
TEMP = 74.5
ITEMP = 100
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:
Example: X = 5 is equivalent to X = 5.0
N = 0.9999 0
M = -1.9999 -1
[Contents] [Previous] [Next] [Home]
webmaster Massimo F. ARENA