Professional Documents
Culture Documents
77
Fortran 200x
Fixed Format
Fixed Format *.F *.FOR
Free FormatFortran 90
Free Format *.F90
Fixed Format
1 C c *
1-5
6 0
7-72
73
Free Format
Fortran 90 *.F90
!
132
&
1:! free format
2:
program free
3:
read (*,10) a,b
4:10 format (f5.1,f5.1)
5:
sum=a+b
6:
write (*,20) &
7:
sum
8:20 format (1x,f6.1)
9:
end
WRITE
write(*,*) "Hello"
"*"
write(UNIT=*,FMT=*) "Hello" !
write(6,*) "Hello"
!
write(UNIT=6,FMT=*) Hello !
FORMAT
PRINT
print *, "Hello"
print
READ
read (*,*) a
"*"
read(UNIT=*,FMT=*) "Hello" !
read(5,*) "Hello"
!
read(UNIT=5,FMT=*) "Hello" !
read (*,*) a, b
Happy Birthdaya="Happy""b="Birthday"
read (*,*) a
Happy Birthdaya="Happy"
FORMAT
a = 12.3456
write (*,*) a
write (*,100) a
100 format(f5.2)
12.345612.34
f5.151
1. 1write
write (*,"(1x,f5.2)") a
FORMAT
2. 2
(1x,f5.2)format (1xf5.2)
(a3,a3)(a3a3)format (2a3)
(1xf5.2 , 1xf5.2 , 1xf5.2)format (3(1xf5.2))
3.
write (*,"(a4,I1)") "1+2=",1+2
write (*,"('1+2=',I1)") 1+2
1+2=3 F77
write (*,'(''1+2='',I1)') 1+2
4.
character(len=10) fmtstring
fmtstring = "(f5.1)"
write (*,fmtstring) 123.4
5fmtstring(3:3)=8
(f8.1)
5.
123A3
1X
6. formatformat
writewriteformat
writeformat
integer a
"a"
1. program example
2. integer a, b
3. a = 3
! a3
4. b = a ** 2
! ba**23**2
5. print *, "a=", a ! a
6. print *, "b=", b ! b
7. stop
8. end example
a=
b=
(INTEGERAL)
integer a
1.
a=3/2 a=1 a=1/2 a=0
2. - 44 bytes, 32 bits
integer(kind=4) a ! F90
integer(4) b !
INTEGER*4
c ! F77
-2,147,483,648 ~ +2,147,483,647
3. - 22 bytes, 16 bits
integer(kind=2) a ! F90
integer(2) b !
INTEGER*2
c ! F77
-32,768 ~ +32,767
4. - 11 bytes, 8 bits
integer(kind=1) a ! F90
integer(1) b !
INTEGER*1
c ! F77
-128 ~ +127
(REAL)
real a
kind(kind=4)
1. - 44 bytes, 32 bits
real(kind=4) a ! F90
real(4)
REAL*4
b !
c ! F77
(PC)1.18*10-38 ~ 3.40*1038
6-7
a = 1000000. + 0.1 a = 1000000.
2. - 48 bytes, 64 bits
real(kind=8) a ! F90
real(8) b !
REAL*8
c ! F77
(PC)2.23*10-308 ~ 1.79*10308
15
3.
".0"3 3.0"."
real a
a= 1.5+3./2. a= 3.0 3.2.
a= 1.5+3/2 a= 2.5 3/2
(REAL*8)"d0"3 3.d0
a = 1.0_4 kind = 4
a = 1.0_8 kind = 8
(COMPLEX)
complex a
a=x+yix y
1.
a = (x,y) ! a = x + yi x=1.5y=2.5a=1.5 + 2.5i
a = (1.5,2.5) ! a = 1.5 + 2.5i
a = 1.5
! a = 1.5 + 0i
2. -
complex(kind=4) a ! F90
complex(4)
b !
COMPLEX*8
c ! F77
3. -
complex(kind=8) a ! F90
complex(8)
b !
COMPLEX*16
c ! F77
(CHARACTER)
character a
1.
character(len=10) a ! F90
character(10) b !
CHARACTER*10
c ! F77
CHARACTER*(10) d !
2.
a = "Hello"
! F90
b = 'Hello'
! F77
c = "That's right" !
d = Thats right !
e = Hello !
"Hello"
string(1:4) = "Good" ! 1-4
string(5:5) = " "
! 5
string(6:) = "morning" ! 6
// string_a = string_b // string_c
(LOGICAL)
logical a
1.
a = .true. !
a = .false. !
WRITE T F
2. 1(1 bit)
44 bytes, 32 bits
logical(kind=4) a ! F90
LOGICAL*4
b ! F77
LOGICAL(4)
c ! F77
242
(TYPE) - F90
Visual Basic
! person
type :: person
character(len=25) :: name !
integer(kind=2) :: length !
integer(kind=2) :: weight !
character(len=2) :: blood !
end type person
! person
type(person) :: a ! person
read(*,*) a%name ! a
write(*,*) a%name ! a
a = person("Frank",172,75,"O") !
KIND - F90
INTEGERREALKINDPC
integer(kind=1)-128 ~ +127
integer(kind=2)-32,768 ~ +32,767
integer(kind=4)-2,147,483,648 ~ +2,147,483,647
kind
SELECTED_INT_KIND(n)
n kind
-1
SELECTED_REAL_KIND(n,e)
n e kind
-1
-2
-3
a-z_0-9
F771-6F901-31
PRINT
i,j,k,l,m,n
IMPLICIT
IMPLICIT NONE
i j
parameter
equivalence
DATA
programimplicitDATA
IMPLICIT
i,j,k,l,m,n
IMPLICIT
1.
implicit integer(A,B,C) ! A,B,C
! L-P
!
2. IMPLICIT IMPLICIT
program
3. IMPLICIT NONE
i,j,k,l,m,n
4. IMPLICIT NONE
IMPLICIT
.DATA
real pi
pi = 3.14159
DATA/.../
real a, b
complex c
character(10) string
data a, b, c, string /1.0, 2.0, (3.0,4.0), 'Hello'/
a=1.0 b=2.0 c=(3.0,4.0) string = 'Hello'
real a(4)
data a /1.0 , 2.0 , 3.0 , 4.0/
F90
real :: pi = 3.14159
integer :: count = 0
2816
28
a = B"11100" ! B (Binary) - 0,1
a = O"34"
! O (Octal) - 0,1,2,3,4,5,6,7
a = Z"1C"
! Z - 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E
PARAMETER
PARAMETER (pi=3.14159)
pi
3. F90
real, parameter :: pi=3.14159 ! pi
EQUIVALENCE
integer a,b
equivalence (a,b)
a,b
1.
temp
equivalence
2.
equivalence
equivalence (array(2,3,4), a)
array(2,3,4) a
array(2,3,4)
a
Logical Expression
LOGICAL Type
The <condition>s are logical expressions, which can only have values .TRUE.
and .FALSE.
Logical expressions
F90
.EQ.
==
equal to
.NE.
/=
not equal to
.LT.
<
less than
.GT.
>
greater than
.LE.
<=
.GE.
>=
Logical operators
.NOT. A
A .AND. B
A .OR. B
A .EQV. B
A .NEQV. B
True
True
False
True
True
True
False
True
False
False
False
True
False
True
False
True
True
False
True
False
True
False
False
True
False
False
True
False
Example:
a = (3.**0.5)** 2
b = 3.
-> b - a 0.0 (b-a .EQ. 0.0) is .FALSE.
b - a 0.0 a
(3.**0.5) 3. 3.
(b-a .EQ. 0) .FALSE.
a a = 0.0
if (abs(b-a) .LE. 0.000001) then ...
if (abs(b-a)/b .LE. 0.000001) then ...
abs
a = (4.**0.5)** 4 - 3. (4.**0.5) 2.
b - a 0.0
ASCII
'a' < 'b' ascii(a)=97ascii(b)=98
'A' < 'a' ascii(A)=65ascii(a)=97
! asciiichar
SELECT CASE
F90F77
SELECT CASE (variable)
CASE (value-1)
...
... (statement group 1)
...
CASE (value-2)
...
... (statement group 2)
...
CASE (value-3)
...
... (statement group 3)
...
CASE DEFAULT
...
... (all false statement group)
...
END SELECT
case default
CASE(value) (value)(value1:value2) 1:5 1
5
IF-ELSE IF
CASE
GOTO
F77
Example 1: (IF-ELSE)
if (a .GE. 60) then goto 100
print *, "pass!"
goto 200
100 print *, "
200 stop
Example 2: (LOOP)
sum = 0
count = 1
100 if (count .GT. 10) then goto 200
sum = sum + count
count = count + 1
goto 100
200 print *, sum
stop
PAUSE.CONTINUE.STOP
PAUSE
ENTER
CONTINUE
...
F77GOTO
STOP
IF
GOTO
Fortran 77 GOTO DO
GOTO
... statement preceding loop ...
<label> CONTINUE
... loop body ...
GOTO <label>
... statement following loop ...
Indexed Looping
<index-variable> = <initial-value>
* .... top of indexed loop ....
100
CONTINUE
IF (<index-variable> .GT. <final-value> GOTO 199
...
... loop statement
...
<index-variable> = <initial-value> + <increment>
GOTO 100
* .... bottom of indexed loop ....
199 CONTINUE
Fortran 77
DO <label> <index-var> = <initial-val>,<final-val>[,<increment>]
...
... loop statement
...
<label> CONTINUE
1. DO<index-var> = <initial-val>
2. loop statement<index-var><increment>
<index-var>
DO 100 count = 1,10 ! 100
i=i+1
!
100 CONTINUE
3. <index-var> <final-val>
<index-var> <final-val>
4. <initial-val>,<final-val>,<increment>
program test
integer start , end , inc , i
start = 1
end = 10
inc = 1
do i = start, end, inc ! enddoF9077
start = 3
end = end - 1
inc = -2
print *, i , end ! i 1 10
enddo
print *, i
end
ans = 0
! 0
do count = 2,12,2
ans = ans + count
end do
! count = 14
Nested Loops A loop body may itself contain a loop, and such nesting may be
arbitrarily deep.
do i = 1, 5
do j = 1, 3
print *, a(i,j) ! 15 print 5*3
end do
end do
DO WHILE
CYCLE, EXIT
EX:
do while (.true.)
print *, 'Speed = ? (must > 99)'
read *, speed
if (speed .LT. 100) cycle ! speed100
...
end do
EXIT
EX:0 EXIT
do i = 1, 1000
sum = sum + a(i)
if (a(i) .EQ. 0) exit !
enddo
end docycleexit
<cycle_name> : do ...
end docycleexit <cycle_name>
EX:
outer: do i = 1, 10
! outer
inner: do j = 1, 10
! inner
...
if (i = 3) exit outer ! i=3 i=4
if (j = 5) cycle inner ! j=5
...
enddo inner
enddo outer
integer a(10)
!
integer, dimension(10) :: a ! F90
INTEGER a
! F77
DIMENSION a(10)
! a
<array name>(<sequence number>)
integer a(10)
read *, a(2) ! a
print *, a(2) ! a
real a(10)
a(n) a n
a0,a1,a2,a3,...,a9 a(10)
a(1) = a0
a(2) = a1
a(0:10)
a(0) = a0
a(1) = a1
a(0) a 1 a(n)
a n+1
<data type> a(i:j) a(i+n) n+1
1
2
3
...
80
90
75
...
85
85
80
75
80
90
80
90
85
C(1,2)
C(2,2)
C(3,2)
C(1,3)
C(2,3)
C(3,3)
C(1,4)
C(2,4)
C(3,4)
1
2
3
...
C(1,1)
C(2,1)
C(3,1)
...
2 C(2,3)
integer a(10,10)
!
integer, dimension(10,10) :: a ! F90
INTEGER a
! F77
DIMENSION a(10,10)
! a
integer a(3,4,5)
read *, a(2,2,3)
print *, a(2,2,3)
A:
A(1)
A(2)
A(3)
A(4)
A(5)
B:
B(0)
B(1)
B(2)
B(3)
B(4)
C:
C(1,1)
C(1,2)
C(1,3)
C(1,4)
C(2,1)
C(2,2)
C(2,3)
C(2,4)
C(3,1)
C(3,2)
C(3,3)
C(3,4)
equivalence C,E
C:
E:
C(1,1) C(2,1) C(3,1) C(1,2) C(2,2) C(3,2) C(1,3) C(2,3) C(3,3) C(1,4) C(2,4) C(3,4)
E(1)
E(2)
E(3)
E(4)
E(5)
E(6)
E(7)
E(8)
E(9)
E(10)
E(11)
E(12)
do i = 1, 5
sum = sum + a(1,i)
enddo
do i = 1, 5
sum = sum + a(i,1)
enddo
.DATA.
DATA
integer A(5)
data A/1,2,3,4,5/ ! A(1)=1,A(2)=2,A(3)=3,A(4)=4,A(5)=5
data A/5*3/
! /5*3/53/3,3,3,3,3/
! A(1)=3,A(2)=3,A(3)=3,A(4)=3,A(5)=3
data (A(i),i=2,4) /2,3,4/ ! A(i), i=2,3,4
! A(2)=2,A(3)=3,A(4)=4, A(1)A(5)
integer B(2,3)
data ( (B(i,j),i=1,2) , j=1,3 ) /1,2,3,4,5,6/
! B(1,1)=1, B(2,1)=2, B(1,2)=3, B(2,2)=4, B(1,3)=5 ,B(2,3)=6
Fortran 90(DATA)
integer :: a(5) = (/1,2,3,4,5/) ! 5
! A(1)=1,A(2)=2,A(3)=3,A(4)=4,A(5)=5
integer :: a(5) = (/1,(2,i=2,4),5/) !
! A(1)=1,A(2)=2,A(3)=2,A(4)=2,A(5)=5
integer :: a(5) = (/(i,i=1,5)/) ! A(i)=i, i=1~5
ALLOCATABLE - Fortran 90
Fortran 90ALLOCATABLE
integer, allocatable :: student(:) !
integer :: stu_count
print *,":"
read *, stu_count
allocate( student(stu_count) ) ! stu_count
do i = 1, stu_count
print *, "",i,""
read *, student(i)
end do
...
allocatable allocate allocatable
allocate
deallocate
deallocate( student )
allocated
ALLOCATED
Subroutine()Function()
(
CALL)
Subroutine
! swap
! DO
program Swap_2_Real
do while (.true)
read *, value1, value2
!
if (value1 .eq. value2) stop !
call swap(value1, value2)
! swap
print *, value1, value2
!
end do
stop
end
! - swap
subroutine swap(a, b)
real a, b
real temp
temp = a
! a
a=b
! b a
b = temp
! a b
return
!
end
programsubroutine
returnreturnstop
Fortran 90
Function
! x x2 - 2x + 1
program func_test
real x, y
do while (.ture.)
!
read *, x
! x
y = f(x)
! f x2 - 2x + 1
print *, y
! y = f(x)print *, y print *,f(x)
end do
end
-----------------------------------------real function f(x) ! f real
real x
f = x**2 2*x + 1
return
end
Calling programs (main program, subroutines)
<function name>[([<actual argument list>])]
-------------------------------------------------Called programs (functions)
[<type>] FUNCTION <func. name>[([<dummy argument list>])]
END [FUNCTION [<func. name>]] (Fortran 77END)
-------------------------------------------------Passed by address: if an actual argument is a variable name, array name,
array element or a substring
Passed by value: if the actual argument is any other expression (including a
constant)
real function f(x)
function f(x)
real f, x
EXTERNAL
CALL []
print *, f(x)
a = f(1.0) + f(2.0)
b = f( f(1.0) )
function
subroutine
Statement Function
[<type>] <function name>
<function name>([<dummy argument list>])=<expression>
real x, f
f(x) = x**2 2*x + 1
f(x)
COMMON
program common_ex
integer a, b
common a, b
a = 99
b = 100
call ShowCommon()
end
subroutine ShowCommon()
integer num1, num2
common num1, num2
print *, num1, num2
return
end
99,100
a b ShowCommon num1
num2 num1 a num2
b
common a,b
integer a,b
common num
integer num(2) num(1) a num(2) b
program_maincommon a,b,c,d,e,f,g,h,i,j
subroutine_1common p,q,r,s,t
subroutine_2common p,q,r,s,t,u,v,w,x,y
subroutine_2i,j
i.j
common /group1/ a, b
common /group2/ c, d
-1 common /group1/ num1. num2 ! a, b
-2 common /group2/ num1. num2 ! c, d
COMMON DATA
common a
real a
a = 1.0
common num
integer num num a
a num
print *, num 1065353216
a = 1.0
00111111100000000000000000000000 num 2
1065353216
num2
BLOCK DATA
program common_ex
integer a, b, c, d
common a, b
common /group1/ c, d group1
print *, a, b, c, d
end
block data
implicit none
integer a, b
common a, b
data a, b /99, 100/
integer c, d
common /group1/ c, d
data c, d /101, 102/
end data block
99 100 101 102
BLOCK DATA
PARAMETER
function test(a,b,c)
character*(*) a ! F77
character(*) b ! F90
character(len=*) c ! F90
...
end function
(sequential files)
(direct-access files)
OPEN
OPEN ( open-list )
open-list
FILE =
FILE = FILE = char-exp char-exp ()
STATUS =
STATUS = STATUS = char-exp char-exp ()
: OLD FILE = NEW
FILE = SCRATCH UNKNOWN
STATUS =
IOSTAT =
FILE = IOSTAT = int-var int-var
ERR =
ERR = ERR = n n
ACCESS =
ACCESS = ACCESS = char-exp char-exp (
) : SEQUENTIAL DIRECT
FORM =
FORM = FORM = char-exp char-exp ()
: FORMATTED UNFORMATTED
RECL =
RECL = RECL = int-exp int-exp
(processor)
BLANK =
BLANK = BLANK = char-exp char-exp ()
: ZERO 0 NULL
ZERO NULL 0
CLOSE CLOSE
CLOSE ( close-list ) close-list
IOSTAT =
ERR =
STATUS =
STATUS = STATUS = char-exp char-exp ()
: KEEP DELETE
SCRATCH KEEP STATUS =
CLOSE OPEN
()