We are now ready to write a simple FORTRAN program. All that is required is some information on printing output, program layout and a few simple statements.
The PRINT statement
Output can be printed using the PRINT statement, which is very similar to the READ statement shown on page 4:
PRINT *, output_list
where output_list is a single constant, variable or expression or a list of such items, separated by commas.
Example: PRINT *,'THE RESULTS ARE', X ,'AND',Y
The PRINT statement prints the output list on the terminal screen in a standard format. Later, we shall consider more flexible output statements which give us greater control over the appearance of the output and the device where it is printed.
The PROGRAM statement
A program can optionally be given a name by beginning it with a single PROGRAM statement. This has the form:
where program_name is a name conforming to the rules for FORTRAN variables. (see 'Variables' on page 3).
END and STOP
Each program must conclude with the statement END, which marks the end of the program. There must be no previous END statement.
The statement STOP stops execution of the program. In FORTRAN 77, but not in previous versions, END also has this effect. Therefore, if execution is simply to stop at the end of the program, STOP is optional. However, one or more STOP statements may be written earlier, to stop execution conditionally at points other than the end.
When FORTRAN was introduced, punched cards were a common input medium. FORTRAN was designed to make use of the cards' 80-column layout by ignoring spaces and reserving different fields of the card for different purposes. Although cards are no longer used, FORTRAN still uses this column-based layout.
All FORTRAN statements must be written in columns 7 to 72. A statement ends with the last character on the line, unless the next line has any character other than 0 in column 6. Any such character indicates that columns 7 to 72 are a continuation of the previous line.
Columns 73 to 80 are ignored by the compiler. Originally, these columns were used to print sequence numbers on the cards, but now they are normally unused.
Columns 1 to 5 are reserved for statement labels. These are optional unique unsigned non-zero integers used to provide a reference to statements.
The layout rules are summarised in Figure 5.
Columns Usage 1-5 Statement labels 6 Continuation character or blank 7-72 FORTRAN statements 73-80 Unused
Figure 5: FORTRAN layout
The letter 'C' or an asterisk in column one causes the compiler to ignore the rest of the line, which may therefore be used as a comment to provide information for anyone reading the program.
A simple program
The following example uses the FORTRAN statements introduced so far to solve a simple problem.
A driver fills his tank with petrol before setting out on a journey. Each time he stops for petrol he puts in 40 litres. At his destination, he fills the tank again and notes the distance he has travelled in kilometres. Write a program which reads the distance travelled, the number of stops and the amount of petrol put in at the end of the journey, and prints the average petrol consumption in kilometres per litre, rounded to the nearest litre.
PROGRAM PETROL INTEGER STOPS, FILLUP C C THESE VARIABLES WOULD OTHERWISE BE TYPED REAL BY DEFAULT C ANY TYPE SPECIFICATIONS MUST PRECEDE THE FIRST EXECUTABLE STATEMENT C READ *, KM,STOPS,FILLUP USED = 40*STOPS + FILLUP C COMPUTES THE PETROL USED AND CONVERTS IT TO REAL KPL = KM/USED + 0.5 C 0.5 IS ADDED TO ENSURE THAT THE RESULT IS ROUNDED PRINT *, 'AVERAGE KPL WAS',KPL
Figure 6: Petrol consumption program
This program illustrates some of the points about type conversion made in the previous chapter. In line 8, the number of litres of petrol used is computed. The computed value is of type INTEGER, but is converted to REAL when assigned to the REAL variable USED. In line 10, the expression KM/USED is evaluated as REAL, but would be truncated, not rounded, when assigned to the INTEGER variable KPL. Adding 0.5 before truncating has the effect of rounding up or down. This is a useful rounding method. It is illustrated further below.
KM/USED KM/USED + 0.5 KPL 12.0 12.5 12 12.4 12.9 12 12.5 13.0 13 12.9 13.4 13
[Contents] [Previous] [Next] [Home]
webmaster Massimo F. ARENA