Expressions and assignments

Expression: Describes a computation to be carried out.

Assignment: Result of computation is assigned to  variable.

Operands and operators

Monadic (unary) example: -x

Dyadic (binary) example: x+y

Expressions are evaluated from left to right for operators of equal precedence, with the exception of exponentiation (**).

Parentheses may be used to indicate which subexpression should be evaluated first.

The processor is permitted to evaluate an equivalent expression: a/b/c might be evaluated as a/(b*c).

Scalar numeric expressions

Operands are of type integer, real or complex.

Operators (in decreasing order of precedence):

** exponentiation
* / multiplication, division
+ - addition, subtraction

x**(-y) requires parentheses

-a+b+c will be evaluated as ((-a)+b)+c

a**b**c will be evaluated as a**(b**c)

Result of integer division will be truncated towards zero:

6/3 is 2, 8/3 is 2, -8/3 is -2

2**(-3) is 1/(2**3) which is zero.

Fortran allows mixed-mode expressions using operands of differing types. Except for exponentiation to an integer power, the object of the weaker type will be converted into the stronger one.

For +, -, *, /, ** operations a .op. b:

a b result
I I I
I R R
I C C
R I R
R R R
R C C
C I C
C R C
C C C

Defined and undefined variables

A variable which has been declared but has not been assigned a value is undefined. No attempt must be made to reference its value since it has none.

Scalar numeric assignment

variable = expression

If expression is of different type, it will be converted.

There may be a loss of precision, or a loss of the imaginary part:

i = 273.15       ! i = 273
a = (2.3, -0.45) ! a = 2.3

Scalar relational operators

Compare numeric or character expressions. Result is .false. or .true.

< less than
<= less than or equal
== equal
/= not equal
> greater than
>= greater than or equal

For complex expressions, only == and /= are available.

Numeric operators take precedence over relational operators.

Examples:

i > 0            ! integer
a-b < 1e-6       ! real (beware of a==b)
line(i:i) == 'K' ! character

Scalar logical expressions and assignments

Logical operators (in decreasing order of precedence):

.not. negation (unary)
.and. intersection
.or. union
.eqv. .neqv. equivalence, non-equivalence

The result of a logical expression (.false. or .true.)  may be assigned to a logical variable.

Scalar character expressions and assignments

Concatenation operator //

Example: 'AB'//'CD' results in 'ABCD'

Truncation or padding with blanks will occur in assignments if lengths are different.

line(11:15) = 'truncate' ! results in 'trunc'

Array expressions

Any of the intrinsic operations may be applied to arrays. In binary operations both arrays must be of the same shape. Correspondence is by position in the extent not by subscript value. One of the operands may be a scalar.

Examples:

real, dimension(10,20) :: a, b
real, dimension(5) :: v
a / b
v + 1.
5/v+a(1:5,5) ! shape (5)
a(2:9,5:10) + b(1:8,15:20) ! shape(8,6)

Array assignment

An array expression may be assigned to an array variable of the same shape.

A scalar expression may be assigned to an array (scalar value is broadcast to all elements).

The order in which the elements are assigned is not specified, to allow optimizations.

Examples:

a = a + 1.0
a(1,11:15) = v
v(2:5)=v(1:4) ! fully evaluated before assignment

Exercises

1. Improve the Celsius-to-Fahrenheit program by adding comments, strong typing and constants of type real.

2. Instead of reading a single input value, set up a named array constant containing equally spaced Celsius temperatures, e.g. from -15 to 50 degrees Celsius in steps of 5 degrees. Use an array expression and an array assignment to obtain the corresponding Fahrenheit values.