The Queen's University of Belfast

Parallel Computer Centre
[Next] [Previous] [Top]
Sources
Types
Control Structures
Sources, types
and control structures
Topics
Source form
Free source form
- Lines up to 132 characters
- Lowercase letters permitted
- Names up to 31 characters (including underscore)
- Semicolon to separate multiple statements on one line
- Comments may follow exclamation (!)
- Ampersand (&) is a continuation symbol
- Character set includes + < > ; ! ? % - " &
- New relational operators: `<`, `<=', `==',
`/=', `>=', `>'
Example
PROGRAM free_source_form
! Long names with underscores
! No special columns
IMPLICIT NONE
! upper and lower case letters
REAL :: tx, ty, tz ! trailing comment
! Multiple statements per line
tx = 1.0; ty = 2.0; tz = tx * ty;
! Continuation on line to be continued
PRINT *, &
tx, ty, tz
END PROGRAM free_source_form
Specifications
type [[,attribute]... ::] entity list
- type can be INTEGER, REAL, COMPLEX, LOGICAL or CHARACTER with optional kind value:
- INTEGER [(KIND=] kind-value)]
- CHARACTER ([actual parameter list])
([LEN=] len-value and/or [KIND=] kind-value)
- TYPE (type name)
- attribute can be
PARAMETER, PUBLIC, PRIVATE,
ALLOCATABLE, POINTER, TARGET,
INTENT(inout), DIMENSION (extent-list),
OPTIONAL, SAVE, EXTERNAL,
INTRINSIC
- Can initialise variables in specifications
Examples
! Integer variables:
INTEGER :: ia, ib
! Parameters:
INTEGER, PARAMETER :: n=100, m=1000
! Initialisation of variables:
REAL :: a = 2.61828, b = 3.14159
! Character variable of length 8:
CHARACTER (LEN = 8) :: ch
! Integer array with negative lower bound:
INTEGER, DIMENSION(-3:5, 7) :: ia
! Integer array using default dimension:
INTEGER,DIMENSION(-3:5, 7) :: ia, ib, ic(5, 5)
IMPLICIT NONE
- In Fortran 77, implicit typing permitted use of undeclared variables. This has been the cause of many programming errors.
- IMPLICIT NONE forces you to declare all variables.
- IMPLICIT NONE may be preceded in a program unit only by USE and FORMAT statements (see Order of statements).
Kind values
- 5 intrinsic types: REAL, INTEGER, COMPLEX, CHARACTER, LOGICAL
- Each type has an associated non negative integer value called the KIND type parameter
- Useful feature for writing portable code requiring specified precision
- A processor must support at least 2 kinds for REAL and COMPLEX, and 1 for INTEGER, LOGICAL and CHARACTER
- Many intrinsics for enquiring about and setting kind values
REAL
REAL (KIND = wp) :: ra ! or
REAL(wp) :: ra
- Declare a real variable, ra, whose precision is determined by the value of the kind parameter, wp
- Kind values are system dependent
- An 8 byte (64 bit) real variable usually has kind value 8 or 2
- A 4 byte (32 bit) real variable usually has
kind value 4 or 1
- Literal constants set with kind value:
const = 1.0_wp
REAL
- Simplest example is to replace DOUBLE PRECISION:
INTEGER, PARAMETER :: idp = KIND(1.0D0)
REAL (KIND = idp) :: ra
ra is declared as `double precision', but this is system dependent.
- To declare real in system independent way, specify kind value associated with precision and exponent range required:
INTEGER, PARAMETER :: &
i10 = SELECTED_REAL_KIND(10, 200)
REAL (KIND = i10) :: a, b, c
a, b and c have at least 10 decimal digits of precision and the exponent range 200.
INTEGER
- Integers usually have 16, 32 or 64 bit
- 16 bit integer normally permits
-32768 < i < 32767
- Kind values for each supported type
- To declare integer in system independent way, specify kind value associated with
range of integers required:
...
INTEGER, PARAMETER :: &
i8 = SELECTED_INT_KIND(8)
INTEGER (KIND = i8) :: ia, ib, ic
...
ia, ib and ic can have values between -108 and +108 at least (if permitted by processor).
Intrinsics
INTEGER, PARAMETER :: &
i8 = SELECTED_INT_KIND(8)
INTEGER (KIND = i8) :: ia
PRINT *, HUGE(ia), KIND(ia)
This will print the largest integer available for this integer type (2147483674), and its kind value.
INTEGER, PARAMETER :: &
i10 = SELECTED_REAL_KIND(10, 200)
REAL (KIND = i10) :: a
PRINT *, RANGE(a), PRECISION(a), KIND(a)
This will print the exponent range, the decimal digits of precision and the kind value of a.
Derived types
- Defined by user
- Can include different intrinsic types and other derived types
- Components accessed using percent (%)
- Only assignment operator (=) is defined for derived types
- Can (re)define operators - see later operator overloading
Examples
- Define the form of derived type
TYPE vreg
CHARACTER (LEN = 1) :: year
INTEGER :: number
CHARACTER (LEN = 3) :: place
END TYPE vreg
- Create the structures of that type
TYPE (vreg) mycar1, mycar2
- Assigned by a derived type constant
mycar1 = vreg(`L', 240, 'VPX')
- Use % to select a component of that type
mycar2%year = `R'
TYPE (vreg), DIMENSION (n) :: mycars
- Derived type including derived type:
TYPE household
CHARACTER (LEN = 30) :: name
CHARACTER (LEN = 50) :: address
TYPE (vreg) :: car
END TYPE household
TYPE (household) :: myhouse
myhouse%car%year = `R'
Control structures
- Three block constructs
- All can be nested
- All may have construct names to help readability or to increase flexibility
IF
[name:] IF (logical expression) THEN
block
[ELSE IF ( logical expression) THEN [name]
block]...
[ELSE [name]
block]
END IF [name]
select: IF (i < 0) THEN
CALL negative
ELSE IF (i == 0) THEN select
CALL zero
ELSE select
CALL positive
END IF select
DO loops
[name:] DO [control clause]
block
END DO [name]
- control clause may be:
- an iteration control clause
count = initial, final [,inc]
WHILE (logical expression)
- or nothing (no control clause at all)
- Iteration control clause:
rows: DO i = 1, n
cols: DO j = 1, m
a(i, j) = i + j
END DO cols
END DO rows
true: DO WHILE (i <= 100)
...
body of loop
...
END DO true
- Use of EXIT and CYCLE:
- exit from loop with EXIT
- transfer to END DO with CYCLE
- EXIT and CYCLE apply to inner loop by default but can refer to specific, named loop
outer: DO i = 1, n
middle: DO j = 1, m
inner: DO k = 1, l
IF (a(i,j,k) < 0.0) EXIT outer ! leave loops
IF (j == 5) CYCLE middle ! set j = 6
IF (i == 5) CYCLE ! skip rest of inner
...
END DO inner
END DO middle
END DO outer
DO
READ (*, *) x
IF (x < 0) EXIT
y = SQRT(x)
...
END DO
Notice that this form can have the same effect as a DO WHILE loop.
CASE construct
- Structured way of selecting different options, dependent on value of single expression
- Replacement for
- computed GOTO
- or IF ... THEN ... ELSE IF ... END IF constructs
[name:] SELECT CASE (expression)
[CASE (selector) [name]
block]
...
END SELECT [name]
- expression - character, logical or integer
- selector - DEFAULT, or one or more values of same type as expression:
- single value
- range of values separated by : (character or integer only), upper or lower value may be absent
- list of values or ranges
colour: SELECT CASE (ch)
CASE (`C', `D', `G':'M')
colour = `red'
CASE (`X':)
colour = `green'
CASE DEFAULT
colour = `blue'
END SELECT colour
[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker