HP Fortran Programmer's Reference (September 2007)

HP Fortran statements
WHERE (statement and construct)
Chapter 10494
The first two assignment statements are processed for elements corresponding to true
elements of the mask. The second two assignment statements are processed for elements
corresponding to false elements of the mask. Unlike the ELSE clause of an IF statement, the
assignment statements in both the WHERE and ELSEWHERE parts are processed.
Note the different behavior of the calls to ABS. In evaluating the mask expression, the entire
VALUES array is passed to ABS, producing an array result whose elements are then compared
to 10. In the assignment statement, however, ABS is only invoked for those particular
elements of VALUES corresponding to true elements of the mask. Also, note the mixed use of
arrays and scalars in the assignment statement expressions.
The mask expression must have the same shape as the arrays in the assignment statements,
but it might involve completely separate arrays. In the following example, A, B, and C can be
independent of D and E, as long as they are all conformable:
WHERE (a+b .EQ. c) d = SIN(e)
The following example illustrates why the order of processing is important for dependency
reasons:
REAL a(100)
REAL b(100)
EQUIVALENCE b, a
WHERE(a(1:20:1) .GT. 0) a(20:1:-1) = -1.0
WHERE(a(61:100:2) .LT. 1) b(20:1:-1) = a(1:20:1) * 100.0
In the first WHERE statement, changing elements of a in the assignment might be thought to
affect the mask expression. However, because the mask is evaluated before the assignment is
processed, the behavior of this statement is well defined. A similar situation arises in the
second WHERE statement. Assignment values to elements of the assignment variable b alter
the elements of the assignment expression a * 100.0. Because the assignment expression is
evaluated for all true elements of the mask before any transfer of values to B, the behavior is
again well defined.
It is important to note that assignment statements in a WHERE construct are processed
sequentially. In the next example, the second assignment is not processed until the first is
completely finished. This means that the values of b used in the second assignment have been
modified by the first statement:
WHERE (SQRT(ABS(a)) .gt. 3.0)
b = SIN(a)
c = SQRT(b)
ENDWHERE
Related statements
END (construct) and ELSEWHERE