|
|
|
if Statement
do Loops
do while Loops
exit
|
|
open & close
rewind
|
|
|
end
statement. The main unit starts with
the header: program
followed by the main program's name. Other units
represent subprograms and they begin with either a function
or a subroutine
header.
program
, function
, and
subroutine
); variables (implicit none
and
type declarations); and constants (parameter
).
!
. Note that each comment line must start with !
.
parameter
statement. In our style convention, these are named using all-caps.
program somename implicit none ! Variable / Constant Declarations ! executable statements ... end !-------------------------------- subroutine subname(parameters) implicit none .... end !-------------------------------- type function name (parameters) implicit none .... end
parameter
statement.
integer*1 code ! +/- 127, exact integer*2 count ! +/- 32K, exact integer*4 sample ! +/- 2G, exact real*4 amount ! +/- 10^38, accuracy 10^-6 real*8 density ! +/- 10^308, accuracy 10^-15 logical*1 done ! two values: .true. and .false. character*20 title ! a string of upto 20 char !--------------------------------------------------- ! You can optionally combine declarations of the ! different sizes of the same type: !--------------------------------------------------- character title*20, name*30, college*1 integer code*1, count*2, sample*4 real amount*4, density*8 !--------------------------------------------------- ! You can also initialize as you declare: !--------------------------------------------------- character title*20 / 'York' / integer*2 count / 0 / real*4 amount / 1.0 / !--------------------------------------------------- ! Note that title will have 20 characters even though ! we stored only 4 (they will be padded by blanks). !--------------------------------------------------- ! Some symbolic constants: !--------------------------------------------------- real*4 PI character*1 BASE parameter (PI=3.141592, BASE="N")
()
, **
, *
, /
,
+
, -
.
The precedence is highest for parentheses, followed
by exponentiation, multiplication / division, and then addition / subtraction. Operators
of the same precedence are evaluated left to right. Note that /
behaves
as a quotient operator when its two operands are integers, and as a division
operator when either operand is real.
//
for concatentation and (a:b)
for extracting (or setting)
a substring beginning at character position a
(or 1 if omitted) up to
and including character position b
(or the declared length if omitted).
.LT. .LE. .GT. .GE. .EQ. .NE.
.NOT. .OR. .AND. .XOR.
x = y + -4
or
x = 2y
); using relational
operators on logical variables (as in if (valid .eq. .true.)
).
if
statement if (condition A) then statements A else if (condition B) then statements B else statements C end ifNotes:
if (x .gt. 0) then ... else if (x .gt. 5) then
else
block
else if
blocks
if
statements; i.e.
if
statements
can be properly nested (one is fully contained within another w/o overlapping)
.true.
or .false.
), a logical variable (just the variable name), or a logical
expression (logical values combined using logical operators).
do
statement do var = start, stop, increment statements end doNotes:
int((stop-start)/inc)
times. If this number is not positive, the loop is skipped.
var = var + increment
and
repeats if the resulting value of var is within the [start,
stop] range.
EXIT
statement.
do while (condition) statements end doNotes:
.true.
This way, the do
while(.true.)
loops will always be infinite, and we use EXIT
(in an IF statement in the loop body) to abort.
exit
statement can be used to exit loops (not if
statements!) abruptly. It can be used in both do
and
do while
loops but its usage in do
loops may reduce
readability so use it with care.
print*,
or write(*,*)
for output and
read*,
or read(*,*)
for input.
format
statement to use. The format
statement must contain an edit
descriptor for each written or read value.
A
edit descriptor,
entered strings must be surrounded by single quotes.
open (file='filename', unit=n)
to associate the filename
with the unit number. You can either read or write to a file. Note
that if you open a file intending to write on it and it already exists,
it will be deleted and re-created; i.e. you'll loose its old data.
close(n)
to remove the association created by
open
rewind(n)
return the file pointer to the beginning of
the file. Useful for reading the fille more than once (faster than
closing and re-opening it).
OPEN
statement.
read(unit, format or *, IOstat = var) variable list
where var
is your own integer variable. The variable
becomes negative once end of file has been encountered (at which point
the reading loop must be exited).
ACCESS='DIRECT' RECL=xx
in open
) and specify
the record number (through rec=xx
in read / write
).
open
statement and read / write one character at a time without formatting (not
even a *).
open
is involved.
float
(integer to real) and mod
(remainder)
ABS
; EXP
and ALOG
;
SIN/COS/TAN
and ASIN/ACOS/ATAN/ATAN2
;
SQRT
realPart
, imagPart
, and carg
for
the real/imaginary parts and the argument in radians.
complex(a,b)
is used as a constructor to create
a+ib
.
LEN
, INDEX
, CHAR
, ICHAR
rand()
returns a pseodorandom sequence uniformly distributed in
[0,1). You can pass a seed to change the sequence. The
time()
function
returns a suitable seed (an integer*4 = #of milliseconds since Jan. 1, 1970).
Note that the ctime
function can format time()
into a readable date/time.
EXTERNAL
if it is user-defined
or as INTRINSIC
if it is built-in.
(*)
for the length of passed character strings and
a variable size for a passed array provided the size and the array are
both passed.
integer*2 list(-5:5)
(an array of 11 elements). For a 2-dimensional array,
the left index is for the row number.
! reads all the declared elements of array List read*, List ! prints selected elements using an implied DO LOOP print*, (i, List(i), i=1,20,2)