The steps every programmer has to follow in order to code a good program, in any language, are the following.
The programs which will be presented here are all going to be in source code form. The actual compiling and linking is done by the Fortran 90 compiler & linker on your computer. Once this is done, the program can be executed.
FORTRAN 90 has a free format style as follows:-
Comments
Comments come after an exclamation mark ! which can be in column 1 as shown in the first program. Inline comments are now allowed, where any source code line can have a comment provided the FORTRAN 90 statement is followed by the !. Thus the second PRINT statement in the first program could have been commented as follows:-
PRINT *, 'To-day is Wednesday' ! Or another weekday
Labels
Apart from numeric statement numbers (1 - 99999) described in FORTRAN 77, alphanumeric labels (e.g. LOOP1) are allowed as well, provided these are the first element of a new FORTRAN 90 statement and are followed by a colon (:).
Column Layout
Statement layout is not restricted to any particular columns.
Continuation
Continuation is shown by using the ampersand (&) at the end of one line (before any ! and comment). It may also be used on the following line to show exactly where a string is to be continued. This is useful, since in general, spaces are ignored between keywords (like PRINT and END), and separators (such as commas), but are significant in strings. The previous example could be written on 2 lines using continuation as follows:-
PRINT *, 'To-day is & ! We have broken this string in 2 &Wednesday' ! Need & to have only 1 space after "is"
Multiple Statements
Multiple statements per line can be used, provided they are separated by semicolons (;), as in
PRINT *, 'To-day'; PRINT *, 'is'; PRINT *, 'Wednesday'
This crowding of multiple statements per line is possible but not recommended.
The first program used the simplest (list-directed) form of the PRINT statement.
PRINT *,list
Let's look at a very simple FORTRAN 90 program.
Click here to get the source code, data and output of this program to your computer.
! ! =====> Program - P11.F90 ! ! PRINT *, 'This is Program >> P11 - Prints a string' ! PRINT *, 'TO-DAY IS WEDNESDAY' END OUTPUT: [FTN90 Version 1.12 Copyright (c)SALFORD SOFTWARE LTD 1992 & ] [ (c)THE NUMERICAL ALGORITHMS GROUP 1991,1992] NO ERRORS [FTN90] Program entered This is Program >> P11 - Prints a string TO-DAY IS WEDNESDAY
Computers can perform the four basic \ind{operations} of \ind{arithmetic} and these are coded in FORTRAN 90 as follow:-
+Addition
-Subtraction
*Multiplication
/Division
Brackets can be used in pairs to show the order of calculations to be done.
An example will show how these can be used.
Click here to get the source code, data and output of this program.
! ! =====> Program - P12.F90 ! ! PRINT *, 'This is Program >> P12 & & - Adds two numbers' ! ! Simple program to add 2 numbers PRINT *,2,3,2+3 END OUTPUT: This is Program >> P12 - Adds two numbers 2 3 5
A little more complex program now:
Click here to get the source code, data and output of this program.
! ! =====> Program - P13.F90 ! ! PRINT *, 'This is Program >> P13 - & &Prints a string and a result' ! ! Simple program to show output of a string ! and a result PRINT *, '2+3 =' , 2+3 END OUTPUT: This is Program >> P13 - Prints a string and a result 2+3 = 5
And here is an example showing how to calculate the final cost of a product with the addition of taxes.
Click here to get the source code, data and output of this program.
! ! =====> Program - P14.F90 ! ! ! THIS PROGRAM SHOWS A ! MORE COMPLEX NUMERIC ! CALCULATION ! $10 with 7% tax and then 6.5% tax on top of ! that - too much? ! PRINT *, 'This is Program >> P14 - & &Prints a tax calculation' ! ! PRINT *,'Total cost of $10.00 item with 2 taxes' PRINT *, 10.0 * ( 1.0 + 0.07) & ! line broken up * (1.0 + 0.065) ! continuation END OUTPUT: This is Program >> P14 - Prints a tax calculation Total cost of $10.00 item with 2 taxes 11.3955011Do you want to take the Quiz on this section to verify if you understood it well? If so, click here.
Page builder: Charles Boivin
Last modified: 11/07/95