The basic arithmetic operations of addition, subtraction, multiplication,
division, and exponentiation (raising to a power) are all possible in
FORTRAN. Addition and subtraction in FORTRAN use the same familiar
symbols + and -. However, multiplication (which
is denoted in a variety of ways in mathematics) is represented in FORTRAN
by an asterisk * and division by a forward slash
/. A double asterisk ** is employed to raise a
base to a power.
| Priority | Operation | Symbol | FORTRAN Expression | Arithmetic Expression |
|---|---|---|---|---|
| inside to outside | Parentheses | ( ) | ||
| right to left | Exponentiation | ** | A**B | ab |
| left to right | Multiplication and Division | * / |
A*B A/B |
a×b a÷b |
| left to right | Addition, Subtraction, and Unary Minus | + - |
A+B A-B -A |
a+b a-b -a |
Parentheses have the highest priority and can be used to force lower priority calculations to occur before higher priority ones.
The arithmetic expression -an+b×c-d÷e is
written in FORTRAN as -A**N+B*C-D/E and is evaluated in the
following order:
A**NB*CD/E- in front of A**N+ between -A**N and B*C- between B*C and D/E
The arithmetic expression a+(bm)n/(c-d)
is written in FORTRAN as A+((B**M)**N)/(C-D) and is
evaluated in the following order:
B**MB**M to the N powerC-D/ between (B**M)**N and C-D+ between A and the rest of the expressionHere parentheses are used to override the default priorities.
Use as many parentheses as necessary to make the expression clear. Note that only parentheses are used in FORTRAN. Neither brackets [ ] nor braces { } are used.
Two CHARACTER strings can be joined together in a process
called concatenation. The concatenation operator is a double
forward slash //.
The two strings 'FORT' and 'RAN' can be
combined as 'FORT'//'RAN' to give 'FORTRAN'.
The concatenation operator can be used on CHARACTER constants
(as in the above example) or on CHARACTER variables. Any
number of strings can be combined into one string using this operator.
A substring is any string that is a subset of the original string
and maintains the order of the original. The notation variable(a:b)
indicates a substring of the CHARACTER variable
variable starting at the ath character and
ending at the bth character. In order for a substring
to make sense, a must be greater than or equal to 1, b
must be greater than or equal to a, and b must be less
than or equal to the length of the original string.
Suppose the CHARACTER variable BEST has length
7 and has been assigned the value FORTRAN. Then
BEST(6:6) gives the value A and
BEST(1:4) gives FORT.
Three operations can be performed on LOGICAL variables:
negation, and, and or. To negate a
LOGICAL expression, precede it with .NOT.
When two LOGICAL expressions are combined with an
.AND., then the result is .TRUE. only if both
parts are .TRUE. Two LOGICAL expressions
combined with an .OR. yield .TRUE. unless both
parts are .FALSE. The truth table below sums this up:
| A | B | .NOT. A | A .AND. B | A .OR. B |
|---|---|---|---|---|
| .TRUE. | .TRUE. | .FALSE. | .TRUE. | .TRUE. |
| .TRUE. | .FALSE. | .FALSE. | .FALSE. | .TRUE. |
| .FALSE. | .TRUE. | .TRUE. | .FALSE. | .TRUE. |
| .FALSE. | .FALSE. | .TRUE. | .FALSE. | .FALSE. |
.NOT. has the highest precedence, followed by
.AND. and then .OR. A LOGICAL
expression may contain several logical operators, and parentheses may
be used to override default precedences.
The FORTRAN expression
A .OR. .NOT. (B .OR. C) .AND. D
where all of the variables are of type LOGICAL is evaluated
in the following order:
B .OR. C.NOT. in front of B .OR. C.AND. between .NOT. (B .OR. C) and
D.OR. between A and the rest of the
expression.If the parentheses were absent, then the evaluation order of
A .OR. .NOT. B .OR. C .AND. D
would be
.NOT. BC .AND. D.OR. between A and .NOT. B.OR. between .NOT. B and
C .AND. D
Note that it is possible to have two LOGICAL operators
next to each other provided the first is an .AND. or an
.OR. and the second is .NOT.
Relational operators compare two expressions of a similar type and
evaluate to either .TRUE. or .FALSE.
| Comparing Numbers | Comparing Booleans | ||
|---|---|---|---|
| Operator | Meaning | Operator | Meaning |
| .EQ. | equal to | .EQV. | equivalent to |
| .NE. | not equal to | .NEQV. | not equivalent to |
| .LT. | less than | ||
| .LE. | less than or equal to | ||
| .GT. | greater than | ||
| .GE. | greater than or equal to | ||
Every character in a program is represented internally as a binary
number. There are a number of different codes for translating characters
into binary strings but the two most commonly used are EBCDIC
(Extended Binary Coded Decimal Interchange Code) and ASCII
(American Standard Code for Information Interchange). Because every code
has a different collating sequence, it is impossible to use the
relational operators to try to compare two CHARACTER
strings alphabetically. However, FORTRAN provides a number of intrinsic
functions that permit alphabetical comparisons of two strings based on the
ASCII code, regardless of which code the computer actually uses.
A LOGICAL expression is defined when two numbers are compared
using one of the relational operators. These LOGICAL
expressions can be combined into a compound LOGICAL expression
by using the LOGICAL operators defined above.
To test whether a value a is less than 2.0, or greater than or equal to 5.0, write
A .LT. 2.0 .OR. A .GE. 5.0
The relational operators .LT. and .GE. have a
higher priority than the LOGICAL operator .OR.
However, parentheses such as these
(A .LT. 2.0) .OR. (A .GE. 5.0)
might make the logic clearer.
To test whether either a or b are negative REAL
numbers, you cannot write
A .OR. B .LT. 0.0
because the LOGICAL operator .OR. can only be used
between two LOGICAL expressions. In this instance,
B .LT. 0.0
returns a LOGICAL expression which leaves the comparison
numerical expression .OR. LOGICAL expression
Instead, write
A .LT. 0.0 .OR. B .LT. 0.0
The .NEQV. relational operator is also known as
exclusive OR. Note how it differs from .OR.
| A | B | A .EQV. B | A .NEQV. B |
|---|---|---|---|
| .TRUE. | .TRUE. | .TRUE. | .FALSE. |
| .TRUE. | .FALSE. | .FALSE. | .TRUE. |
| .FALSE. | .TRUE. | .FALSE. | .TRUE. |
| .FALSE. | .FALSE. | .TRUE. | .FALSE. |