Language elements

Character set

Alphanumeric: A - Z (also a - z), 0 - 9 and _ (underscore)

Special characters

= equals sign : colon
+ plus sign blank
- minus sign ! exclamation mark
* asterisk " quotation mark
/ slash % percent
( left parenthesis & ampersand
) right parenthesis ; semicolon
, comma < less than
. decimal point > greater than
' apostrophe ? question mark

Beware of typing errors: O, 0, o, DO, D0, do, d0, I, 1, l, L, U, V, u, v, 2, Z, z

Tokens

Significant sequences of characters

Free format source form

Up to 132 characters per line.

Up to 39 continuation lines, indicated by an ampersand (&) at the end of the line to be continued and (recommended option) by another & at the beginning of the continuation line.

Comments are indicated by an exclamation mark (!). Stand-alone comment lines or end-line comments. Cannot be continued.

Make judicious use of comments.

Leading blanks or empty lines are ignored and can be used to enhance the layout.

Multiple short statements on the same line, separated by ; (semicolon) are possible but make the code usually less readable.

A statement label of 1 - 5 digits may precede the statement.

Intrinsic data types

Constants and variables

Constants

Integer

Typical range (32 bits): [-2147483648, +2147483647]

Examples: 286, -7, 0, 1001, -999

There are also unsigned binary, octal or hexadecimal constants.

Examples: b'101101011', o'553', z'16b' (decimal 363)

Real

Use decimal point, not comma.

Typical range (32 bits): -10^38, ..., -10^-38, -0., +0., +10^-38, ..., +10^38

Precision: 6 - 7 decimal digits

Examples: 0.0, 0., 1., .1, 0.1, 1e6, 1.e-7, 6.02e23

Complex

Real and imaginary components

Example: (2.3, -0.45)

Character

Character strings, delimited by apostrophs (') or quotation marks (").

Examples: 'Hello, world!', "It's a nice day.", 'Metcalf & Reid'

Logical

.false.
.true.

Names

Up to 31 alphanumeric characters of which the first must be a letter.

There are no reserved words.

Examples: k, n1, c0, vacuum_speed_of_light

Variables

Make use of strong typing, i.e. switch off implicit typing.

implicit none

Recommendation: Let names start with (i - n) for integers, (a - h, o - z) for reals.

Scalar

Type declaration statements

Examples:

integer :: i, j, k1, k2
real :: a, t1, t2, x, y, z0
complex :: z
character :: letter ! a single character
character(len=80) :: line
logical :: eof ! end of file

Arrays

Up to rank (number of dimensions) 7.

Add the dimension attribute to a type declaration.

Default lower bound 1. A different lower bound can be declared.

Examples:

real, dimension(10) :: a ! 10 elements
integer, dimension(0:10) :: ib ! 11 elements
real, dimension(-10:10) :: c ! 21 elements
integer, dimension(5, -2:3) :: id ! rank 2

Extent

Number of elements along a dimension.

Shape

Sequence of extents. Array id (above) is of shape (5, 6).

Array element order

First index runs fastest. Contiguous storage on most implementations (not required).

Individual elements are referenced by specifying their subscript values.

Subarrays (sections) may be referenced by specifying a range for one or more subscripts.

Examples: a(2:5), id(2:4, -1:2)

In addition to constants, other forms of subscripts are available, e.g, scalar integer expressions, arrays of indices.

Array constants

Rank 1 examples:

(/ 1, 2, 3, 4, 5 /)
(/ (i, i = 1, 5) /)

Initial values for variables

Examples:

real :: a = 0.0
real, dimension(5) :: b = (/ 1.1, 1.3, 1.5, 1.7, 1.9 /)
real, dimension(5) :: c = (/ (i*0.1, i=11,19,2) /) ! stride 2
real, dimension(-3:3) :: d = 1.0 ! all elements

Named constants

Examples:

integer, parameter :: ivmax = 11 ! max. vib. quantum number
real, parameter :: h = 6.626e-34 ! Planck's constant
real, parameter :: pi = 3.141592653, c0 = 299792458.

May I have a large container of coffee right now? (mnemonic for pi)

A named constant may be an array.

Example:

integer, dimension(7), parameter :: nf=(/1,1,2,3,5,8,13/)

Character substrings

Examples:

character(len=80) :: line
line(:i)  ! same as line(1:i)
line(i:)  ! same as line(i:80)
line(:)   ! same as line or line(1:80)
line(i:i) ! single character
character(len=80), dimension(60) :: page ! 60 lines
page(j)(i:i) ! character i on line j