Statements

This section is about simple statements. Control structures, which span several lines and include simple statements are discussed later. We also save the detailed discussion of input / output for later.

Non-executable statements

As we mentioned earlier, program units consist of a specification part of non-executable statements, followed by an execution part. Here are the possible statements in the specification part.

Type declaration

All variables have a data type, numerical, character, or logical. There are various ways of declaring the data type of a variable.

Implicit declaration

First of all, there is implicit declaration of variables. Any variable with a name starting with I,J,...,N is an INTEGER; any other starting character of its name makes it a REAL. Until it is assigned to, an implicitly declared variable has value zero.

You can change this implicit declaration scheme by a statements such as

      IMPLICIT INTEGER(H-K)
      IMPLICIT LOGICAL(P-R)
Implicit declaration is very dangerous, since any typo will result in a new and unintended variable, with value zero. Many compilers recognise the IMPLICIT NONE statement (not part of Fortran77), which forces you to declare every variable explicitly, if they don't, there is usually a flag to make the compiler complain about undeclared variables.

Explicit declaration

Explicit data declarations have the form
      DATATYPE LIST-OF-NAMES
where DATATYPE is any of the simple datatypes, and the list is a comma-separated series of variables.

Dimension statement

For any of the simple datatypes you can form arrays of such values. There are two ways of declaring an array. First of all you can simply attach the dimension to the variable in an explicit type declaration, eg
      REAL X(20)
The alternative is to declare the type and the size of the array separately. If the type has been declared explicitly or implicitly, the size is specified as
      DIMENSION X(20)
      DIMENSION Y(5,3),Z(50,-1:1)
et cetera.

Data initialisation

Most variables get their value assigned during the run of a program, either in an assignment, or through an input statement. Sometimes, however, it is convenient to let a variable have a value right from the start of the program. This can be done with the DATA statement. Example: a single variable is initialised with
      DATA X/1.0/
Two variables can be initialised either as
      DATA X/1.0/,Y/2.5/
or
      DATA X,Y/1.0,2.5/
Data statements can be used to initialise arrays in various ways.

Variables initialised in a DATA statement do not keep their initial value forever: once that variable appears in an assignment or READ statement, its initial value is gone. Also, the value in the DATA statement is only assigned at compile time, that is, it is present at the start of the program. If the statement appears in a subprogram and the variable is assigned to the first time the subprogram is executed, the initial value is no longer in place the second time the subprogram is executed.

Parameter statement

If a certain number, eg pi/2, appears often in one program, it is a good idea not to write out the value each time, but to introduce a name for it. Example:
      PARAMETER (HALFPI = 1.57)
This makes your code more intelligible, and if ever the number has to be changed, you need only change it in one instance.

You can introduce several parameters in one statement:

      PARAMETER (N = 3, M = 5)
and you can define parameters in terms of other parameters:
      PARAMETER (PI = 3.14159)
      PARAMETER (HALFPI = PI/2)
You can even use functions such as MAX in the parameter statement, but you can not involve ordinary variable or subprogram arguments. The rule is that the value has to be computable by the compiler, before the program is ever run.

The variable in a parameter statement looks a lot like an ordinary variable, eg you have to declare its type, and the statement itself looks like an assignment. Do not let that fool you. Such an identifier is really a named constant. You can not use it in an ordinary assignment.

The name parameter is somewhat unfortunate, since this term is often also used as synonym of `argument'.

Common blocks

Fortran does not have a block structure like Pascal, or the concept of file like C, to let variables be shared between subprograms. Instead, you can declare variables to be in a COMMON block. Example:
      COMMON /PROGVARS/MAXSIZE,MINSIZE
It is necessary to declare the type of variables in common, and inside one common block all variables have the same type. To share variables of more than one type, you need more than one common block. Example:
      COMMON /IVARS/IMAX,IMIN
      COMMON /RVARS/RMAX,RMIN
These are named common blocks; there can be exactly one blank common block, which is simply a list of variables.

Data in blank common can not be initialised with a DATA statement; data in named common can only be initialised in a completely unexecutable program unit: a BLOCK DATA unit. This type of unit contains only non-executable statements.

Equivalence statement

A relic. Do not use it.

Save statement

This is discussed in the section on subprograms.

External and Intrinsic statements

These are discussed in the section on subprograms.

Format statement

This non-executable statement can be placed anywhere among the executable statements; it is discussed in the section on I/O.

Statement function definition

If a computation that can be expressed in a single expression occurs often within one subprogram, you could turn it into statement function. Statement functions are discussed in detail elsewhere; they are placed after all specification statements, and before the execution part.

In addition to the function definition, you need to declare the type of the function name and its arguments in the function definition.

Executable statements

Here are the main simple executable statements. Multi-line constructs (control structures) are discussed elsewhere.

Assignment

There are two main things you can do with a variable: use its value, and change its value. Changing the value of a variable is done by
      VAR = value
where value can be another variable, a constant value, an array element, or an expression. The interpretation of this statement is: evaluate the right-hand side to find its value, then assign that to the variable on the left-hand side.

And assignment statement may look like a mathematical equility, but do not let that deceive you. The line

      X = X+1
cannot possibly be mathematically true, but as an assignment it means "find the current value of X, add one to it, and assign that value to X".

There are other ways of changing the value of a variable, namely by passing it as a subroutine or function argument, through initialisation in a data statement, or by reading in the value.

Input / output

Read and write statements are discussed in extenso elsewhere.

Subroutine invocation

The invocation of a subroutine is an executable statement. The forms are
      CALL SPROG
for a routine without arguments, and
      CALL SPROG(ARG1,ARG2,...)
for routines with arguments.

CONTINUE statement

The CONTINUE statement
      CONTINUE
has no effect. Its main purpose is to serve as target for jumps, or as the final statement of DO loops.

Labeled statements

There are two types of labeled statements: executable ones (as target for a jump or end of a DO loop), and the FORMAT statement. Labels are placed in the first 5 columns of a line.