Академический Документы
Профессиональный Документы
Культура Документы
Constants or more formally literal constants are the tokens used to denote the value
of a particular type. Fortran has five types of constants: integer, real, complex, logical,
and character string.
Integer Constants: a string of digits with an optional sign:
o Correct Examples: 0, -345, 768, +12345
o Incorrect Examples:
1,234 : comma is not allowed
12.0: no decimal point
--4 and ++3: too many optional signs
5- and 7+: the optional sign must precede the string of digits
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.
Correct Examples: 23.45, .123, 123., -0.12, -.12
Incorrect Examples:
12,345.95: no comma is allowed
75: real constant must have a decimal point
123.5-: the optional sign must precede the number
$12.34: cannot use dollar sign $
This string has content Lori's apple and length 12. Alternatively, you can write
the single quote twice as follows:
'Lori''s apple'
The compiler will treat a pair of single quotes in the content of a string as one.
Thus, the content of the above string is still Lori's apple.
o Correct Examples:
'What''s this?': content = What's this? and length = 11
'''''': content = '' and length = 2
o Incorrect Examples:
'Tech's seminar': the single quote between h and s should be
written twice.
Fortran Identifiers
A Fortran identifier must satisfy the following rules:
It has no more than 31 characters
The first character must be a letter,
identical to program, PROgram, and progRAM. In this course, all keywords such
as PROGRAM, READ, WRITE and END are in upper case and other identifiers use
mixed cases.
Fortran Constants
Constants or more formally literal constants are the tokens used to denote the value
of a particular type. Fortran has five types of constants: integer, real, complex, logical,
and character string.
Integer Constants: a string of digits with an optional sign:
o Correct Examples: 0, -345, 768, +12345
o Incorrect Examples:
1,234 : comma is not allowed
12.0: no decimal point
--4 and ++3: too many optional signs
5- and 7+: the optional sign must precede the string of digits
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.
Correct Examples: 23.45, .123, 123., -0.12, -.12
Incorrect Examples:
12,345.95: no comma is allowed
This string has content Lori's apple and length 12. Alternatively, you can write
the single quote twice as follows:
'Lori''s apple'
The compiler will treat a pair of single quotes in the content of a string as one.
Thus, the content of the above string is still Lori's apple.
o Correct Examples:
'What''s this?': content = What's this? and length = 11
'''''': content = '' and length = 2
o Incorrect Examples:
'Tech's seminar': the single quote between h and s should be
written twice.
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 are easy. The following are examples:
Variables ZIP, Mean and Total are of type INTEGER:
REAL
::
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,
o Name and Street are character variables that can hold a string of no
more than 15 characters:
o
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)
CHARACTER(1)
CHARACTER
:: letter, digit
:: letter, digit
:: letter, digit
Here, variables letter and digit can only hold no more than one character.
If you want to declare character variables of different length with a single
statement, you can attach a length specification, *i, to the right of a variable. In
this case, the corresponding variable will have the indicated length and all other
variables are not affected.
Here, variables City and BOX can hold a string of no more than 10
characters, Nation can hold a string of no more than 20 characters, and bug can
hold only one character.
There is one more way of specifying the length of a character variable. If the
length value is replaced with a asterisk *, it means the lengths of the declared
variables are determined elsewhere. In general, this type of declarations is used
in subprogram arguments or in PARAMETER and is refereed to asassumed
length specifier.
Here, the actual lengths of variables Title and Position are unknown and will
be determined elsewhere.
Simple Examples:
1 + 3 is 4
1.23 - 0.45 is 0.78
3 * 8 is 24
6.5/1.25 is 5.2
INTEGER
REAL
INTEGER
mixed mode
mixed mode
REAL
The following are rules of evaluating a more complicated single mode arithmetic
expression:
Expressions are always evaluated from left to right
If an operator is encountered in the process of evaluation,
its priority is compared with that of the next one:
o if the next one is lower, evaluate the current operator
with its operands
o
3 * 5 - 4
4 + 5 * 7 ** 3
In the following examples, brackets are used to indicated the order of evaluation.
The result is 4 rather than 4.444444 since the operands are all
integers.
2 * 4 * 5 / 3 ** 2
--> [2 * 4] * 5 / 3 ** 2
--> 8 * 5 / 3 ** 2
--> [8 * 5] / 3 ** 2
--> 40 / 3 ** 2
--> 40 / [3 ** 2]
--> 40 / 9
--> 4
-->
-->
-->
-->
-->
100
100
100
100
127
+
+
+
+
([1 + 2]) ** 3
3 ** 3
[3 ** 3]
27
INTEGER
REAL
INTEGER
REAL
REAL
REAL
REAL
The rules for evaluating mixed mode arithmetic expressions are simple:
Use the rules for evaluating single mode arithmetic
expressions for scanning.
After locating an operator for evaluation, do the following:
o if the operands of this operator are of the same type,
compute the result of this operator.
o otherwise, one of the operand is an integer while the
other is a real number. In this case, convert the integer to
a real (i.e., adding .0 at the end of the integer operand)
and compute the result. Note that since both operands
are real numbers, the result is a real number.
There is an exception, though. In a**n, where a is a real
and n is a positive integer, the result is computed by
multiplying n copies of a. For example,3.5**3 is computed
as 3.5*3.5*3.5
Simple Examples:
1 + 2.5 is 3.5
1/2.0 is 0.5
2.0/8 is 0.25
-3**2.0 is -9.0
4.0**(1/2) is first converted to 4.0**0 since 1/2 is a single
mode expression whose result is 0. Then, 4.0**0 is 1.0
An Important Note:
5 * (11.0 - 5) ** 2 / 4 + 9
--> 5 * (11.0 - {5}) ** 2 / 4 + 9
--> 5 * (11.0 - 5.0) ** 2 / 4 + 9
--> 5 * ([11.0 - 5.0]) ** 2 / 4 + 9
--> 5 * 6.0 ** 2 / 4 + 9
--> 5 * [6.0 ** 2] / 4 + 9
--> 5 * 36.0 / 4 + 9
--> {5} * 36.0 / 4 + 9
--> 5.0 * 36.0 / 4 + 9
--> [5.0 * 36.0] / 4 + 9
--> 180.0 / 4 + 9
--> 180.0 / {4} + 9
--> 180.0 / 4.0 + 9
--> [180.0 / 4.0] + 9
--> 45.0 + 9
--> 45.0 + {9}
--> 45.0 + 9.0
--> 54.0
25.0 ** 1 / 2 * 3.5 ** (1 / 3)
--> [25.0 ** 1] / 2 * 3.5 ** (1 / 3)
--> 25.0 / 2 * 3.5 ** (1 / 3)
--> 25.0 / {2} * 3.5 ** (1 / 3)
--> 25.0 / 2.0 * 3.5 ** (1 / 3)
--> 12.5 * 3.5 ** (1 / 3)
--> 12.5 * 3.5 ** ([1 / 3])
--> 12.5 * 3.5 ** 0
--> 12.5 * [3.5 ** 0]
--> 12.5 * 1.0
--> 12.5
IF-THEN-ELSE-END IF
Examples
INTEGER :: Number
READ(*,*) Number
IF (MOD(Number, 2) == 0) THEN
WRITE(*,*) Number, ' is even'
ELSE
WRITE(*,*) Number, ' is odd'
END IF
REAL
:: X, Absolute_X
X = .....
IF (X >= 0.0) THEN
Absolute_X = X
ELSE
Absolute_X = -X
END IF
WRITE(*,*) 'The absolute value of ', x, &
' is ', Absolute_X
INTEGER
:: a, b, Smaller
READ(*,*) a, b
IF (a <= b) THEN
Smaller = a
ELSE
Smaller = b
END IF
Write(*,*) 'The smaller of ', a, ' and ', &
b, ' is ', Smaller
A Useful Tip
For example, the third example above has the following description:
a is the smaller number
a <= b
b is the smaller number
Although this is an easy example, you will sense its power when you will be dealing
with more complex problems.