A Fortran statement is a line (or several, continued) that starts with a keyword, or is an assignment. The keywords are not reserved (Lexical structure), so the viewer decides from the whole statement whether the first word is a keyword.
Assignment
variable = expression: intrinsic assignment of numbers, logicals, characters, derived types (component by component) and whole arrayspointer => target: pointer assignment (Fortran 90)f(x) = x**2 + 1in a specification part followed by executable code: a statement function (FORTRAN 66 onward, obsolescent since Fortran 95). Because it looks like an assignment to an array element, nothing in it is coloured as a keyword- defined assignment through
interface assignment(=)
IF
if (x < 0) x = -x ! logical IF: one statement
if (x > 0) then ! block IF
sign = 1
else if (x < 0) then
sign = -1
else
sign = 0
end if
check: if (ok) then ! construct names
end if check
if (n) 10, 20, 30 ! arithmetic IF: negative, zero, positive
The logical if takes any executable statement except another if or a block opener; the viewer reads the statement after the parenthesis as a new statement
(if (a) read = 1 assigns to a variable named read). The arithmetic if (three labels) is obsolescent from Fortran 90 and was deleted from the Fortran 2018 standard (compilers still read it).
DO
do i = 1, n ! counted loop, step 1
do i = n, 1, -1 ! with a step
do ! no control: leave with exit
read (u, *, end=99) x
if (x < 0) exit
end do
do while (err > tol) ! test before each pass
end do
do concurrent (i = 1:n, j = 1:m, mask(i, j)) local(t) shared(a) default(none)
a(i, j) = b(i, j) * t
end do
do 10 i = 1, n ! labelled form
s = s + a(i)
10 continue
outer: do i = 1, n
cycle outer ! next pass of the named loop
exit outer ! leave it
end do outer
- the number of passes is computed once on entry:
max((end - start + step) / step, 0). Changing the loop variable inside the loop is an error - a real or double precision loop variable was allowed until Fortran 90 and deleted in Fortran 95
cyclegoes to the next pass;exitleaves the loop. With a construct name they act on that construct. Since Fortran 2008exitcan leave any named construct (ablock, anif)do concurrent(Fortran 2008) says that passes are independent. Fortran 2018 added the locality specifierslocal,local_init,sharedanddefault(none); Fortran 2023 addedreduce(op : variable). A type specifier may stand before the control (do concurrent (integer :: i = 1:n), Fortran 2008)- sharing one label between loops, or ending a labelled
doin anything butend doorcontinue, was obsolescent from Fortran 90 and deleted in Fortran 2018. The labelleddoitself is reported as obsolescent from Fortran 2018 (see Versions and standards)
SELECT CASE, SELECT TYPE and SELECT RANK
select case (n)
case (1)
print *, 'one'
case (2:5, 7) ! a range, and a list
print *, 'few'
case default
print *, 'many'
end select
select type (shape) ! polymorphic: Fortran 2003
type is (circle)
print *, shape%radius
class is (polygon)
print *, shape%sides
class default
print *, 'unknown'
end select
select rank (a) ! assumed-rank argument: Fortran 2018
rank (0)
print *, a
rank (1)
print *, a(1)
rank (*)
print *, 'assumed size'
rank default
print *, 'higher rank'
end select
The selector of select case is an integer, character or logical scalar; cases may not overlap and control does not fall through to the next case. select type works on a polymorphic variable
(class(*) or class(parent)) and select rank on an assumed-rank dummy argument. Each form may be named like any construct.
ASSOCIATE, BLOCK, CRITICAL and teams
associate (r => mesh%cells(i)%radius, h => height(i) + 1.0) ! names for variables and values
print *, pi * r**2 * h
end associate
block ! a local scope with its own declarations (Fortran 2008)
integer :: tmp
tmp = i
end block
critical ! one image at a time (Fortran 2008)
total = total + 1
end critical
change team (t, x[*] => y) ! Fortran 2018
end team
GO TO and other jumps
go to 100(alsogoto 100): unconditional jump to a labelled statement of the same unitgo to (10, 20, 30), i: computed go to, to the i-th label (obsolescent from Fortran 95)assign 10 to kandgo to k: the assigned go to, andassign … tofor formats, deleted in Fortran 95- a jump into a construct from outside is not allowed, and a jump to the
end ifof a block from outside it was deleted in Fortran 95 stop,stop 'message',stop 2,error stop(Fortran 2008: an error termination of all images, with an optional code or message),stop quiet=.true.(Fortran 2018, no message);pause(deleted in Fortran 95)returnfrom a subprogram (andreturn 2, an alternate return to the second*label, obsolescent from Fortran 95);exit;cycle;continue(does nothing, a place for a label)end,end program,end subroutineand so on close a program unit or construct;containsseparates a unit’s body from its internal or module procedures;entryadds another entry point to a subprogram (obsolescent in practice)
ALLOCATE and friends
allocate(a(n), b(0:m), stat=ierr, errmsg=msg), withsource=xormold=x(Fortran 2003) and a type:allocate(character(len=10) :: s),allocate(circle :: shape)deallocate(a, stat=ierr);nullify(p)call move_alloc(from, to)(Fortran 2003), the only way to pass an allocation without copying
Parallel statements
sync all, sync images(list), sync memory, sync team(t), lock(l), unlock(l), event post(e), event wait(e), form team(n, t), fail image, error stop.
See Parallel features.
Input and output statements
open, close, read, write, print, rewind, backspace, endfile, flush, inquire, wait and the non-executable format and namelist.
See Input and output.
Specification statements
| Statements | Page |
|---|---|
use, import, implicit, implicit none | Types and declarations, Program units and procedures |
type declarations; allocatable, asynchronous, bind, codimension, contiguous, dimension, external, intent, intrinsic, optional, parameter, pointer, private, protected, public, save, target, value, volatile | Types and declarations |
common, equivalence, data, namelist, enum, enumerator | Types and declarations |
type … end type, sequence, procedure, generic, final, abstract interface, interface … end interface | Derived types and objects |
format | Format edit descriptors |
include | File structure |
Order of statements in a unit
- the unit’s header (
program,module,subroutine,functionand so on) usestatementsimport(interface bodies only), thenimplicit- declarations;
formatandentrymay appear among them, and in FORTRAN 77datacould be mixed with executable statements - executable statements
contains, then internal or module subprogramsend
All keywords
The words below are keywords in some statement of Fortran 2023 or of the common extensions. None is reserved. Words marked with a dagger are extensions.
abstract access action advance all allocatable allocate assign assignment associate asynchronous
automatic† backspace bind blank block block data call case change team class close codimension common concurrent
contains contiguous continue critical cycle data deallocate default deferred dimension do elemental else
else if elsewhere end endfile entry enum enumeration type enumerator equivalence error stop event post event wait
exit extends external fail image file final flush forall form team format formatted function generic
go to if images implicit import impure in include inout inquire intent interface intrinsic
is local local_init lock map† memory module namelist none non_intrinsic non_overridable non_recursive nopass
nullify only open operator optional out parameter pass pause pointer post precision print
private procedure program protected public pure quiet rank read record† recursive reduce result
return rewind save select sequence shared simple static† stop structure† submodule subroutine sync
target team then to type typeof classof union† unformatted unlock use value volatile wait where while write
The viewer colours each of these words only where the grammar allows the keyword, as explained above.
Deleted and obsolescent statements
The summary is on Versions and standards. A compiler reads the deleted ones as extensions, and so does the viewer.