• Aucun résultat trouvé

Assigning Values to Variables, Array Elements, and Character Substrings

Dans le document FORMAT (3(1) STOP (Page 77-80)

In a VS FORTRAN program, the assignment statement lets you assign values to variables, array elements, and character substrings. It is distinguished from other FORTRAN statements by use of the equal sign.

The assignment statement closely resembles a conventional algebraic equation, except that the value to the right of the equal sign replaces (is assigned to) the value of the item to the left of the· equal sign.

You can use the assignment statement to assign a value to a variable, an array element, or a character substring.

You can specify arithmetic, character, and logical operands and expressions to the right of the equal sign.

Arithmetic Assignments

Character Assignments

You can assign the value of arithmetic operands and expressions to variables and array elements; the item(s) to the right of the equal sign don't have to be the same type or length as the item to the left of the equal sign.

For example, assuming default naming conventions (see "Implied Default Data Type Declaration" on page 12), if you make the following assignments, you'll get the indicated results:

PI = 3.14159

Assigns the real constant 3.14159 to the 4-byte real variable named PI.

ARRAY3(NUM) = DIFF

Assigns the value currently contained in the 4-byte real variable DIFF to the 4-byte real array element ARRA Y3 (NUM)

INTR = DIFF

The value of DIFF is converted to an integer value 4 bytes long (that is, the largest integer in the real item is used, without rounding) and placed in INTR.

DIFF

=

INTR

The value of INTR is converted to a 4-byte real value and placed in the variable DIFF.

DIFF = INTR+DIFF

The value of INTR is converted to a 4-byte real value and added to the current value of DIFF; the result is the new value of DIFF.

You can use and combine all the arithmetic operators, as shown in Figure 9 on page 46.

Character assignments are only allowed when you are initializing character items.

For example:

CHARACTER*10 SUVAR SUVAR = 'ABCDEFGHIJ'

which assigns the value ABCDEFGHIJ to the character variable SUV AR.

The items in the assignment statement need not be the same length. When you execute the assignment, the item to the left is either padded at the right with blanks or the data is truncated to fit into the item at the left:

CHARACTER *5 A,B,C,E *13

DATA A/'WHICH'/,B/' DOG '/,C/'BITES'/

E

=

A//B//C

Chapter 3. Using Expressions and Assignment Statements

51

Logical

Assignments

In the assignment statement above, the concatenation symbols (/ /) place the contents of A, B, and C one after another into E.

After the assignment statement is executed, the character variable E contains:

WHICH DOG BIT (The word "BITES" is truncated to "BIT.") You can also define character items on either side of the equal sign as substrings, in which.case only the substring portion of the item is acted upon:

A(4:5) = C(3:4)

After this assignment statement is executed, A in the previous example contains the characters "WHITE".

There's one restriction upon character assignment statements: The items to the left of the equal sign, and those to the right must not overlap in any way; that is, they must not refer to the same character positions, completely or in part. If they do, you will get unpredictable results.

If you compiled your program with Release 4, library messages IFY193I through IFY197I will not be generated. This is because of the faster character handling now being done in-line, which is described in "Writing Efficient Character Manipulations" on page 146.

When the operand to the left of the equal sign is a logical item, the operands or expressions to the right must evaluate to a logical value of either "true" or "false."

In a logical assignment, the items to the right may be either logical or relational expressions. Within the relational expressions, you can use arithmetic or character operands.

For example, using arithmetic operands:

LOGICAL *4 LOGOP

REAL *8 AR1,AR2,AR3,AR4

LOGOP = (AR4.GT.AR1) .OR. (AR2.EQ.AR3)

"true" is placed in LOGOP when AR4 is greater than ARl; otherwise, AR2 and AR3 must be compared. Then if AR2 is equal to AR3, "true" is placed in LOGOP; otherwise, LOGOP is evaluated as "false."

For example, using character operands:

LOGICAL *4 LOGOP

CHARACTER *6 CHAR1, CHAR2, CHAR3

LOGOP

=

(CHAR2.EQ.CHAR3).AND. (CHAR1.LT.CHAR2)

"true" is placed in LOGOP when CHAR2 and CHAR3 are equal and CHARI is less than CHAR2; otherwise, LOGOP is evaluated as "false."

~ Saving Coding Effort with Statement Functions

If your program makes the same complex calculation a number of times, you can simplify your program by defining a statement function that refers to the calculation by name. F or example,

WORK(A,B,C,O,E) = 3.274*A + 7.477*B - c/o + (X+Y+Z)/E

defines the statement function WORK, where WORK is the function name, and A, B, C, D, and E are the dummy arguments.

The expression to the right of the equal sign defines the operations to be performed when the function reference appears in an arithmetic statement. For example:

W = WORK (GAS, OIL, TIRES,BRAKES, PLUGS) -

v

is equivalent to:

W = 3.274*GAS + 7.477*OIL - TIRES/BRAKES + ,(X+Y+Z)/PLUGS - V Note the correspondence between the dummy arguments A, B, C, D, and E in the function definition and the actual arguments GAS, OIL, TIRES, BRAKES, and PLUGS in the function reference.

All statement function definitions must precede the first executable statement of the program.

Dans le document FORMAT (3(1) STOP (Page 77-80)