• Aucun résultat trouvé

CHARACTER ASSIGNMENTS

Dans le document VS FORTRAN (Page 77-82)

You can~use character assignments to initialize character items.

For example:

CHARACTER*10 SUVAR SUVAR='ABCDEFGHIJ'

which assigns the value ABCDEFGHIJ to the character variable SUVAR.

When the operands are character items or expressions, you can specify the item to the left and the item(s) to the right of the equal sign with differing lengths. 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

In the assignment statement, 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

(Note that the characters ES are truncated.)

Coding Your Program--Advanced Programming 59

LOGICAL ASSIGNMENTS

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:

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

There's one restriction upon character assignment statements:

the item 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 can get unpredictable results, but you won't get an error

message.

Reference documentation for character assignments is given in the VS FORTRAN Application Programming: language Reference manual.

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 righthand operands and expressions can be logical items, and logical or relational expressions.

Within the relational expressions, you can use arithmetic or character operands.

For example, you can use arithmetic operands, as follows:

LOGICAL *4 LOGOP

DOUBLE PRECISION ARl/!.l/,AR2/2.2/,AR3/3.3/,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 tested. Then if AR2 is equal to AR3, "true"

is placed in LOGOP; otherwise "false" is placed in LOGOP.

For example, you can use character operands, as follows:

LOGICAL *4 LOGOP

CHARACTER *6 CHAR!, CHAR2, CHAR3 DATA CHARl/'ABCOEF'/ CHAR2/'GHIJKL'/

LOGOP

=

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

"true" is placed in LOGOP when CHAR2 and CHAR3 are equal and CHARl is less than CHAR2; otherwise, "false" is placed in lOGOP.

(Unless their values change during execution, CHAR! always evaluates as less than CHAR2.)

Reference documentation for logical assignments is given in the VS FORTRAN Application Proqramming: language Reference manual.

SAVING CODING EFFORT WITH STATEMENT FUNCTIONS

If your program makes the same complex calculation a number of times, you can define a statement function and then, whenever the program must make the given calculation,_refer to that function.

For example,

WORKeA,B,C,D,E)·: 3.274*A + 7.477*B - C/D + eX+Y+Z)/E defines the statement function WORK, where WORK is the function name and A, B, C, 0, and E are the dummy arguments.

60 VS FORTRAN Applicat)on Programming: Guide

o

)

( (I"'-i

J

(0

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

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

The function reference might appear in a statement as follows:

W

=

WORKCGAS,OIL,TIRES,BRAKES,PLUGS) - V This 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, 0, and E in the function definition and the actual arguments GAS, OIL, TIRES, BRAKES, and PLUGS in the function reference.

For reference documentation about statement functions, see the VS FORTRAN Application Programming: Language Reference manual.

CONTROLLING PROGP.AM FLOW

Unless you explicitly change the flow of control, VS FORTRAN programs execute one statement after another sequentially. In VS FORTRAN, you can alter the sequence of control, using the ASSIGN, DO, GO TO, IF, PAUSE, STOP, and END statements.

In VS FORTRAN, the arithmetic and logical IF statements and the unconditional GO TO statement work in the same way they've done

in older IBM FORTRAN implementations.

However, there are new VS FORTRAN programming options avai lable-such as the block IF statement-end changed

programming rules for the DO statement, assigned and computed GO TO statements, and the PAUSE, STOP, and END statements.

Programming considerations for these new language features are reviewed in the following sections.

USING STRUCTURED PROGRAMMING-BLOCK IF STATEt1ENT

The VS FORTRAN structured programming statements-the block IF statement and its associated ELSE IF, ELSE, and END IF

statements-help you create programs that conform to structured programming rules:

1. Write all code in control structures.

2. Construct each control structure so that it has only one entrance and only one exit.

3. Make each control structure nestable.

4. Control program flow along paths that define the structure itself.

5. Indent the source code to reflect the logic flow between control structures.

These rules make programs simpler and easier to understand; each structure is small and self-contained, and the structure of the program reflects its logic.

The block IF statement-together with its subsidiary ELSE IF, ELSE, and END IF statements-helps you wri te VS FORTRAN programs that conform to these structured programming rules. They let you nest IF procedures in a simple straightforward way.

Coding Your Program-Advanced Programming 61

Two terms are needed to explain the concepts of the block IF statement,

IF-level

and

IF-block:

IF-level

The number of IF-levels; n a program un; t -; s determined by the number of sets of block-IF statements (IF THEN and END IF statements).

The IF-level of a particular statement is determined with the formula:

nl - n2

wher-e:

nl

"2

is the number of block IF statements from the beginning of the program unit up to and including this statement.

is the numb~r of END IF 5tat~ment5 in the program unit up to, but not including, this statement.

Thus, in the following example:

10 IF (A . EQ. 5) THEN 11

20 IF (C+D .EQ. 0) THEN 21

29

30 END IF 39

40 END

IF-The block IF at number 10 is at IF levell, and the block IF statement at statement 20

is at IF level 2.

IF-block

An IF-block begins with the first statement after the block IF statement (IF THEN), and ends with the

statement preceding the next ELSE IF, ELSE, or END IF statement at the same IF-level as the block IF

statement. The IF-block includes all the executable statements in between.

Thus, in th~ preceding e~ample, the IF blocks are:

• Statements 11-39 inclusive.

• Statements 21-29 inclusive.

In Figure 15, the IF blocks for each example are:

Example 1: The IF block ends with the s~atement

preceding END IF.

Example 2: The IF block ends with the statemen~

preceding ELSE.

Example 3: The IF block ends with the statement preceding ELSE IF.

You can code an empty IF-block; that is, you can code an IF-block that has no executable statements in it.

You must not transfer control into an IF-block from outside the IF-block.

62 VS FORTRAN Application Programming: Guide

/(_ .. "'\

\'<IL~p,'

( (

"false" and ex~ression2 is "true") ELSE

END IF

(code executed if both ex~ression 1 and ex~ression2 are "false")

Note: In thi s thi rd form, you can repeat ELSE IF blocks as your program's logic requires; for example:

IF (expression!) THEN

(code executed if expression! is "true") ELSE IF (expression2) THEN

(code executed if ex~ression2

is "true")

ELSE IF (expression3) THEN'

(code executed if express;on3 is "true")

ELSE IF (expression4) THEN

(code executed if expression4 is "true")

Coding Your Program--Advanced Programming 63

1. The first block IF statement has an IF block that includes the range of statements to the ELSE IF statement.

2. The ELSE IF THEN statement has as its ELSE IF block the range to the ELSE statement at the same level (the first ELSE

statement).

After your program executes an ELSE IF block, control is transferred to the END IF statement at the same level.

3. The first ELSE statement is the alternative condition for the ELSE IF THEN statement.

4. The second IF THEN statement is subordinate to the first ELSE statement. This IF block continues to the first END IF

statement.

5. The second ELSE statement is the alternative condition for the second IF THEN statement. Its ELSE block is the range of code to the first END IF statement.

6. The CONTINUE statement provides a convenient reference point for the DO-loop code executed in the current nested ELSE block. (The nested DO loop must be completely contained within the current block; in this example, the current nested ELSE block.)

7. The first END IF statement corresponds to the nested block IF statement.

8. The second END IF statement corresponds to the first block IF statement (the one that begins the entire code sequence).

Reference documentation for the block IF statement is given in the VS FORTRAN Application Programming: language Reference manual.

Dans le document VS FORTRAN (Page 77-82)