FORTRAN 90 L e c

advertisement
FORTRAN 90
Lecture : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc
Mechanical Engineering Dep.
1.DATA TYPE
1.1 CONSTANT
Fortran has six types of constants: integer, real, complex, logical, double
precision, and character string.
Integer Constants: a string of digits with an optional sign:
Examples: 0, -345, 768, +12345
Real Constants: There are two representations, decimal representation
and exponential representation.
o Decimal Representation: A decimal point must be presented, but
no commas are allowed. A real constant can have an optional sign.
Examples: 23.45, .123, 123., -0.12, -.12
o
Exponential Representation: It consists of an integer or a real
number in decimal representation .
Examples:
12.3458E2 or 12.3458e2: this is equal to 1234.58
-3.14E1 or -3.14e1: this is equal to -31.4
-1.2E-3 or -1.2e-3: this is equal to -0.0012
12E3 or 12e3: this is equal to 12000.0
0E0 or 0e0: this is equal to 0.0
ϭ
Complex : Two real number stored as a pair and treated as the real and
imaginary parts of a complex number.
Examples:
(1.234,-6.5E-3)
Where in this example, 1.234 is the real part of the complex constant and
-0.0065 is the imaginary component.
Examples:
complex:: x,y,i
x=(1,1)
y=(1,-1)
i=(0,-1)
print*,conjg (x),i*x*y
end
Logical : Logical variables have a value of either .true. or .false.
Character String: Character constants must be enclosed between double
quotes or apostrophes (single quotes). The content of a string consists of
all characters, spaces included, between the single or quote quotes, while
the length of the string is the number of characters of its content. The
content of a string can be zero and in this case it is an empty string
Examples:
'Ali' and "Ali": content = Ali and length = 3
' ' and " ": content = a single space and length = 1
'ahmed #2' and "ahmed #2": content = ahmed #2 and length = 8
'' and "": content = nothing and length = 0 (empty string)
Double precision : are similar to real number but are allocated twice as
much storage space. and can lie in approximate range 10-307 to 10308.
Ϯ
2. Fortran Variables and Their Types
A Fortran variable can be considered as a box that is capable of holding a
single value of certain type. Thus, a variable has a name, the variable name and
a type. The type of a variable can be one of the following:




INTEGER: the variable is capable of holding an integer
REAL: the variable is capable of holding a real number
COMPLEX: the variable is capable of holding a complex number
LOGICAL: the variable is capable of holding a logical value (i.e., true or
false)
 CHARACTER: the variable is capable of holding a character string of
certain length
2.1 Fortran Variable Declarations
Declaring the type of a Fortran variable is done with type statements. It
has the following form:
type-specifier :: list
where the type-specifier is one of the following and list is a list of variable
names separated with commas:





INTEGER : the variables in list can hold integers
REAL: the variables in list can hold real numbers
COMPLEX: the variables in list can hold complex numbers
LOGICAL: the variables in list can hold logical values (i.e., true or false)
CHARACTER: the variables in list can hold character strings
Types INTEGER and REAL .
Examples:

Variables ZIP, Mean and Total are of type INTEGER:
INTEGER :: ZIP, Mean, Total
INTEGER :: a = 828, b = 59

Variables Average, error, sum and ARAE are of type REAL:
ϯ
REAL :: Average, error, sum, AREA
REAL :: a = 2.61828, b = 3.14159
Type CHARACTER is more involved. Since a string has a length attribute, a
length value must be attached to character variable declarations. There are two
ways to do this:
 Use CHARACTER(LEN=i) to declare character variables of length i. For
examples,
Name and Street are character variables that can hold a string of no
more than 15 characters:
CHARACTER(LEN=15) :: Name, Street
 Use CHARACTER(i) to declare character variables of length i. That is,
there is no LEN= in the parenthesis. For examples,
Name and Street are character variables that can hold a string of no
more than 15 characters:
CHARACTER(15) :: Name, Street
 If a variable can only hold a single character, the length part can be
removed. The following three declarations are all equivalent:
CHARACTER(LEN=1) :: letter, digit
CHARACTER(1) :: letter, digit
CHARACTER
:: letter, digit
Here, variables letter and digit can only hold no more than one character.

If the length value is replaced with a asterisk *, it means the lengths of
the declared variables are determined elsewhere.
CHARACTER(LEN=*) :: Title, Position
Here, the actual lengths of variables Title and Position are unknown and
will be determined elsewhere.
ϰ
3. PARAMETERS
It refers to a value which will not change during a program’s execution.
REAL, PARAMETER :: pi=3.141592
INTEGER, PARAMETER :: maxvalue=1024
CHARACTER(LEN=4), PARAMETER :: Name = 'Smith'
It is an error to try to redefine the value of a parameters while a program
executes.
4. Implicit Declaration
Fortran 90 permits real and integer variables to be typed and declared
implicitly, that is used in a program without using a declaration statement.
It is possible and advisable, to disable this feature by including the statement:
IMPLICIT NONE at the start of each program. This forces a programmer to
declare all variables that are used, and means that some potential errors may be
identified during compilation.
If implicit typing is permitted then variables are have a data type according to
the initial letter of their name: those beginning with I, J, K, L, M and N being
integers; and those beginning A to H and O to Z being real.
PROGRAM free_source_form
IMPLICIT NONE
REAL :: tx, ty, tz
tx = 1.0; ty = 2.0;
tz = tx * ty
! Continuation symbol on line to be continued
PRINT *, &
tx, ty, tz
END PROGRAM free_source_form
ϱ
Download