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).
Operands are of type integer, real or complex.
Operators (in decreasing order of precedence):
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 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.
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
Compare numeric or character expressions. Result is .false. or .true.
|<=||less than or equal|
|>=||greater than or equal|
For complex expressions, only == and /= are available.
Numeric operators take precedence over relational operators.
i > 0 ! integer a-b < 1e-6 ! real (beware of a==b) line(i:i) == 'K' ! character
Logical operators (in decreasing order of precedence):
The result of a logical expression (.false. or .true.) may be assigned to a logical variable.
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'
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.
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)
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.
a = a + 1.0 a(1,11:15) = v v(2:5)=v(1:4) ! fully evaluated before assignment
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.