Operations

Arithmetic Operators

The basic arithmetic operations of addition, subtraction, multiplication, division, and exponentiation (raising to a power) are all possible in FORTRAN 77. Addition and subtraction in FORTRAN 77 use the same familiar symbols + and -. However, multiplication (which is denoted in a variety of ways in mathematics) is represented in FORTRAN 77 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 77 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.

Example

The arithmetic expression -an+b×c-d÷e is written in FORTRAN 77 as -A**N+B*C-D/E and is evaluated in the following order:

  1. A**N
  2. B*C
  3. D/E
  4. - in front of A**N
  5. + between -A**N and B*C
  6. - between B*C and D/E

Example

The arithmetic expression a+(bm)n/(c-d) is written in FORTRAN 77 as A+((B**M)**N)/(C-D) and is evaluated in the following order:

  1. B**M
  2. raise B**M to the N power
  3. C-D
  4. / between (B**M)**N and C-D
  5. + between A and the rest of the expression

Here 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 77. Neither square brackets [ ] nor curly braces { } are used.

Character Operators

Two CHARACTER strings can be joined together in a process called concatenation. The concatenation operator is a double forward slash //.

Example

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 stringand 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.

Example

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.

Logical Operators

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.

Example

The FORTRAN 77 expression

A .OR. .NOT. (B .OR. C) .AND. D

where all of the variables are of type LOGICAL is evaluated in the following order:

  1. B .OR. C
  2. .NOT. in front of B .OR. C
  3. .AND. between .NOT. (B .OR. C) and D
  4. .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

  1. .NOT. B
  2. C .AND. D
  3. .OR. between A and .NOT. B
  4. .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

Relational operators compare two expressions of a similar type and evaluate to either .TRUE. or .FALSE.

Comparing Numbers Comparing Booleans
OperatorMeaning OperatorMeaning
.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 77 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.

Example

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.

Example

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 .EQV. relational operator is true if both operands are identical, i.e. if both are true or if both are false. The .NEQV. relational operator is the negation of .EQV.. It is true only if one operand is true and the other is false. It is also known as exclusive OR. Note how it differs from .OR., which is true if either or both operands is true.

A B A .EQV. B A .NEQV. B
.TRUE. .TRUE. .TRUE. .FALSE.
.TRUE. .FALSE. .FALSE. .TRUE.
.FALSE. .TRUE. .FALSE. .TRUE.
.FALSE. .FALSE. .TRUE. .FALSE.