You may not realize it, but most of you have experience programming arithmetic assignment statements. If I asked you to solve the current homework problem with a spreadsheet, you might begin by putting the time of fall in box A1 of the spreadsheet. Then in box A2, you would enter the formula "=0.5*9.8*A1^2. Box A2 would contain the distance of fall, and you would have just finished a simple programming job.

Fortran arithmetic assignments are very similar to those used in spreadsheets, Basic, or C. We had a simple example in the last class setting "s=a+b". For a spreadsheet, it is implied that "A2", is to the left of the equals. For Fortran or any similar language, you must include the name of a variable (shorthand for a memory address) to the left of the "=" to receive the results of the operations to the right of the "=".

+ add - subtract * multiply / divide ** exponentiateThe Fortran expression, equivalent to our spreadsheet example above, might be:

**Parentheses**- Fortran first performs operations grouped within parentheses. Within a given set of parentheses the precedence below is followed. There are no surprises here. Fortran interprets the use of parentheses in the way you have been trained during years of math courses.**Exponentiation (** or ^) -**Once parentheses are sorted out, Fortran takes the value to the left of every "**" and raises it to the power given by the value immediately to the right. Remember if a sequence like a**b**c occurs, it is interpreted as a**(b**c).**Multiplication and Division (* and /)**- given values existing or generated from any parenthetic expressions or exponentiation, Fortran next sweeps the arithmetic expression from left to right and performs each multiply and divide as they are found, using the values immediately to the left and right of the operator.**Addition and Subtraction (+ and -)**- given values existing or generated by operations with higher precedence, Fortran finally sweeps the arithmetic expression from left to right and performs each add and subtract as they are found, using the values immediately to the left and right of the operator.

As an example let's look at the following expression:

Next a sweep is made evaluating multiplications and divides:

x(a+b) or (a+b)xIn Fortran you must explicitly include the multiplication operator to avoid confusion with the use of functions:

x*(a+b) or (a+b)*x

You had an example of how to declare Fortran variables to be real numbers. If you want them to be integers, the declaration statement is analogous. For example to let Fortran know that the variables "ians", "i1", and "i2" are integers include the statement:

Let's look at what happens with the following lines of code.

At times you will want to do calculations involving mixtures of reals and integers. The results can be somewhat unexpected, until you recognize the pattern. If both variables (or constants) associated with a given operator (+,-,*,/) are integers, then the integer arithmetic unit is used and an integer value is returned for that portion of the calculation. If one is real, then the other is converted to real, and a real value is returned from the operation. Take a careful look at the examples of mixed arithmetic in arith.f.

If the value generated by the operations on the right side of the "=" is real, but the variable on the left side of the "=" is integer then the result of the calculation is truncated down to the next integer value less than or equal to the real result.

For example a Fortran statement:

Be very careful, with this type of operation. You may think that the result of a set of real arithmetic is going to be 2.0. However, due to the finite precision of computer arithmetic the computed result may be 1.999999. If this is converted to an integer, the answer is 1.

c = 3.0e8

If I needed the mass of an electron, I might make the assignment:

emass = 9.11e-31

Instead of an executable assignment statement like:

c = 3.0e8

You can get the compiler to put the value 3.0e8 into the location represented by "c", so it will be ready as soon as the program starts to execute. One way of doing this is a DATA statement:

data c / 3.0e8/

The value associated with the variable is always surrounded by a pair of slashes. Look at arith.f for examples of the use of the DATA statement to define more than one variable, paying attention to the use of commas to separate variable names and values.

Another way of preassigning a value to "c" is the PARAMETER statement:

parameter (c = 3.0e8)

The parentheses are always present in the syntax, as is the "=". Look at arithp.f for examples setting more than one parameter in a given statement, and for use of arithmetic assignments within the PARAMETER statement.

Because DATA and PARAMETER are "non-executable", they must be placed in the block of statements before "executable" statements (with REAL and INTEGER statements). They should be positioned after the REAL or INTEGER statements that establish the data type of the particular variables or parameters defined in the specific DATA and PARAMETER statements.

Notice that I'm starting to talk about variables and parameters as separate objects. That is because there are some important differences between the items set in DATA statements and those defined in PARAMETER statements. Anything set in a DATA statement is a true Fortran variable. It can be given a different value elsewhere in the program with an arithmetic assignment statement. The parameters assigned in a PARAMETER statement behave exactly like Fortran constants (2.0, 2, 2.0e0, etc) within a program. They can be used anywhere a constant can, and like a constant have a fixed value that can't be altered by the program. You will later see statement constructs that want constants in certain positions and don't accept variables. Any attempt to use a parameter on the left side of an "=" outside of the PARAMETER statement itself, will result in a Fortran syntax error at compilation time.

The restriction on reassignment of parameters is accompanied by the flexibility of using arithmetic expressions to initial set the value of the parameter. These expressions can contain constants and any previously defined parameter. No such capability exists within a DATA statement. The DATA statement is a straight initial assignment of the value of a Fortran constant to a variable.

We will have to learn about Fortran functions, subroutines, and arrays, before the full power of DATA and PARAMETER statements becomes apparent.

Test your knowledge with some review questions on this lecture.

Maintained by John Mahaffy : jhm@cac.psu.edu