DOCX

A COMPREHENSIVE GUIDE TO FORTRAN

By Veronica Baker,2014-06-16 18:51
7 views 0
A COMPREHENSIVE GUIDE TO FORTRANA,TO,to,GUIDE,Guide,guide

A COMPREHENSIVE GUIDE TO FORTRAN (F90/F77)

    Fortran Comments

    Comments should be used liberally to improve readability. The following are the rules for making comments:

    ; All characters following an exclamation mark, !, except in a

    character string, are commentary, and are ignored by the

    compiler.

    PROGRAM TestComment1 .......... READ(*,*) Year ! read in

    the value of Year .......... Year = Year + 1 ! add 1 to

    Year .......... END PROGRAM TestComment1

    ; An entire line may be a comment

    ! This is a comment line ! PROGRAM TestComment2 ......... !

    This is a comment line in the middle of a program ......... END

    PROGRAM TestComment2

    ; A blank line is also interpreted as a comment line.

    PROGRAM TestComment3 .......... READ(*,*) Count ! The

    above blank line is a comment line WRITE(*,*) Count + 2 END

    PROGRAM TestComment3

Fortran Continuation Lines

    In Fortran, a statement must start on a new line. If a statement is too long to fit on a line, it can be continued with the following methods:

    ; If a line is ended with an ampersand, &, it will be continued on

    the next line.

    ; Continuation is normally to the first character of the

    next non-comment line.

    A = 174.5 * Year & + Count / 100

    The above is equivalent to the following

    A = 174.5 * Year + Count / 100

    Note that & is not part of the statement.

    A = 174.5 * Year & ! this is a comment line + Count / 100

    The above is equivalent to the following, since the comment is

    ignored by the compiler:

    A = 174.5 * Year + Count / 100

    ; If the first non-blank character of the continuation line is &,

    continuation is to the first character after the &:

    A = 174.5 + ThisIsALong&&VariableName * 123.45

is equivalent to

    A = 174.5 + ThisIsALongVariableName * 123.45

    In this case, there should be no spaces between the last character and the & on the first line. For example, A = 174.5 + ThisIsALong &&VariableName * 123.45

    is equivalent to

    A = 174.5 + ThisIsALong VariableName * 123.45

    Note that there are spaces

    between ThisIsALong and VariableName. In this way, a token (name and number) can be split over two lines. However, this is notrecommended

    Fortran Alphabets

Fortran only uses the following characters:

    ; Letters:

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g

    h i j k l m n o p q r s t u v w x y z

    ; Digits:

    0 1 2 3 4 5 6 7 8 9

    ; Special Characters:

    space ' " ( ) * + - / : = _ ! & $ ; <> % ? , .

The Assignment Statement

The assignment statement has the following form:

    variable = expression

    Its purpose is saving the result of the expression to the right of

    the assignment operator to the variable on the left. Here are some

    rules:

    ; The expression is evaluated first with the rules discussed in

    the single mode or the mixed mode expressions pages.

    ; If the type of the expression is identical to that of the variable,

    the result is saved in the variable.

    ; Otherwise, the result is converted to the type of the variable and

    saved there.

    o If the type of the variable is INTEGER while the type of

    the result is REAL, the fractional part, including the

    decimal point, is removed making it an integer result.

    o If the type of the variable is REAL while the type of the

    result is INTEGER, then a decimal point is appended to

    the integer making it a real number.

    ; Once the variable receives a new value, the original one

    disappears and is no more available.

    ; CHARACTER assignment follows the rules stated in the

    discussion of the PARAMETER attribute.

    Examples:

    ; The program segment below declares

    three INTEGER variables. The first assignment statement

    saves an integer value to variable Unit. The second saves a

    real number 100.99 into variable Amount. However,

    since Amount is an INTEGER variable, the real value 100.99 is

    converted to an integer, 100, and saved intoAmount. Thus, after

    the second assignment completes, variable Amount holds 100.

    The third assignment computes the single mode expression,

    yielding a result 500 = 5*100. Thus, variable Total receives 500.

    INTEGER :: Total, Amount, Unit Unit = 5 Amount = 100.99 Total

    = Unit * Amount

    ; In the following, PI is a PARAMETER and is an alias of

    3.1415926. The first assignment statement puts integer value 5

    into integer variable Radius. The expression in the second

    assignment is first evaluated, yielding a result 78.539815, which

    is then saved into REAL variable Area.

    REAL, PARAMETER :: PI = 3.1415926 REAL :: Area

    INTEGER :: Radius Radius = 5 Area = (Radius ** 2) * PI

    ; In the following, Counter is an INTEGER variable initialized to

    zero.

    The meaning of the first assignment is computing the sum of the value in Counter and 1, and saves it back to Counter.

    Since Counter's current value is zero,Counter + 1 is 1+0 = 1 and hence 1 is saved into Counter. Therefore, the new value of Counter becomes 1 and its original value 0 disappears. The second assignment statement computes the sum of Counter's current value and 3, and saves the result back to Counter. Thus, the new value of Counter is 1+3=4. INTEGER :: Counter = 0 Counter = Counter + 1 Counter = Counter

    + 3

    ; The following swaps the values in A and B, with the help of C. That is, after completing the following three assignment statements, A and B have 5 and 3, respectively.

    Initially, A and B are initialized to 3 and 5, respectively,

    while C is uninitialized. The first assignment statement puts A's value into C, making A=3, B=5 and C=3.

    The second assignment statements puts B's value into A. This destroys A's original value 3. After this, A = 5, B = 5 and C = 3.

    The third assignment statement puts C's value into B. This

    makes A=5, B=3 and C=3. Therefore, the values in A and B are

    exchanged.

    INTEGER :: A = 3, B = 5, C C = A A = B B = C

    The following is another possible solution; but, it uses one more

    variable.

    INTEGER :: A = 3, B = 5, C, D C = A D = B A = D B = C

An Important Note:

    A name declared with the PARAMETER attribute is an alias of a

    value and is not a variable. Therefore, it cannot be used on the left-hand side of =, although it can be used on the right-hand side. The following is wrong!

    INTEGER, PARAMETER :: InchToCM = 2.54, factor = 123.45

    INTEGER :: X = 15 InchToCM = factor * X

Printer Control

    Traditionally Fortran provides a unique way for controlling printers. Except that your output are always sent to files, you need to know how Fortran controls printers.

    As discussed in edit descriptors, the values of variables and expressions of a WRITE statement are evaluated in the given order and based on the the width of field (i.e., the value of w) an output line

    is composed. In the good old days, printers have 133 positions per line and the content in the first position is for printer control.

    Nowadays, the length of a line is no more an issue; but, the content in the first position of an output line is still for printer control. Therefore, when you want to print your result with a printer, any printer, you should remember the following figure:

The way Fortran prints your information is line-oriented. More

    precisely, the information of each edit descriptor are not printed directly to a printer. Instead, they are "printed" to a buffer line. Once

    the construction of that buffer line completes (e.g., all values are

    processed), this buffer line is sent to printer and based on the content in the first position the printer advances to next line, advances two lines, jumps to the first line of the next page, or stays on the same line. Then, the contents from position 2 to the end of that line are printed. As a result, the content of the first position is not printed!

    As mentioned above, there are four printer controls, each of which is presented by a character:

    Printer Control Effect Character

    space Advance to the next line before printing

    0 Advance two lines before printing

    Advance to the first line of the next 1 page before printing

    + No advancing, print on the current line (overprinting)

    Let us discuss all four printer control characters in some detail:

    ; If the first position is a space, then the printer will advance to the

    next line and print. In the following example, constant 123 is

    printed using edit descriptor I4. Its output consists of four

    positions containing a space, 1, 2 and 3. Therefore, the printer

    control character is a space and the printer advances to the

    next line and prints 1, 2 and 3 in the first, second and third

    positions. After printing this line, the printer stops there. In the

    figure, prev indicates the previous line.

Report this document

For any questions or suggestions please email
cust-service@docsford.com