HP Fortran Programmer's Reference (September 2007)

Expressions and assignment
Assignment
Chapter 4102
WHERE (a > b)
a = b
b = 0
ELSEWHERE
b = a
a = 0
END WHERE
is evaluated as if it was specified as:
mask = a > b
WHERE (mask) a = b
WHERE (mask) b = 0
WHERE (.NOT.mask) b = a
WHERE (.NOT.mask) a = 0
Only assignment statements may appear in a WHERE block or an ELSEWHERE block. Within a
WHERE construct, only the WHERE statement may be the target of a branch.
The form of a WHERE construct is similar to that of an IF construct, but with this important
difference: no more than one block of an IF construct may be executed, but in a WHERE
construct at least one (and possibly both) of the WHERE and ELSEWHERE blocks will be executed.
In a WHERE construct, this difference has the effect that results in a WHERE block may feed into,
and hence affect, variables in the ELSEWHERE block. Notice, however, that results generated in
an ELSEWHERE block cannot feed back into variables in the WHERE block.
The following example score2grade.f90 illustrates the use of a masked assignment to find the
letter-grade equivalent for each test score in the array test_score. To do the same operation
without the benefit of masked array assignment would require a DO loop iterating over the
array either in an IF-ELSE-IF construct or in a CASE construct, testing and assigning to each
element at a time.
Example 4-2 score2grade.f90
PROGRAM main
! illustrates the use of the WHERE statement in masked array
! assignment
!
! use an array constructor to initialize the array that holds
! the numerical scores
INTEGER, DIMENSION(10) :: test_score = &
(/75,87,99,63,75,51,79,85,93,80/)
! array to hold the equivalent letter grades (A, B, C, etc.)
CHARACTER, DIMENSION(10) :: letter_grade
! because the array arguments are declared in the procedure
! as assumed-shape arrays, the procedure’s interface must
! be explicit
!
INTERFACE
SUBROUTINE convert(num, letter)