next up previous contents index
Next: Expressions and Assignments Up: Basic Fortran Concepts Previous: Basic Fortran Concepts

Statements

To start with, here is one of the simplest program that can be devised:

 
       PROGRAM TINY 
       WRITE(UNIT=*, FMT=*) 'Hello, world' 
       END
As you can probably guess, all this program does is to send a rather trite message ``Hello, world" to your terminal. Even so its layout and structure deserve some explanation.

The program consists of three lines, each containing one statement. Each Fortran statement must have a line to itself (or more than one line if necessary), but the first six character positions on each line are reserved for statement labels and continuation markers. Since the statements in this example need neither of these features, the first six columns of each line have been left blank.

The PROGRAM statement gives a name to the program unit and declares that it is a main program unit. Other types of program unit will be covered later on. The program can be called anything you like provided the name conforms to the Fortran rules; the first character of a Fortran symbolic name must be a letter but, unfortunately, they cannot be more than six characters long in total. It is generally sensible to give the same name to the program and to the file which holds the Fortran source code (the original text).

The WRITE statement produces output: the parentheses enclose a list of control items which determine where and in what form the output appears. UNIT=* selects the standard output file which is normally your own terminal; FMT=* selects a default output layout (technically known as list-directed format). Asterisks are used here, as in many places in Fortran, to select a default or standard option. This program could, in fact, have been made slightly shorter by using an abbreviated form of the WRITE statements:

 
      WRITE(*,*) 'Hello, world'
Although the keywords UNIT= and FMT= are optional, they help to make the program more readable. The items in the control list, like those in all lists in Fortran, are separated by commas.

The control information in the WRITE statement is followed by a list of the data items to be output: here there is just one item, a character constant which is enclosed in a pair of apostrophe (single quote) characters.

An END statement is required at the end of every program unit. When the program is compiled (translated into machine code) it tells the compiler that the program unit is complete; when encountered at run-time the END statement stops the program running and returns control to the operating system.

The Standard Fortran character set does not contain any lower-case letters so statements generally have to be written all in upper case. But Fortran programs can process as data any characters supported by the machine; character constants (such as the message in the last example) are not subject to this constraint.


next up previous contents index
Next: Expressions and Assignments Up: Basic Fortran Concepts Previous: Basic Fortran Concepts
Helen Rowlands
8/27/1998