• Keine Ergebnisse gefunden

The do statement in Ratfor is quite similar to the DO statement in Fortran except that it uses no statement number (braces are used to mark the end of the do instead of a statement number). The syntax of tbe ratfor do statement is

do legal-Fortran-DO-text { rat/or statements

The legal-Fortran-DO-text must be something that can legally be used in a Fortran DO statement. Thus if a local version of Fortran allows DO limits to be expressions (which is not currently permitted in ANSI Fortran 66), they can be used in a ratfor do statement.

The rat/or statements are enclosed in braces; but as with the if, a single statement need not have braces around it. For example, the following code sets an array to zero:

do i = I, n xii) = 0.0 and the code

do i = l,n do j = I, n

m(i,j) = 0

sets the entire array m to zero.

10-6

THE

"break" AND "next" STATEMENTS The Ratfor break and next statements provide a means for leaving a loop early and one for beginning the next iteration. The break causes an immediate exit from the do; in effect, it is a branch to tbe statement after the do. The next is a branch to the bottom of the loop, so it causes the next iteration to be done. For example, this code skips over negative values in an array

do i = 1, n { if (x(i) < 0.0)

next

process positive element

The break and next statements will also work in the other Ratfor looping constructions and will be discussed with each looping construction.

The break and next can be followed by an integer to indicate breaking or .iterating that level of enclosing loop. For example:

break 2

exits from two levels of enclosing loops, and break 1

is equivalent to break. The next 2

iterates the second enclosing loop.

10-7

THE "while" STATEMENT

The Ratfor language provides a while statement. The syntax of the while statement is

while (legal-Fortran-condition) rat/or statement

As with the if, legal-Fortran-condition is something that can go into a Fortran Logical IF, and ratfor statement is a single statement which may be multiple statements enclosed in braces.

For example, suppose nextch is a function which returns the next input character both as a function value and in its argument. Then a while loop to find the first nonblank character could be

while (nextch(ich) == iblank)

where a semicolon by itself is a null statement (which is necessary here to mark the end of the while). If the semicolon were not present, the while would control the next statement. When the loop is exited, ich contains the first nonblank.

THE "for" STATEMENT

The for statement is another Ratfor loop. A for statement allows explicit initialization and increment steps as part of the statelllent.

The syntax of the for statement is for ( init ; condition; increment)

rat/or statemen t

where init is any single Fortran statement which is executed once before the loop begins. The increment is any single Fortran statement that is executed at the end of each pass through the loop before the test. The condition is again anything that is legal in a

Fortran Logical IF. Any of init, condition, and increment may be omitted although the semicolons mus.t always be present. A nonexistent condition is treated as always true, so

for (;;) is an infinite loop.

For example, a Fortran DO loop could be written as for (i = 1; i <= n; i = i

+

1) ...

which is equivalent to i = 1

while (i <= n) { i =i

+

1

The initialization and increment of i have been moved into the for statement.

The for, do, and while versions have the advantage that they will be done zero times if n is less than 1. In addition, the break and next statements work in a for loop.

The increment in a for need not be an arithmetic progression. The program

sum = 0.0

for (i

=

first; i > 0; i

=

ptr(i)) sum = sum + value(i)

steps through a list (stored in an integer array ptr) until a zero pointer is found while adding up elements from a parallel array of values. Notice that the code also works correctly if the list is empty.

10-9

THE

"repeat-until"

STATEMENT

There are times when a test needs to be performed at the bottom of a loop after one pass through. This' facility is provided by the repeat-until statement. The syntax for the repeat-until statement is

repeat

ratlor statement

until (legal-Fortran-condition )

where ratfor-statement is done once, then the condition is evaluated.

If it is true, the loop is exited; if it is false, another pass is made.

The until part is optional, so a repeat by itself is an infinite loop.

A repeat-until loop can be exited by the use of a stop, return, or break statement or an implicit stop such as running out of input with a READ statement.

As stated before, a break statement causes an immediate exit from the enclosing repeat-until loop. A next statement will cause a skip to the bottom of a repeat-until loop (Le., to the until part).

THE

"return"

STATEMENT

The standard Fortran mechanism for returning a value from a routine uses the name of the routine as a variable. This variable can be assigned a value. The last value stored in it is the value returned by the function. For example, in a Fortran routine named equal, the statements

equal = 0 return

cause equal to return zero.

The Ratfor language provides a return statement similar to the C language return statement. In order to return a value from any routine, the return statement has the syntax

return ( expression)

where expression is the value to be returned.

If there is no parenthesized expression after return, no value is returned.

THE "define" STATEMENT

The Ratior language provides a define statement similar to the C language version. Any string of alphanumeric characters can be defined as a name. Whenever that name occurs in the input (delimited by nonalphanumerics), it is replaced by the rest of the definition line. (Comments and trailing white spaces are stripped off.) A defined name can be arbitrarily long and must begin with a letter.

Usually the define statement is used for symbolic parameters. The syntax of the define statement is

define name value

where name is a symbolic name that represents the quantity of value.

For example:

define ROWS 100 define CLOS 50

dimension a(ROWS), b(ROWS, COLS) if (i > ROWS I j > COLS) ...

IO-ll

causes the preprocessor to replace the name ROWS with the value 100 and the name COLS with the value 50. Alternately, definitions may be written as

define(ROWS, 1(0)

in which case the defining text is everything after the comma up to the right parenthesis. This allows multiple-line definitions.