next up previous contents index
Next: General Rules Up: Arithmetic Previous: Arithmetic

Arithmetic Expressions

 

An expression in its simplest form is just a single operand, such as a constant or variable. More complicated expressions combine various operands with operators, which specify the computations to be performed. For example:
RATE * HOURS + BONUS
The rules of Fortran have been designed to resemble those of mathematics as far as possible, especially in determining the order in which the expression is evaluated. In this example the multiplication would always be carried out before the addition, not because if comes first, but because it has a higher precedence. When in doubt, or to over-ride the precedence rules, parentheses can be used:
(ROOM + DINNER) * 1.15

Sub-expressions enclosed in parentheses are always evaluated first; they can be nested to any reasonable depth. If in doubt, there is no harm in adding parentheses to determine the order of evaluation or to make a complicated expression easier to understand.

Arithmetic expressions can contain any of the five arithmetical operators  + - * / ** . The double asterisk represents exponentiation, i.e. raising a number to a power. Thus the mathematical expression:

(1 + RATE/100)years

could be represented in Fortran as:
(1.0 + RATE/100.0)**YEARS
(note the explicit decimal points in the constants to make them real values).

Arithmetic expressions can involve operands of different data types: the data type of the result is determined by some simple rules explained below.



 
next up previous contents index
Next: General Rules Up: Arithmetic Previous: Arithmetic
Helen Rowlands
8/27/1998