Вы находитесь на странице: 1из 18

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
75: real constant must have a decimal point
123.5-: the optional sign must precede the number
$12.34: cannot use dollar sign $

o Exponential Representation: It consists of an integer or a real number


in decimal representation (the mantissa or fractional part), followed by
the letter E or e, followed by an integer (the exponent).
Correct Examples
12.3456E2 or 12.3456e2: this is equal to 1234.56
-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
Incorrect Examples
12.34E1.2: the exponential part must be an integer constant
12.34-5: there is no exponential sign E or e
Complex: Will not be covered in this course
Logical: See Chapter 3

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
o Correct Examples:
'John' and "John": content = John and length = 4
' ' and " ": content = a single space and length = 1
'John Dow #2' and "John Dow #2": content = John Dow #2 and
length = 11

'' and "": content = nothing and length = 0 (empty string)


o Incorrect Examples:
'you and me: the closing apostrophe is missing
Hello, world': the opening apostrophe is missing
'Hi" and "Hi': the opening and closing quotes do not match.
If single quote is used in a string, then double quotes should be used to enclose
the string:
"Lori's apple"

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,

The remaining characters, if any, may be letters, digits, or underscores,


Fortran identifiers are case insensitive. That
is, Smith, smith, sMiTh, SMiTH, smitH are all identical identifiers.
Correct Examples:
o MTU, MI, John, Count
o I, X
o I1025, a1b2C3, X9900g
o R2_D2, R2D2_, A__
Incorrect Examples:
o M.T.U.: only letters, digits, and underscores can be used
o R2-D2: same as above
o 6feet: the first character must be a letter
o _System: same as above
Use meaningful names
o Good names: Total, Rate, length
o Not so good
names: ThisIsALongFORTRANname, X321, A_B_012cm, OPQ
Fortran has many keywords such
as INTEGER, REAL, PARAMETER, PROGRAM, END, IF, THEN, ELSE, DO,
just name a few; however, Fortran does not have any reserved words. More
precisely, a programmer can use these keywords as identifiers.
Therefore, END, PROGRAM, DO are perfectly legal Fortran identifiers. However,
this is definitely not a good practice.
Except for strings, Fortran 90 is not case sensitive. Therefore, identifier Name is
identical to name, nAmE, NAme, NamE and namE. Similarly, PROGRAM is

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

75: real constant must have a decimal point


123.5-: the optional sign must precede the number
$12.34: cannot use dollar sign $
o Exponential Representation: It consists of an integer or a real number
in decimal representation (the mantissa or fractional part), followed by
the letter E or e, followed by an integer (the exponent).
Correct Examples
12.3456E2 or 12.3456e2: this is equal to 1234.56
-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
Incorrect Examples
12.34E1.2: the exponential part must be an integer constant
12.34-5: there is no exponential sign E or e
Complex: Will not be covered in this course
Logical: See Chapter 3

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
o Correct Examples:

'John' and "John": content = John and length = 4


' ' and " ": content = a single space and length = 1
'John Dow #2' and "John Dow #2": content = John Dow #2 and
length = 11
'' and "": content = nothing and length = 0 (empty string)
o Incorrect Examples:
'you and me: the closing apostrophe is missing
Hello, world': the opening apostrophe is missing
'Hi" and "Hi': the opening and closing quotes do not match.
If single quote is used in a string, then double quotes should be used to enclose
the string:
"Lori's apple"

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.

ortran 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 are easy. The following are examples:
Variables ZIP, Mean and Total are of type INTEGER:

INTEGER :: ZIP, Mean, Total

Variables Average, error, sum and ZAP are of type REAL:

REAL

::

Average, error, sum, ZAP

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

CHARACTER(LEN=15) :: Name, Street

o FirstName, LastName and OtherName are character variables that can


hold a string of no more than 20 characters:
o

CHARACTER(LEN=20) :: FirstName, LastName, OtehrName

Use CHARACTER(i) to declare character variables of length i. That is, there is


no LEN= in the parenthesis. For examples,
o Name and Street are character variables that can hold a string of no
more than 15 characters:
o

CHARACTER(15) :: Name, Street

o FirstName, LastName and OtherName are character variables that can


hold a string of no more than 20 characters:
o

CHARACTER(20) :: FirstName, LastName, OtehrName

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.

CHARACTER(LEN=10) :: City, Nation*20, BOX, bug*1

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.

CHARACTER(LEN=*) :: Title, Position

Here, the actual lengths of variables Title and Position are unknown and will
be determined elsewhere.

Single Mode Arithmetic Expressions

An arithmetic expression is an expression using additions +,


subtractions -, multiplications *, divisions /, and exponentials **.
A single mode arithmetic expression is an expression all of whose
operands are of the same type (i.e. INTEGER, REAL or COMPLEX).
However, only INTEGER and REAL will be covered in this note.
Therefore, those values or variables in a single mode arithmetic
expression are all integers or real numbers.
In single mode arithmetic expressions, the result of an operation is identical to that of
the operands. The following is a table showing this fact. The empty entries will be
discussed in mixed mode arithmetic expressions.
Operator
INTEGER
REAL

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

8.4/4.2 is 2.0 rather than 2, since the result must be


of REAL type.
-5**2 is -25
12/4 is 3
13/4 is 3 rather than 3.25. Since 13/4 is a single mode
arithmetic expression and since all of its operands are
of INTEGER type, the result must also be ofINTEGER type.
The computer will truncate the mathematical result (3.25)
making it an integer. Therefore, the result is 3.
3/5 is 0 rather than 0.6.

Rules for Evaluating Expressions

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

In the above expression, in the left to right scan,


operator * is encountered first. Since the the operator - is
lower, 3 * 5 is evaluated first transforming the given
expression to 15 - 4. Hence, the result is 11.
o if the next one is equal to the current, the associativity
rules are used to determine which one should be
evaluated. For example, if both the current and the next
operators are *, then 3 * 8 * 6 will be evaluated as (3 *

8) * 6. On the other hand, if the operator is **, A ** B **


C will be evaluated as A ** (B ** C).
o if the next one is higher than the current, the scan should
continue with the next operator. For example, consider
the following expression:
o

4 + 5 * 7 ** 3

if the current operator is +, since the next operator * has


higher priority, the scan continues to *. Once the scan
arrives at *, since the next operator **is higher, 7 ** 3 is
evaluated first, transforming the given expression to
4 + 5 * 343

Then, the new expression is scan again. The next


operator to be evaluated is *, followed by +. Thus, the
original expression is evaluated as 4 + (5 * (7 ** 3)).

More Complicated Examples:

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

As in mathematics, subexpressions in parenthesis must be


evaluated first.

100 + (1 + 250 / 100) ** 3


--> 100 + (1 + [250 / 100]) ** 3
--> 100 + (1 + 2) ** 3

-->
-->
-->
-->
-->

100
100
100
100
127

+
+
+
+

([1 + 2]) ** 3
3 ** 3
[3 ** 3]
27

In the following example, x**0.25 is equivalent to computing


the fourth root of x. In general, taking the k-th root of x is
equivalent to x**(1.0/k) in Fortran, where k is a real number.

1.0 + 2.0 * 3.0 / ( 6.0*6.0 + 5.0*44.0) ** 0.25


--> 1.0 + [2.0 * 3.0] / (6.0*6.0 + 5.0*44.0) ** 0.25
--> 1.0 + 6.0 / (6.0*6.0 + 5.0*55.0) ** 0.25
--> 1.0 + 6.0 / ([6.0*6.0] + 5.0*44.0) ** 0.25
--> 1.0 + 6.0 / (36.0 + 5.0*44.0) ** 0.25
--> 1.0 + 6.0 / (36.0 + [5.0*44.0]) ** 0.25
--> 1.0 + 6.0 / (36.0 + 220.0) ** 0.25
--> 1.0 + 6.0 / ([36.0 + 220.0]) ** 0.25
--> 1.0 + 6.0 / 256.0 ** 0.25
--> 1.0 + 6.0 / [256.0 ** 0.25]
--> 1.0 + 6.0 / 4.0
--> 1.0 + [6.0 / 4.0]
--> 1.0 + 1.5
--> 2.5

Click here to continue with mixed mode arithmetic expressions.

Mixed Mode Arithmetic Expressions

If operands in an expression contains


both INTEGER and REAL constants or variables, this is a mixed
mode arithmetic expression.
In mixed mode arithmetic expressions, INTEGER operands are always converted
to REAL before carrying out any computations. As a result, the result of a mixed
mode expression is of REAL type. The following is a table showing this fact.
Operator
INTEGER

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:

In expression a**b where a is REAL, the result is undefined if


the value of a is negative. For example, -4.0**2 is defined
with -16.0 as its result, while (-4.0)**2 is undefined.

More Complicated Examples:

In the following, brackets will be used to indicated the order of


evaluation and braces will be used to indicated an integer-to-real
conversion.
Note that 6.0 ** 2 is not converted to 6.0 ** 2.0. Instead, it is
computed as 6.0 * 6.0.

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

In the following, 25.0 ** 1 is not converted, and 1 / 3 is zero.

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

Click here to continue with single mode arithmetic expressions.

IF-THEN-ELSE-END IF

The most general form of the IF-THEN-ELSE-END IF statement is the following:


IF (logical-expression) THEN
statements-1
ELSE
statements-2
END IF

where statements-1 and statements-2 are sequences of executable


statements, and logical-expression is a logical expression. The
execution of this IF-THEN-ELSE-END IF statement goes as follows:
the logical-expression is evaluated, yielding a logical value
if the result is .TRUE., the statements in statements-1 are
executed
if the result is .FALSE., the statements in statements-2 are
executed
after finish executing statements in statements1 or statements-2, the statement following END IF is executed.

Examples

The following code first reads in an integer


into INTEGER variable Number. Then, if Number can be
divided evenly by 2 (i.e., Number is a multiple of 2),
the WRITE(*,*) between IF and ELSE is executed and shows
that the number is even; otherwise,
the WRITE(*,*) between ELSE and END IF is executed and
shows that the number is odd. Function MOD(x,y) computes

the remainder of x divided by y. This is the the remainder (or


modulo) function

INTEGER :: Number
READ(*,*) Number
IF (MOD(Number, 2) == 0) THEN
WRITE(*,*) Number, ' is even'
ELSE
WRITE(*,*) Number, ' is odd'
END IF

The following program segment computes the absolute value


of X and saves the result into variable Absolute_X. Recall that
the absolute value of x is x if x is non-negative; otherwise, the
absolute value is -x. For example, the absolute value of 5 is 5
and the absolute value of -4 is 4=-(-4). Also note that
theWRITE(*,*) statement has been intentionally broken into
two lines with the continuation line symbol &.

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

The following program segment reads in two integer values


into a and b and finds the smaller one into Smaller. Note that
the WRITE(*,*) has also been broken into two lines.

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

You may find the following way of organizing IF-THEN-ELSE-END


IF very useful, especially when your program logic is reasonably
complex.
Draw a rectangular box and a vertical line dividing the box into two parts. Then, write
down the logical expression in the left part and draw a horizontal line dividing the
right parts into two smaller ones. The upper rectangle is filled with what you want to
do when the logical expression is .TRUE., while the lower rectangle is filled with
what you want to do when the logical expression is .FALSE.:
what you want to do when the logical expression is .TRUE.
logical-expression
what you want to do when the logical expression is .FALSE.

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.

Вам также может понравиться